Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions apps/search/es_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from itertools import chain, count, izip
import json
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, and we keep imports in alphabetical order. Sorry for the OCD :-)

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
Expand Down Expand Up @@ -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']))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of adding some kind of separator between each page? /nit


output.append('')

print '\n'.join(output)
18 changes: 18 additions & 0 deletions apps/search/management/commands/essearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import logging
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our import grouping convention is to have a space after this import and another after the one below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this. I thought we group all the stdlib stuff together, then group things by library. What should the import lines look like?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I should have said newline :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't understand. Is this specifically about the logging module? Can you show me what the import lines should look like here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this:

import logging

from django.core.management.base import BaseCommand, CommandError

from search.es_utils import es_search_cmd

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, whoops. I was looking at the wrong file. Bah.


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)
3 changes: 2 additions & 1 deletion apps/search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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))
2 changes: 1 addition & 1 deletion vendor/src/elasticutils
Submodule elasticutils updated 2 files
+8 −6 elasticutils/__init__.py
+13 −2 tests/tests.py