Skip to content

Commit

Permalink
Improve handling of the FacetedSearch sort keyword-argument (elastic#575
Browse files Browse the repository at this point in the history
)

* Allow to FacetedSearch class to sort by multiple fields, not just the first one

* add backwards compat for sort behaviour
  • Loading branch information
wimglenn authored and honzakral committed Feb 22, 2017
1 parent 1a90924 commit 0e26599
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ Andy Barilla <andrew.barilla@one.verizon.com>
Rajiv Bakulesh Shah <brainix@gmail.com>
Gabor Nagy <mail@aigeruth.hu>
Shezad Khan <shaz@revl.world>
Wim Glenn <hey@wimglenn.com>
11 changes: 7 additions & 4 deletions elasticsearch_dsl/faceted_search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta, datetime
from six import iteritems, itervalues
from six import iteritems, itervalues, string_types

from .search import Search
from .aggs import A
Expand Down Expand Up @@ -214,15 +214,18 @@ def search(self):
fields = ('*', )
facets = {}

def __init__(self, query=None, filters={}, sort=None):
def __init__(self, query=None, filters={}, sort=()):
"""
:arg query: the text to search for
:arg filters: facet values to filter
:arg sort: sort information to be passed to :class:`~elasticsearch_dsl.Search`
"""
self._query = query
self._filters = {}
self._sort = sort
if isinstance(sort, string_types):
self._sort = (sort,)
else:
self._sort = sort
self.filter_values = {}
for name, value in iteritems(filters):
self.add_filter(name, value)
Expand Down Expand Up @@ -316,7 +319,7 @@ def sort(self, search):
Add sorting information to the request.
"""
if self._sort:
search = search.sort(self._sort)
search = search.sort(*self._sort)
return search

def build_search(self):
Expand Down
33 changes: 33 additions & 0 deletions test_elasticsearch_dsl/test_faceted_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ def test_query_is_created_properly():
'highlight': {'fields': {'body': {}, 'title': {}}}
} == s.to_dict()

def test_query_is_created_properly_with_sort_tuple():
bs = BlogSearch('python search', sort=('category', '-title'))
s = bs.build_search()

assert s._doc_type == ['user', 'post']
assert {
'aggs': {
'_filter_tags': {
'filter': {
'match_all': {},
},
'aggs': {'tags': {'terms': {'field': 'tags'}}},
},
'_filter_category': {
'filter': {
'match_all': {},
},
'aggs': {'category': {'terms': {'field': 'category.raw'}}},
},
},
'query': {
'multi_match': {'fields': ('title^5', 'body'), 'query': 'python search'}
},
'highlight': {'fields': {'body': {}, 'title': {}}},
'sort': ['category', {'title': {'order': 'desc'}}]
} == s.to_dict()

def test_sort_string_backwards_compat():
bs_old = BlogSearch('python search', sort='-title').build_search().to_dict()
bs_new = BlogSearch('python search', sort=['-title']).build_search().to_dict()
assert bs_old == bs_new
assert [{'title': {'order': 'desc'}}] == bs_new['sort']

def test_filter_is_applied_to_search_but_not_relevant_facet():
bs = BlogSearch('python search', filters={'category': 'elastic'})
s = bs.build_search()
Expand Down

0 comments on commit 0e26599

Please sign in to comment.