Skip to content

Commit

Permalink
Change reservation unit text search to use AND instead of OR
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Dec 10, 2024
1 parent 87cd188 commit a7909d9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
})
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand All @@ -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)

0 comments on commit a7909d9

Please sign in to comment.