From d3912d3b6616d4957a6f4704e852ee8603821ac9 Mon Sep 17 00:00:00 2001 From: Reto Tschuppert Date: Tue, 24 Dec 2024 07:49:25 +0100 Subject: [PATCH] Adjust search --- src/onegov/org/models/search.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/onegov/org/models/search.py b/src/onegov/org/models/search.py index 1c0f5a8b1b..bed22f88c9 100644 --- a/src/onegov/org/models/search.py +++ b/src/onegov/org/models/search.py @@ -298,30 +298,22 @@ def generic_search(self) -> list['Searchable']: func.unaccent(self.query)) for model in self.search_models: - query = self.request.session.query(model) + query = self.request.session.query( + model, + func.ts_rank_cd(model.fts_idx, ts_query, 0).label('rank')) doc_count += query.count() query = self.filter_user_level(model, query) + query = query.filter(model.fts_idx.op('@@')(ts_query)) if self.request.session.query(query.exists()).scalar(): - weighted = ( - self._create_weighted_vector(model, language)) - rank_expression = func.coalesce( - func.ts_rank( - weighted, - ts_query, - 0 # normalization, ignore document length - ), 0).label('rank') - query = (query.filter(model.fts_idx.op('@@')(ts_query)) - .add_columns(rank_expression)) results.extend(query) - # sort by rank - results.sort(key=lambda x: x[1], reverse=True) + results.sort(key=lambda x: x.rank, reverse=True) self.number_of_docs = doc_count self.number_of_results = len(results) - # return results without rank column + # only return the model instances return [r[0] for r in results] def hashtag_search(self) -> list['Searchable']: