Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Improve ingredient search relevance

Mikael Siidorowb7c6a077e1fc
diff --git a/CHANGELOG.md b/CHANGELOG.md
index df5fad621..6288f38e7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,8 @@ is switched off by default unless configured, see the docs for the setup.
## Bug fixes
+* Ingredient search now finds matching words in long ingredient names and ranks
+ more relevant results first
* Gated progressions (configs with `requirements`) now advance exactly one step
per qualifying workout instead of back-filling increments for skipped,
non-qualifying iterations
diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.py
index 6d6aa587b..2aa6eb10d 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,
+ TrigramWordSimilarity,
+)
# Third Party
from django_filters import rest_framework as filters
@@ -99,9 +102,12 @@ class IngredientFilterSet(filters.FilterSet):
# cursor.execute('SET LOCAL pg_trgm.similarity_threshold = 0.15')
return (
- queryset.filter(name__trigram_similar=value)
- .annotate(similarity=TrigramSimilarity('name', value))
- .order_by('-similarity', 'name')
+ queryset.filter(name__trigram_word_similar=value)
+ .annotate(
+ word_similarity=TrigramWordSimilarity(value, 'name'),
+ similarity=TrigramSimilarity('name', value),
+ )
+ .order_by('-word_similarity', '-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.py
index c4db1ac2a..af9b1fc10 100644
--- a/wger/nutrition/tests/test_search_api.py
+++ b/wger/nutrition/tests/test_search_api.py
@@ -14,8 +14,12 @@
# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.
# Standard Library
+from unittest import skipUnless
from unittest.mock import patch
+# Django
+from django.db import connection
+
# Third Party
from rest_framework import status
@@ -251,3 +255,107 @@ class SearchIngredientApiTestCase(BaseTestCase, ApiBaseTestCase):
mock_fetch.assert_called_once_with('0000000000000')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data['count'], 0)
+
+
+@skipUnless(connection.vendor == 'postgresql', 'PostgreSQL ingredient search only')
+class IngredientSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):
+ """Regression tests for ingredient search retrieval and ranking."""
+
+ url = '/api/v2/ingredient/'
+
+ corpus = (
+ 'Chicken',
+ 'Chicken, Boiled Without Salt',
+ "'Nduja Chicken, Mozzarella, Slow Roast Tomato Sandwich",
+ 'Chickpea Salad',
+ 'Lunch out (placeholder)',
+ 'Pasta Carbonara, Bacon',
+ '"Oat Milk Mocha" Oat Milk Latte Bar',
+ 'Milk',
+ 'Rice',
+ 'Rice Pudding',
+ 'Salty Liquorice',
+ 'Banana',
+ 'Banana Bread',
+ 'Salmon',
+ 'Smoked Salmon',
+ )
+
+ @classmethod
+ def setUpTestData(cls):
+ super().setUpTestData()
+ for name in cls.corpus:
+ Ingredient.objects.create(
+ name=name,
+ source_name='Test',
+ language_id=2,
+ energy=100,
+ protein=10,
+ carbohydrates=10,
+ fat=10,
+ )
+
+ def search_names(self, query):
+ response = self.client.get(
+ self.url,
+ {'name__search': query, 'language__code': 'en'},
+ )
+ self.assertEqual(response.status_code, status.HTTP_200_OK)
+ return [result['name'] for result in response.data['results']]
+
+ def assert_ranked_before(self, names, first, second):
+ self.assertIn(first, names)
+ self.assertIn(second, names)
+ self.assertLess(names.index(first), names.index(second))
+
+ def test_search_finds_words_in_long_ingredient_names(self):
+ cases = (
+ ('chicken', 'Chicken, Boiled Without Salt'),
+ ('chicken', "'Nduja Chicken, Mozzarella, Slow Roast Tomato Sandwich"),
+ ('lunch', 'Lunch out (placeholder)'),
+ ('pasta', 'Pasta Carbonara, Bacon'),
+ ('milk', '"Oat Milk Mocha" Oat Milk Latte Bar'),
+ ('rice', 'Rice Pudding'),
+ ('banana', 'Banana Bread'),
+ ('salmon', 'Smoked Salmon'),
+ )
+
+ for query, expected_name in cases:
+ with self.subTest(query=query, expected_name=expected_name):
+ names = self.search_names(query)
+ self.assertIn(expected_name, names)
+
+ def test_search_ranks_exact_names_first(self):
+ cases = ('Chicken', 'Milk', 'Rice', 'Banana', 'Salmon')
+
+ for expected_name in cases:
+ with self.subTest(expected_name=expected_name):
+ names = self.search_names(expected_name.lower())
+ self.assertEqual(names[:1], [expected_name])
+
+ def test_search_ranks_leading_matches_before_later_word_matches(self):
+ names = self.search_names('chicken')
+
+ self.assertEqual(names[:1], ['Chicken'])
+ self.assert_ranked_before(
+ names,
+ 'Chicken, Boiled Without Salt',
+ "'Nduja Chicken, Mozzarella, Slow Roast Tomato Sandwich",
+ )
+
+ def test_search_supports_partial_words(self):
+ names = self.search_names('chick')
+
+ self.assertIn('Chicken', names)
+ self.assertIn('Chickpea Salad', names)
+
+ def test_search_finds_a_correctly_spelled_name_from_a_typo(self):
+ names = self.search_names('chickn')
+
+ self.assertEqual(names[:1], ['Chicken'])
+
+ 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')