diff --git a/apps/search/es_utils.py b/apps/search/es_utils.py index 6d9b94bb203..8bc0b08b37a 100644 --- a/apps/search/es_utils.py +++ b/apps/search/es_utils.py @@ -1,11 +1,12 @@ -from itertools import chain, count, izip +import json import logging +from itertools import chain, count, izip + +from django.conf import settings import elasticutils import pyes -from django.conf import settings - ESTimeoutError = pyes.urllib3.TimeoutError ESMaxRetryError = pyes.urllib3.MaxRetryError @@ -218,3 +219,46 @@ def es_status_cmd(): log.info(' %-20s: %d', name, count) else: log.info(' Write index is same as read index.') + + +def es_search_cmd(query): + """Simulates a front page search + + .. Note:: + + This **doesn't** simulate an advanced search---just a front + page search. + + """ + from sumo.tests import LocalizingClient + from sumo.urlresolvers import reverse + + client = LocalizingClient() + + output = [] + output.append('Search for: %s' % query) + output.append('') + + data = { + 'q': query, + 'q_tags': 'desktop', 'product': 'desktop', 'format': 'json' + } + url = reverse('search') + + # The search view shows 10 results at a time. So we hit it few + # times---once for each page. + for pageno in ('1', '2', '3'): + data['page'] = pageno + resp = client.get(url, data) + assert resp.status_code == 200 + + content = json.loads(resp.content) + results = content[u'results'] + + for mem in results: + output.append(u'%4d %5.2f %-10s %-20s' % ( + mem['rank'], mem['score'], mem['type'], mem['title'])) + + output.append('') + + print '\n'.join(output) diff --git a/apps/search/management/commands/essearch.py b/apps/search/management/commands/essearch.py new file mode 100644 index 00000000000..3337c91fbc2 --- /dev/null +++ b/apps/search/management/commands/essearch.py @@ -0,0 +1,18 @@ +import logging + +from django.core.management.base import BaseCommand, CommandError + +from search.es_utils import es_search_cmd + + +class Command(BaseCommand): + help = 'Does a front-page search for given query' + + def handle(self, *args, **options): + logging.basicConfig(level=logging.INFO) + if not args: + raise CommandError('You must specify the search query.') + + query = u' '.join(args) + + es_search_cmd(query) diff --git a/apps/search/views.py b/apps/search/views.py index be9c226760c..2e6bbc20806 100644 --- a/apps/search/views.py +++ b/apps/search/views.py @@ -310,6 +310,7 @@ def search_with_es(request, template=None): 'object': ObjectDict(doc)} result['search_summary'] = summary result['rank'] = rank + result['score'] = doc._score results.append(result) except (ESTimeoutError, ESMaxRetryError, ESException), exc: @@ -863,6 +864,6 @@ def _build_es_excerpt(result): """ excerpt = EXCERPT_JOINER.join( [m.strip() for m in - chain(*result.highlighted.values()) if m]) + chain(*result._highlighted.values()) if m]) return jinja2.Markup(clean_excerpt(excerpt)) diff --git a/vendor/src/elasticutils b/vendor/src/elasticutils index d2c5aa3878a..61020c3697a 160000 --- a/vendor/src/elasticutils +++ b/vendor/src/elasticutils @@ -1 +1 @@ -Subproject commit d2c5aa3878a940d4f97bb1deef8c4206363bd3b7 +Subproject commit 61020c3697af1b750e73b5ed79c4371c12bd9b26