Self hosted FLOSS fitness/workout, nutrition and weight tracker
Refine ingredient search ranking
Mikael Siidorow15fbcc24f482
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.pyindex 18599f729..34d41fbe1 100644--- a/wger/nutrition/api/filtersets.py+++ b/wger/nutrition/api/filtersets.py@@ -17,7 +17,10 @@import logging# Django-from django.contrib.postgres.search import TrigramSimilarity+from django.contrib.postgres.search import (+ TrigramSimilarity,+ TrigramStrictWordSimilarity,+)from django.db.models import (Case,F,@@ -39,6 +42,7 @@ from wger.nutrition.models import (from wger.utils.db import (PostgresILikeContains,PostgresILikeExact,+ PostgresILikeStartsWith,is_postgres_db,)from wger.utils.language import load_language@@ -113,27 +117,39 @@ class IngredientFilterSet(filters.FilterSet):return barcode_qsif is_postgres_db():+ if not any(character.isalnum() for character in value):+ return queryset.none()+# A trigram index cannot accelerate unrestricted one- or two-character# substring patterns. Exact matching still supports valid short names.if len(value) < 3:exact = PostgresILikeExact(F('name'), value)return queryset.filter(exact).order_by('name')+ exact = PostgresILikeExact(F('name'), value)+ starts_with = PostgresILikeStartsWith(F('name'), value)+ contains = PostgresILikeContains(F('name'), value)+candidates = Q(name__trigram_similar=value)- if _has_literal_trigram(value):- candidates |= PostgresILikeContains(F('name'), value)+ # Whole-name similarity already retrieves specific multi-word queries;+ # the extra substring scan is useful for single terms in long names.+ if len(value.split(maxsplit=1)) == 1 and _has_literal_trigram(value):+ candidates |= containsreturn (queryset.filter(candidates)+ .annotate(+ word_similarity=TrigramStrictWordSimilarity(value, 'name'),+ similarity=TrigramSimilarity('name', value),+ ).annotate(match_rank=Case(- When(name__iexact=value, then=Value(3)),- When(name__istartswith=value, then=Value(2)),- When(name__icontains=value, then=Value(1)),+ When(exact, then=Value(3)),+ When(starts_with, then=Value(2)),+ When(word_similarity=1, then=Value(1)),default=Value(0),output_field=IntegerField(),),- similarity=TrigramSimilarity('name', value),).order_by('-match_rank', '-similarity', 'name'))diff --git a/wger/nutrition/tests/test_search_api.py b/wger/nutrition/tests/test_search_api.pyindex 94e6c7bd0..f32e7b44c 100644--- a/wger/nutrition/tests/test_search_api.py+++ b/wger/nutrition/tests/test_search_api.py@@ -278,10 +278,16 @@ class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):'Rice','Rice Pudding','Salty Liquorice',+ 'Brown rice, long grain, cooked without salt',+ 'Licorice','Banana','Banana Bread','Salmon','Smoked Salmon',+ 'İstanbul',+ 'Istanbul spice',+ 'Butter',+ 'Apple Buttrr Spread','餅','牛乳',)@@ -320,6 +326,7 @@ class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):def test_search_finds_words_in_long_ingredient_names(self):cases = (('chicken', 'Chicken, Boiled Without Salt'),+ ('chicken boiled', 'Chicken, Boiled Without Salt'),('chicken', "'Nduja Chicken, Mozzarella, Slow Roast Tomato Sandwich"),('lunch', 'Lunch out (placeholder)'),('pasta', 'Pasta Carbonara, Bacon'),@@ -364,10 +371,8 @@ class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):self.assertEqual(self.search_names('餅'), ['餅'])self.assertEqual(self.search_names('牛乳'), ['牛乳'])- def test_short_search_escapes_like_metacharacters(self):- for query in ('%', '_', '\\'):- with self.subTest(query=query):- self.assertEqual(self.search_names(query), [])+ def test_punctuation_only_search_returns_no_results(self):+ self.assertEqual(self.search_names('%'), [])def test_search_finds_a_correctly_spelled_name_from_a_typo(self):names = self.search_names('chickn')@@ -382,5 +387,18 @@ class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):def test_search_ranks_whole_words_above_inside_word_matches(self):names = self.search_names('rice')- self.assert_ranked_before(names, 'Rice', 'Salty Liquorice')- self.assert_ranked_before(names, 'Rice Pudding', 'Salty Liquorice')+ self.assert_ranked_before(+ names,+ 'Brown rice, long grain, cooked without salt',+ 'Licorice',+ )++ def test_search_ranks_unicode_case_insensitive_exact_matches_first(self):+ names = self.search_names('istanbul')++ self.assertEqual(names[:2], ['İstanbul', 'Istanbul spice'])++ def test_search_does_not_rank_generic_substrings_above_fuzzy_matches(self):+ names = self.search_names('buttr')++ self.assert_ranked_before(names, 'Butter', 'Apple Buttrr Spread')diff --git a/wger/utils/db.py b/wger/utils/db.pyindex 8fea834d8..d9d010230 100644--- a/wger/utils/db.py+++ b/wger/utils/db.py@@ -21,6 +21,7 @@ from django.db import modelsfrom django.db.models.lookups import (Contains,Exact,+ StartsWith,)# wger@@ -51,6 +52,13 @@ class PostgresILikeContains(Contains):return f'ILIKE {rhs}'+class PostgresILikeStartsWith(StartsWith):+ """Case-insensitive prefix matching using PostgreSQL's native ILIKE operator."""++ def get_rhs_op(self, connection, rhs):+ return f'ILIKE {rhs}'++def is_postgres_db():return 'postgres' in settings.DATABASES['default']['ENGINE']