Made the search filter have a calculated threshold with a lower threshold for small query strings and a higher threshold for large query strings.
All checks were successful
StarFields Django Rest Framework Generics / build (push) Successful in 12s

This commit is contained in:
2024-08-17 17:28:52 +03:00
parent 7e4596e5b1
commit 621fcace85
2 changed files with 12 additions and 2 deletions

View File

@@ -14,6 +14,15 @@ from django.db.models.functions import Concat
# TODO the dev pages are not done
def calculate_threshold(query, min_threshold, max_threshold):
query_threshold = len(query)/300
if query_threshold < min_threshold:
return min_threshold
if max_threshold < query_threshold:
return max_threshold
return query_threshold
def search_smart_split(search_terms):
"""generator that first splits string by spaces, leaving quoted phrases together,
then it splits non-quoted phrases by commas.
@@ -516,6 +525,7 @@ class TrigramSearchFilter(BaseFilterBackend):
try:
# Attempt postgresql's full text search
from django.contrib.postgres.search import TrigramStrictWordSimilarity
threshold = calculate_threshold(query, 0.02, 0.12)
queryset = queryset.annotate(
search_field=Concat(
*search_fields,
@@ -523,7 +533,7 @@ class TrigramSearchFilter(BaseFilterBackend):
)).annotate(
similarity=TrigramStrictWordSimilarity(
'search_field', query)
).filter(similarity__gt=0.05)
).filter(similarity__gt=threshold)
except ImportError:
# Perform very simple sqlite compatible search