From 0736735ac44fe6aad293fdab53755d3303a2eff4 Mon Sep 17 00:00:00 2001 From: Mitchel Cabuloy Date: Wed, 19 Jul 2017 09:54:04 +0800 Subject: [PATCH] Fix search sorting logic in Postgres search backend --- wagtail/contrib/postgres_search/backend.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/wagtail/contrib/postgres_search/backend.py b/wagtail/contrib/postgres_search/backend.py index e0cdc52550..5887e488b5 100644 --- a/wagtail/contrib/postgres_search/backend.py +++ b/wagtail/contrib/postgres_search/backend.py @@ -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)