Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change reservation unit text search to use AND instead of OR #1459

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading