From 621fcace851db1ab7bd00fe6b5e081e755ee3a0f Mon Sep 17 00:00:00 2001 From: Pelagic Date: Sat, 17 Aug 2024 17:28:52 +0300 Subject: [PATCH] Made the search filter have a calculated threshold with a lower threshold for small query strings and a higher threshold for large query strings. --- pyproject.toml | 2 +- starfields_drf_generics/filters.py | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d95a18f..9668b04 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "starfields-drf-generics" -version = "0.2.6" +version = "0.2.7" authors = [ { name="Anastasios Svolis", email="support@starfields.gr" }, ] diff --git a/starfields_drf_generics/filters.py b/starfields_drf_generics/filters.py index 0864133..f420515 100644 --- a/starfields_drf_generics/filters.py +++ b/starfields_drf_generics/filters.py @@ -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