mirror of
https://github.com/wagtail/wagtail.git
synced 2024-11-29 17:36:49 +01:00
Optimizes the PostgreSQL search query generated by the ORM.
Makes queries at least as fast as before.
This commit is contained in:
parent
77dfab5968
commit
781263d4e1
@ -198,20 +198,26 @@ class PostgresSearchQuery(BaseSearchQuery):
|
|||||||
if self.query_string is None:
|
if self.query_string is None:
|
||||||
return self.queryset[start:stop]
|
return self.queryset[start:stop]
|
||||||
search_query = self.get_search_query(config=config)
|
search_query = self.get_search_query(config=config)
|
||||||
|
queryset = self.queryset
|
||||||
|
query = queryset.query
|
||||||
if self.fields is None:
|
if self.fields is None:
|
||||||
search_annotation = F('index_entries__body_search')
|
vector = F('index_entries__body_search')
|
||||||
else:
|
else:
|
||||||
search_annotation = ADD(
|
vector = ADD(
|
||||||
SearchVector(field, config=search_query.config,
|
SearchVector(field, config=search_query.config,
|
||||||
weight=get_weight(self.get_boost(field)))
|
weight=get_weight(self.get_boost(field)))
|
||||||
for field in self.fields)
|
for field in self.fields)
|
||||||
queryset = (self.queryset.annotate(_search_=search_annotation)
|
vector = vector.resolve_expression(query)
|
||||||
.filter(_search_=search_query))
|
search_query = search_query.resolve_expression(query)
|
||||||
|
lookup = IndexEntry._meta.get_field('body_search').get_lookup('exact')(
|
||||||
|
vector, search_query)
|
||||||
|
query.where.add(lookup, 'AND')
|
||||||
if self.order_by_relevance:
|
if self.order_by_relevance:
|
||||||
queryset = queryset.annotate(
|
# Due to a Django bug, arrays are not automatically converted here.
|
||||||
_rank_=SearchRank(F('_search_'), search_query,
|
converted_weights = '{' + ','.join(map(str, WEIGHTS_VALUES)) + '}'
|
||||||
weights=WEIGHTS_VALUES)
|
queryset = queryset.order_by(SearchRank(vector, search_query,
|
||||||
).order_by('-_rank_', '-pk')
|
weights=converted_weights).desc(),
|
||||||
|
'-pk')
|
||||||
elif not queryset.query.order_by:
|
elif not queryset.query.order_by:
|
||||||
# Adds a default ordering to avoid issue #3729.
|
# Adds a default ordering to avoid issue #3729.
|
||||||
queryset = queryset.order_by('-pk')
|
queryset = queryset.order_by('-pk')
|
||||||
|
Loading…
Reference in New Issue
Block a user