Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Bound short ingredient searches

Mikael Siidorow26f80bd8c1de
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.py
index 7b9ef7f1b..18599f729 100644
--- a/wger/nutrition/api/filtersets.py
+++ b/wger/nutrition/api/filtersets.py
@@ -38,6 +38,7 @@ from wger.nutrition.models import (
)
from wger.utils.db import (
PostgresILikeContains,
+ PostgresILikeExact,
is_postgres_db,
)
from wger.utils.language import load_language
@@ -46,6 +47,16 @@ from wger.utils.language import load_language
logger = logging.getLogger(__name__)
+def _has_literal_trigram(value: str) -> bool:
+ """Return whether a substring lookup has a guaranteed indexable trigram."""
+ consecutive = 0
+ for character in value:
+ consecutive = consecutive + 1 if character.isalnum() else 0
+ if consecutive == 3:
+ return True
+ return False
+
+
class LogItemFilterSet(filters.FilterSet):
class Meta:
model = LogItem
@@ -102,9 +113,18 @@ class IngredientFilterSet(filters.FilterSet):
return barcode_qs
if is_postgres_db():
- contains = PostgresILikeContains(F('name'), value)
+ # 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')
+
+ candidates = Q(name__trigram_similar=value)
+ if _has_literal_trigram(value):
+ candidates |= PostgresILikeContains(F('name'), value)
+
return (
- queryset.filter(contains | Q(name__trigram_similar=value))
+ queryset.filter(candidates)
.annotate(
match_rank=Case(
When(name__iexact=value, then=Value(3)),
diff --git a/wger/nutrition/tests/test_ingredient.py b/wger/nutrition/tests/test_ingredient.py
index a3446aba2..1190df4bd 100644
--- a/wger/nutrition/tests/test_ingredient.py
+++ b/wger/nutrition/tests/test_ingredient.py
@@ -281,7 +281,9 @@ class IngredientSearchTestCase(WgerTestCase):
self.assertEqual(result['count'], 2)
- ingredient_1 = result['results'][0]
+ ingredients = {ingredient['id']: ingredient for ingredient in result['results']}
+
+ ingredient_1 = ingredients[2]
self.assertEqual(ingredient_1['id'], 2)
self.assertEqual(ingredient_1['name'], 'Ingredient, test, 2, organic, raw')
self.assertEqual(ingredient_1['uuid'], '44dc5966-73a2-4df7-8b15-f6d37a8990d9')
@@ -289,7 +291,7 @@ class IngredientSearchTestCase(WgerTestCase):
self.assertEqual(ingredient_1['image'], None)
self.assertEqual(ingredient_1['thumbnails'], None)
- ingredient_2 = result['results'][1]
+ ingredient_2 = ingredients[1]
self.assertEqual(ingredient_2['id'], 1)
self.assertEqual(ingredient_2['name'], 'Test ingredient 1')
self.assertEqual(ingredient_2['uuid'], '7908c204-907f-4b1e-ad4e-f482e9769ade')
diff --git a/wger/nutrition/tests/test_search_api.py b/wger/nutrition/tests/test_search_api.py
index b1476825d..94e6c7bd0 100644
--- a/wger/nutrition/tests/test_search_api.py
+++ b/wger/nutrition/tests/test_search_api.py
@@ -26,6 +26,7 @@ from rest_framework import status
# wger
from wger.core.tests.api_base_test import ApiBaseTestCase
from wger.core.tests.base_testcase import BaseTestCase
+from wger.nutrition.api.filtersets import _has_literal_trigram
from wger.nutrition.models import Ingredient
@@ -281,6 +282,8 @@ class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):
'Banana Bread',
'Salmon',
'Smoked Salmon',
+ '餅',
+ '牛乳',
)
@classmethod
@@ -310,6 +313,10 @@ class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):
self.assertIn(second, names)
self.assertLess(names.index(first), names.index(second))
+ def test_substring_search_requires_an_indexable_trigram(self):
+ self.assertFalse(_has_literal_trigram('q--'))
+ self.assertTrue(_has_literal_trigram('jäätelö'))
+
def test_search_finds_words_in_long_ingredient_names(self):
cases = (
('chicken', 'Chicken, Boiled Without Salt'),
@@ -351,6 +358,17 @@ class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):
self.assertIn('Chicken', names)
self.assertIn('Chickpea Salad', names)
+ def test_short_search_only_returns_exact_names(self):
+ self.assertEqual(self.search_names('c'), [])
+ self.assertEqual(self.search_names('ch'), [])
+ 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_search_finds_a_correctly_spelled_name_from_a_typo(self):
names = self.search_names('chickn')
diff --git a/wger/utils/db.py b/wger/utils/db.py
index 349abeaa5..8fea834d8 100644
--- a/wger/utils/db.py
+++ b/wger/utils/db.py
@@ -18,12 +18,28 @@ from functools import wraps
# Django
from django.conf import settings
from django.db import models
-from django.db.models.lookups import Contains
+from django.db.models.lookups import (
+ Contains,
+ Exact,
+)
# wger
from wger.utils.uuid import uuid7
+class PostgresILikeExact(Exact):
+ """Case-insensitive equality that can use a plain trigram index."""
+
+ def process_rhs(self, compiler, connection):
+ rhs, params = super().process_rhs(compiler, connection)
+ if params:
+ params = (connection.ops.prep_for_like_query(params[0]), *params[1:])
+ return rhs, params
+
+ def get_rhs_op(self, connection, rhs):
+ return f'ILIKE {rhs}'
+
+
class PostgresILikeContains(Contains):
"""Case-insensitive containment using PostgreSQL's native ILIKE operator.