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

fix(HelpdeskSearch): Use synonyms correctly and add fuzzy back #1984

Merged
merged 2 commits into from
Sep 17, 2024
Merged
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
23 changes: 19 additions & 4 deletions helpdesk/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def get_stopwords():
return STOPWORDS + frappe.get_all("HD Stopword", {"enabled": True}, pluck="name")


@redis_cache(3600 * 24)
def get_synonym_words() -> list[str]:
return frappe.get_all("HD Synonym", ["name"], as_list=True) + frappe.get_all(
"HD Synonyms", ["name"], as_list=True
)


class Search:
unsafe_chars = re.compile(r"[\[\]{}<>+!-]")

Expand Down Expand Up @@ -119,7 +126,7 @@ def add_synonyms(self):
for word, synonym in frappe.get_all(
"HD Synonym", ["parent", "name"], as_list=True
):
self.redis.ft(self.index_name).synupdate(word, True, synonym)
self.redis.ft(self.index_name).synupdate(word, True, word, synonym)

def add_document(self, id, doc):
doc = frappe._dict(doc)
Expand Down Expand Up @@ -338,9 +345,17 @@ def search(query, only_articles=False):
search = HelpdeskSearch()
query = search.clean_query(query)
query_parts = query.split()
query = " ".join(
[f"{q}*" for q in query_parts if q not in get_stopwords()]
) # for stopwords to be ignored
for part in query_parts:
if part in get_synonym_words():
query += f" {part}"
continue
if part in get_stopwords():
continue
if len(part) > 3:
query += f" %{part}%"
else:
query += f" {part}*"

result = search.search(query, start=0, highlight=True)
groups = {}
for r in result.docs:
Expand Down
Loading