From 6e7f4ae0d854fba437ee1f0b4ff898118611f84c Mon Sep 17 00:00:00 2001 From: Alessio Siniscalchi Date: Mon, 1 Jul 2024 10:09:19 +0200 Subject: [PATCH] update ranking on searches --- cads_catalogue_api_service/search_utils.py | 6 ++++-- tests/test_10_sorting_paging.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cads_catalogue_api_service/search_utils.py b/cads_catalogue_api_service/search_utils.py index c2e6436..b30a61f 100644 --- a/cads_catalogue_api_service/search_utils.py +++ b/cads_catalogue_api_service/search_utils.py @@ -24,7 +24,7 @@ WEIGHT_HIGH_PRIORITY_TERMS = 1.0 WEIGHT_TITLE = 0.8 WEIGHT_DESCRIPTION = 0.5 -WEIGHT_FULLTEXT = 0.3 +WEIGHT_FULLTEXT = 0.03 def split_by_category(keywords: list) -> list: @@ -60,7 +60,7 @@ def generate_ts_query(q: str = ""): def fulltext_order_by(q: str): """Generate the full text search order by clause.""" tsquery = generate_ts_query(q) - return sa.func.ts_rank( + return sa.func.ts_rank2( "{%s,%s,%s,%s}" % ( # NOTE: order of weights follows {D,C,B,A} labelling of 'search_field' of table resources @@ -70,7 +70,9 @@ def fulltext_order_by(q: str): WEIGHT_TITLE, ), cads_catalogue.database.Resource.search_field, + cads_catalogue.database.Resource.fts, tsquery, + sa.func.coalesce(cads_catalogue.database.Resource.popularity, 1), ).desc() diff --git a/tests/test_10_sorting_paging.py b/tests/test_10_sorting_paging.py index 75a19d4..3027081 100644 --- a/tests/test_10_sorting_paging.py +++ b/tests/test_10_sorting_paging.py @@ -52,7 +52,7 @@ def test_apply_sorting() -> None: assert search.limit == 10 assert search.offset == 0 - assert search.order_by.element.name == "ts_rank" + assert search.order_by.element.name == "ts_rank2" def test_get_next_prev_links() -> None: