Skip to content

Commit

Permalink
[bug 810437] Add sort for documents (advanced) search.
Browse files Browse the repository at this point in the history
New parameter is sortby_documents={relevance, helpful}.
  • Loading branch information
rlr committed Nov 20, 2012
1 parent c67736c commit ede4acd
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
11 changes: 11 additions & 0 deletions apps/search/__init__.py
Expand Up @@ -63,3 +63,14 @@
(2, _lazy(u'Question date')),
(3, _lazy(u'Number of answers')),
)


SORT_DOCUMENTS = {
'relevance': ('-_score',),
'helpful': ('-document_recent_helpful_votes',),
}

SORTBY_DOCUMENTS_CHOICES = (
('relevance', _lazy(u'Relevance')),
('helpful', _lazy(u'Helpful votes')),
)
6 changes: 6 additions & 0 deletions apps/search/forms.py
Expand Up @@ -90,6 +90,12 @@ def clean(self):
include_archived = forms.BooleanField(
required=False, label=_lazy('Include obsolete articles?'))

sortby_documents = forms.TypedChoiceField(
required=False,
empty_value=constants.SORTBY_DOCUMENTS_CHOICES[0][0],
label=_lazy('Sort results by'),
choices=constants.SORTBY_DOCUMENTS_CHOICES)

# Support questions and discussion forums fields
created = forms.TypedChoiceField(
required=False, coerce=int, empty_value=0,
Expand Down
5 changes: 5 additions & 0 deletions apps/search/templates/search/form.html
Expand Up @@ -61,6 +61,11 @@ <h1 class="nomargin">Advanced Search</h1>
</div>
</div>

<div class="container cf">
{{ search_form.sortby_documents.label_tag()|safe }}
{{ search_form.sortby_documents|safe }}
</div>

{% for hidden in search_form.hidden_fields() if not hidden.name == 'w' %}
{{ hidden|safe }}
{% endfor %}
Expand Down
33 changes: 32 additions & 1 deletion apps/search/tests/test_es.py
Expand Up @@ -23,7 +23,7 @@
from sumo.urlresolvers import reverse
from topics.tests import topic
from users.tests import user
from wiki.tests import document, revision
from wiki.tests import document, revision, helpful_vote


class ElasticTestCase(TestCase):
Expand Down Expand Up @@ -348,6 +348,37 @@ def test_advanced_search_questions_sortby(self):
content = json.loads(response.content)
eq_(content['total'], 1)

def test_advanced_search_sortby_documents_helpful(self):
"""Tests advanced search with a sortby_documents by helpful"""
r1 = revision(is_approved=True, save=True)
r2 = revision(is_approved=True, save=True)
helpful_vote(revision=r2, helpful=True, save=True)

# r2.document should come first with 1 vote.
self.setup_indexes()
self.refresh()
response = self.client.get(reverse('search'), {
'w': '1', 'a': '1', 'sortby_documents': 'helpful',
'format': 'json'})
eq_(200, response.status_code)

content = json.loads(response.content)
eq_(r2.document.title, content['results'][0]['title'])

# Vote twice on r1, now it should come first.
helpful_vote(revision=r1, helpful=True, save=True)
helpful_vote(revision=r1, helpful=True, save=True)

self.setup_indexes()
self.refresh()
response = self.client.get(reverse('search'), {
'w': '1', 'a': '1', 'sortby_documents': 'helpful',
'format': 'json'})
eq_(200, response.status_code)

content = json.loads(response.content)
eq_(r1.document.title, content['results'][0]['title'])

def test_advanced_search_questions_num_votes(self):
"""Tests advanced search for questions num_votes filter"""
q = question(title=u'tags tags tags', save=True)
Expand Down
17 changes: 14 additions & 3 deletions apps/search/views.py
Expand Up @@ -291,9 +291,9 @@ def search(request, template=None):
document_title__text_phrase=10.0,
document_content__text_phrase=8.0)

# Apply sortby, but only for advanced search for questions
if a == '1' and cleaned['w'] & constants.WHERE_SUPPORT:
sortby = smart_int(request.GET.get('sortby'))
# Apply sortby for advanced search of questions
if cleaned['w'] == constants.WHERE_SUPPORT:
sortby = cleaned['sortby']
try:
searcher = searcher.order_by(
*constants.SORT_QUESTIONS[sortby])
Expand All @@ -302,6 +302,17 @@ def search(request, template=None):
# sending us sortby values that aren't valid.
pass

# Apply sortby for advanced search of kb documents
if cleaned['w'] == constants.WHERE_WIKI:
sortby = cleaned['sortby_documents']
try:
searcher = searcher.order_by(
*constants.SORT_DOCUMENTS[sortby])
except IndexError:
# Skip index errors because they imply the user is
# sending us sortby values that aren't valid.
pass

# Build the query
if cleaned_q:
query_fields = chain(*[cls.get_query_fields()
Expand Down

0 comments on commit ede4acd

Please sign in to comment.