-
Notifications
You must be signed in to change notification settings - Fork 830
Add cli for front page search #555
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'])) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import logging | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I should have said newline :-)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like this:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| +8 −6 | elasticutils/__init__.py | |
| +13 −2 | tests/tests.py |
There was a problem hiding this comment.
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 :-)