0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 09:33:54 +01:00

Fix search sorting logic in Postgres search backend

This commit is contained in:
Mitchel Cabuloy 2017-07-19 09:54:04 +08:00 committed by Bertrand Bordage
parent 6f5f38076a
commit 0736735ac4

View File

@ -246,11 +246,15 @@ class PostgresSearchQuery(BaseSearchQuery):
def search_in_index(self, queryset, search_query, start, stop):
index_entries = self.get_in_index_queryset(queryset, search_query)
order_sql = ''
values = ['typed_pk']
if self.order_by_relevance:
index_entries = index_entries.rank(search_query)
values.append('rank')
order_sql = 'ORDER BY index_entry.rank DESC'
index_sql, index_params = get_sql(
index_entries.annotate_typed_pk()
.values('typed_pk', 'rank')
.values(*values)
)
model_sql, model_params = get_sql(queryset)
model = queryset.model
@ -258,9 +262,9 @@ class PostgresSearchQuery(BaseSearchQuery):
SELECT obj.*
FROM (%s) AS index_entry
INNER JOIN (%s) AS obj ON obj."%s" = index_entry.typed_pk
ORDER BY index_entry.rank DESC
%s
OFFSET %%s LIMIT %%s;
""" % (index_sql, model_sql, get_pk_column(model))
""" % (index_sql, model_sql, get_pk_column(model), order_sql)
limits = (start, None if stop is None else stop - start)
return model._default_manager.using(get_db_alias(queryset)).raw(
sql, index_params + model_params + limits)