Self hosted FLOSS fitness/workout, nutrition and weight tracker
Preserve typo matches in ingredient search
Mikael Siidorow9ba3694c2f28
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.pyindex 2aa6eb10d..7b9ef7f1b 100644--- a/wger/nutrition/api/filtersets.py+++ b/wger/nutrition/api/filtersets.py@@ -17,9 +17,14 @@import logging# Django-from django.contrib.postgres.search import (- TrigramSimilarity,- TrigramWordSimilarity,+from django.contrib.postgres.search import TrigramSimilarity+from django.db.models import (+ Case,+ F,+ IntegerField,+ Q,+ Value,+ When,)# Third Party@@ -31,7 +36,10 @@ from wger.nutrition.models import (Ingredient,LogItem,)-from wger.utils.db import is_postgres_db+from wger.utils.db import (+ PostgresILikeContains,+ is_postgres_db,+)from wger.utils.language import load_language@@ -94,20 +102,20 @@ class IngredientFilterSet(filters.FilterSet):return barcode_qsif is_postgres_db():- # Note: this uses the default value for pg_trgm.similarity_threshold (0.3) which- # might be too strict (doesn't find "butter" from "buttr"). If this needs to be- # changed later, e.g.:-- # with connection.cursor() as cursor:- # cursor.execute('SET LOCAL pg_trgm.similarity_threshold = 0.15')-+ contains = PostgresILikeContains(F('name'), value)return (- queryset.filter(name__trigram_word_similar=value)+ queryset.filter(contains | Q(name__trigram_similar=value)).annotate(- word_similarity=TrigramWordSimilarity(value, 'name'),+ match_rank=Case(+ When(name__iexact=value, then=Value(3)),+ When(name__istartswith=value, then=Value(2)),+ When(name__icontains=value, then=Value(1)),+ default=Value(0),+ output_field=IntegerField(),+ ),similarity=TrigramSimilarity('name', value),)- .order_by('-word_similarity', '-similarity', 'name')+ .order_by('-match_rank', '-similarity', 'name'))else:# Explicit order_by('name') because the viewset strips Meta.ordering.diff --git a/wger/nutrition/tests/test_search_api.py b/wger/nutrition/tests/test_search_api.pyindex af9b1fc10..b1476825d 100644--- a/wger/nutrition/tests/test_search_api.py+++ b/wger/nutrition/tests/test_search_api.py@@ -37,12 +37,13 @@ class SearchIngredientApiTestCase(BaseTestCase, ApiBaseTestCase):Logged-out users are also allowed to use the search"""response = self.client.get(self.url + '?name__search=test&language__code=en')- result1 = response.data['results'][0]self.assertEqual(response.status_code, status.HTTP_200_OK)self.assertEqual(response.data['count'], 2)- self.assertEqual(result1['name'], 'Ingredient, test, 2, organic, raw')- self.assertEqual(result1['id'], 2)+ self.assertCountEqual(+ [(result['id'], result['name']) for result in response.data['results']],+ [(1, 'Test ingredient 1'), (2, 'Ingredient, test, 2, organic, raw')],+ )def test_basic_search_logged_in(self):"""@@ -50,12 +51,13 @@ class SearchIngredientApiTestCase(BaseTestCase, ApiBaseTestCase):"""self.authenticate('test')response = self.client.get(self.url + '?name__search=test&language__code=en')- result1 = response.data['results'][0]self.assertEqual(response.status_code, status.HTTP_200_OK)self.assertEqual(response.data['count'], 2)- self.assertEqual(result1['name'], 'Ingredient, test, 2, organic, raw')- self.assertEqual(result1['id'], 2)+ self.assertCountEqual(+ [(result['id'], result['name']) for result in response.data['results']],+ [(1, 'Test ingredient 1'), (2, 'Ingredient, test, 2, organic, raw')],+ )def test_search_language_code_en_no_results(self):"""@@ -354,6 +356,11 @@ class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):self.assertEqual(names[:1], ['Chicken'])+ def test_search_preserves_typo_matches_from_whole_name_similarity(self):+ names = self.search_names('chiken')++ self.assertEqual(names[:1], ['Chicken'])+def test_search_ranks_whole_words_above_inside_word_matches(self):names = self.search_names('rice')diff --git a/wger/utils/db.py b/wger/utils/db.pyindex c76898485..349abeaa5 100644--- a/wger/utils/db.py+++ b/wger/utils/db.py@@ -18,11 +18,23 @@ from functools import wraps# Djangofrom django.conf import settingsfrom django.db import models+from django.db.models.lookups import Contains# wgerfrom wger.utils.uuid import uuid7+class PostgresILikeContains(Contains):+ """Case-insensitive containment using PostgreSQL's native ILIKE operator.++ Django implements ``icontains`` as ``UPPER(column) LIKE UPPER(value)``, which+ cannot use a trigram index defined on the untransformed column.+ """++ def get_rhs_op(self, connection, rhs):+ return f'ILIKE {rhs}'++def is_postgres_db():return 'postgres' in settings.DATABASES['default']['ENGINE']