0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-30 01:46:24 +01:00

Implemented search ordering in Elasticsearch

This commit is contained in:
Karl Hobley 2015-10-12 12:28:03 +01:00
parent 8cd55cf8f2
commit c926ee32b3
2 changed files with 42 additions and 2 deletions

View File

@ -318,6 +318,37 @@ class ElasticSearchQuery(BaseSearchQuery):
else:
return inner_query
def get_sort(self):
# Ordering by relevance is the default in Elasticsearch
if self.order_by_relevance:
return
# Get queryset and make sure its ordered
if self.queryset.ordered:
order_by_fields = self.queryset.query.order_by
sort = []
for order_by_field in order_by_fields:
reverse = False
field_name = order_by_field
if order_by_field.startswith('-'):
reverse = True
field_name = order_by_field[1:]
field = self._get_filterable_field(field_name)
field_index_name = field.get_index_name(self.queryset.model)
sort.append({
field_index_name: 'desc' if reverse else 'asc'
})
return sort
else:
# Order by pk field
return ['pk']
def __repr__(self):
return json.dumps(self.get_query())
@ -341,14 +372,22 @@ class ElasticSearchResults(BaseSearchResults):
),
RemovedInWagtail14Warning, stacklevel=2)
return {
body = {
'query': self.query.to_es(),
}
else:
return {
body = {
'query': self.query.get_query()
}
if not for_count:
sort = self.query.get_sort()
if sort is not None:
body['sort'] = sort
return body
def _do_search(self):
# Params for elasticsearch query
params = dict(

View File

@ -383,6 +383,7 @@ class TestElasticSearchResults(TestCase):
query = mock.MagicMock()
query.queryset = models.SearchTest.objects.all()
query.get_query.return_value = 'QUERY'
query.get_sort.return_value = None
return self.ElasticSearchResults(backend, query)
def construct_search_response(self, results):