Skip to content

Commit

Permalink
fix: case search still broken (#1803)
Browse files Browse the repository at this point in the history
  • Loading branch information
stolpeo committed Jul 15, 2024
1 parent 5064a27 commit 86725d9
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions backend/variants/models/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,21 @@ def find(self, search_terms, _keywords=None):
"""
objects = super().get_queryset().order_by("name")
terms = {}
sql_conditions = []
conditions = []
order_by = []

for i, term in enumerate(search_terms):
terms[f"term{i}_like"] = rf"%{term}%"
terms[f"term{i}_like"] = rf"{term}%"
terms[f"term{i}"] = term
sql_conditions.append(
rf"levenshtein(lower(c.name), lower(%(term{i})s)) < 4 "
rf"OR c.name ILIKE %(term{i}_like)s"
conditions.append(
rf"levenshtein(lower(name), lower(%(term{i})s)) < 3 "
rf"OR name ILIKE %(term{i}_like)s"
)
sql_query = f"SELECT * FROM variants_case AS c WHERE {' OR '.join(sql_conditions)}"
order_by.append(rf"levenshtein(lower(name), lower(%(term{i})s))")

conditions = " OR ".join(conditions)
order_by = ", ".join(order_by)
sql_query = f"SELECT * FROM variants_case WHERE {conditions} ORDER BY {order_by} LIMIT 10;"
return objects.raw(sql_query, terms)


Expand Down

0 comments on commit 86725d9

Please sign in to comment.