From 91bc2a0c03ba3d84d0eb41fe1073c3f94adc7858 Mon Sep 17 00:00:00 2001 From: Matti Lamppu Date: Tue, 10 Dec 2024 08:53:40 +0200 Subject: [PATCH] Change reservation unit text search to use AND instead of OR --- .../test_reservation_unit/test_text_search.py | 7 ++++++- .../api/graphql/types/reservation_unit/filtersets.py | 2 +- utils/db.py | 10 +++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/test_graphql_api/test_reservation_unit/test_text_search.py b/tests/test_graphql_api/test_reservation_unit/test_text_search.py index be2664f8c..b1dac1b73 100644 --- a/tests/test_graphql_api/test_reservation_unit/test_text_search.py +++ b/tests/test_graphql_api/test_reservation_unit/test_text_search.py @@ -211,7 +211,12 @@ class Params(NamedTuple): ), "match different grammatical case": Params( text_search="tila pukinmäessä", - reservation_unit_data=SearchableData(description="sijaitsee pukinmäen kirjaston vieressä"), + reservation_unit_data=SearchableData(description="tila sijaitsee pukinmäen kirjaston vieressä"), + ), + "not all search terms are found": Params( + text_search="tenniskenttä kannelmäki", + reservation_unit_data=SearchableData(description="Tämä on uusi tenniskenttä"), + has_results=False, ), }) ) diff --git a/tilavarauspalvelu/api/graphql/types/reservation_unit/filtersets.py b/tilavarauspalvelu/api/graphql/types/reservation_unit/filtersets.py index bde059e78..ec3c97658 100644 --- a/tilavarauspalvelu/api/graphql/types/reservation_unit/filtersets.py +++ b/tilavarauspalvelu/api/graphql/types/reservation_unit/filtersets.py @@ -141,7 +141,7 @@ class Meta: def get_text_search(self, qs: ReservationUnitQuerySet, name: str, value: str) -> QuerySet: language = get_text_search_language(self.request) - search = build_search(value) + search = build_search(value, separator="&") query = SearchQuery(value=search, config=language, search_type="raw") match language: # Do search mostly with full text search, but also search some columns with containment search. diff --git a/utils/db.py b/utils/db.py index 4dff366fa..b3e7da522 100644 --- a/utils/db.py +++ b/utils/db.py @@ -285,12 +285,16 @@ def text_search( return qs.annotate(ts_vector=vector, ts_rank=rank).filter(q) -def build_search(text: str) -> str: +def build_search(text: str, *, separator: Literal["|", "&", "<->"] = "|") -> str: """ Build raw postgres full text search query from text. Quote search terms and do prefix matching. - Match all search terms with the OR operator. + Match all search terms with the given operator: + | = or + & = and + <-> = Followed by + <3> = Followed by less than 3 "words" away (<1> same as <->) Replace single quotes and hyphens in words with spaces so they are treated as whitespace in the search, e.g. "Moe's" becomes "Moe s" and "3D-printer" becomes "3D printer". @@ -302,4 +306,4 @@ def build_search(text: str) -> str: if value: value = f"'{value}':*" search_terms.append(value) - return " | ".join(search_terms) + return f" {separator} ".join(search_terms)