Self hosted FLOSS fitness/workout, nutrition and weight tracker
Improve exercise search relevance for long names
Roland Geidercb2fb3bcb14d
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6288f38e7..7dd97702f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -17,6 +17,9 @@ is switched off by default unless configured, see the docs for the setup.* Ingredient search now finds matching words in long ingredient names and ranksmore relevant results first+* Exercise search now finds matching words in long exercise names, e.g. "curl"+ finds "Alternating Biceps Curls With Dumbbell", and sorts the results by+ relevance* Gated progressions (configs with `requirements`) now advance exactly one stepper qualifying workout instead of back-filling increments for skipped,non-qualifying iterationsdiff --git a/wger/exercises/api/filtersets.py b/wger/exercises/api/filtersets.pyindex d0e649f3c..b6e0a217c 100644--- a/wger/exercises/api/filtersets.py+++ b/wger/exercises/api/filtersets.py@@ -15,15 +15,25 @@# Django+from django.contrib.postgres.search import (+ TrigramSimilarity,+ TrigramWordSimilarity,+)from django.db.models import (+ Case,Exists,+ FloatField,OuterRef,Q,QuerySet,+ Subquery,+ Value,+ When,)# Third Partyfrom django_filters import rest_framework as filters+from rest_framework.filters import OrderingFilter# wgerfrom wger.exercises.models import (@@ -34,6 +44,22 @@ from wger.utils.db import is_postgres_dbfrom wger.utils.language import load_language+class RelevanceOrderingFilter(OrderingFilter):+ """+ Keeps the relevance ordering of the name search.++ Without this the default ordering of the viewset replaces it. An ordering the+ client asks for explicitly still wins.+ """++ def get_ordering(self, request, queryset, view):+ searching = request.query_params.get('name__search')+ if searching and not request.query_params.get(self.ordering_param):+ return None++ return super().get_ordering(request, queryset, view)++class ExerciseFilterSet(filters.FilterSet):"""Filters for the regular exercises endpoints to support fulltext name search@@ -61,6 +87,34 @@ class ExerciseFilterSet(filters.FilterSet):translation_subquery = translation_subquery.filter(language__in=languages)return queryset.filter(Exists(translation_subquery)).distinct()+ def _order_by_relevance(self, queryset: QuerySet, value: str) -> QuerySet:+ """+ Sorts the exercises by their best matching translation.++ The name lives on the translation, so the scores come from a subquery. Exact+ and prefix matches score above the word similarity, which is at most 1, and+ shorter names win ties, e.g. "Leg Curl" over "Alternating Biceps Curls".+ """++ matches = Translation.objects.filter(exercise=OuterRef('pk'))+ if languages := self._languages_from_params():+ matches = matches.filter(language__in=languages)++ matches = matches.annotate(+ match_score=Case(+ When(name__iexact=value, then=Value(3.0)),+ When(name__istartswith=value, then=Value(2.0)),+ default=TrigramWordSimilarity(value, 'name'),+ output_field=FloatField(),+ ),+ name_similarity=TrigramSimilarity('name', value),+ ).order_by('-match_score', '-name_similarity')++ return queryset.annotate(+ match_score=Subquery(matches.values('match_score')[:1]),+ name_similarity=Subquery(matches.values('name_similarity')[:1]),+ ).order_by('-match_score', '-name_similarity')+def search_name_fulltext(self, queryset: QuerySet, name: str, value: str):"""Searches for exercise matches with the given name.@@ -75,12 +129,20 @@ class ExerciseFilterSet(filters.FilterSet):if not is_postgres_db():return self.search_name_exact(queryset, name, value)- # Note: this uses the default value for pg_trgm.similarity_threshold (0.3)- return self._filter_by_translation(+ # Whole-name similarity (pg_trgm.similarity_threshold, 0.3) keeps typos such as+ # "bech press" working, word similarity (pg_trgm.word_similarity_threshold, 0.6)+ # matches single words inside the long compound names most exercises have.+ queryset = self._filter_by_translation(queryset=queryset,- q_expr=Q(name__trigram_similar=value) | Q(alias__alias__icontains=value),+ q_expr=(+ Q(name__trigram_similar=value)+ | Q(name__trigram_word_similar=value)+ | Q(alias__alias__icontains=value)+ ),)+ return self._order_by_relevance(queryset, value)+def search_name_exact(self, queryset: QuerySet, name: str, value: str):"""Searches for exact exercise matches with the given namediff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex 22d58e724..6cc7ae8e6 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -25,6 +25,7 @@ from django.views.decorators.cache import cache_page# Third Partyfrom actstream import action as actstream_action+from django_filters.rest_framework import DjangoFilterBackendfrom drf_spectacular.utils import extend_schemafrom easy_thumbnails.alias import aliasesfrom easy_thumbnails.files import get_thumbnailer@@ -36,7 +37,10 @@ from rest_framework.response import Responsefrom rest_framework.viewsets import ModelViewSet# wger-from wger.exercises.api.filtersets import ExerciseFilterSet+from wger.exercises.api.filtersets import (+ ExerciseFilterSet,+ RelevanceOrderingFilter,+)from wger.exercises.api.permissions import CanContributeExercisesfrom wger.exercises.api.serializers import (DeletionLogSerializer,@@ -226,6 +230,7 @@ class ExerciseInfoViewset(viewsets.ReadOnlyModelViewSet):ordering_fields = '__all__'ordering = ['id']filterset_class = ExerciseFilterSet+ filter_backends = (DjangoFilterBackend, RelevanceOrderingFilter)def get_queryset(self):"""diff --git a/wger/exercises/tests/api/test_exercise_info_search_api.py b/wger/exercises/tests/api/test_exercise_info_search_api.pyindex d77990fd9..cd7e48dd9 100644--- a/wger/exercises/tests/api/test_exercise_info_search_api.py+++ b/wger/exercises/tests/api/test_exercise_info_search_api.py@@ -13,7 +13,11 @@# You should have received a copy of the GNU Affero General Public License# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+# Standard Library+from unittest import skipUnless+# Django+from django.db import connectionfrom django.urls import reverse# Third Party@@ -22,6 +26,11 @@ from rest_framework import status# wgerfrom wger.core.tests.api_base_test import ApiBaseTestCasefrom wger.core.tests.base_testcase import BaseTestCase+from wger.exercises.models import (+ Exercise,+ ExerciseCategory,+ Translation,+)class ExerciseInfoFilterApiTestCase(BaseTestCase, ApiBaseTestCase):@@ -150,3 +159,97 @@ class ExerciseInfoFilterApiTestCase(BaseTestCase, ApiBaseTestCase):self.assertEqual(response.status_code, status.HTTP_200_OK)self.assertEqual(len(results), 1)+++@skipUnless(connection.vendor == 'postgresql', 'PostgreSQL exercise search only')+class ExerciseInfoSearchRankingApiTestCase(BaseTestCase, ApiBaseTestCase):+ """Retrieval and ranking of the exercise name search"""++ corpus = (+ 'Curl',+ 'Leg Curl',+ 'Curl With Kettlebell',+ 'Alternating Biceps Curls With Dumbbell',+ 'Barbell Reverse Wrist Curl',+ 'Rowing Machine',+ 'Bent Over Rowing',+ 'Butterfly Narrow Grip',+ 'Bench Press',+ 'Decline Bench Press Barbell',+ )++ @classmethod+ def setUpTestData(cls):+ super().setUpTestData()++ category = ExerciseCategory.objects.first()+ for name in cls.corpus:+ exercise = Exercise.objects.create(+ category=category,+ license_id=2,+ license_author='test',+ )+ Translation.objects.create(+ exercise=exercise,+ language_id=2,+ name=name,+ description='A description for the search test corpus.',+ )++ def search_names(self, query, **params):+ response = self.client.get(+ reverse('exerciseinfo-list'),+ {'name__search': query, 'language__code': 'en', **params},+ )+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ names = []+ for item in response.data['results']:+ names += [+ translation['name']+ for translation in item['translations']+ if translation['name'] in self.corpus+ ]+ return names++ 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_exercise_names(self):+ names = self.search_names('curl')++ for expected_name in (+ 'Alternating Biceps Curls With Dumbbell',+ 'Barbell Reverse Wrist Curl',+ 'Leg Curl',+ ):+ with self.subTest(expected_name=expected_name):+ self.assertIn(expected_name, names)++ def test_search_ranks_exact_names_first(self):+ self.assertEqual(self.search_names('curl')[:1], ['Curl'])+ self.assertEqual(self.search_names('bench press')[:1], ['Bench Press'])++ def test_search_ranks_shorter_names_before_longer_ones(self):+ names = self.search_names('curl')++ self.assert_ranked_before(names, 'Leg Curl', 'Alternating Biceps Curls With Dumbbell')++ def test_search_matches_word_beginnings(self):+ names = self.search_names('row')++ self.assertIn('Rowing Machine', names)+ self.assertIn('Bent Over Rowing', names)++ def test_search_ignores_matches_inside_a_word(self):+ self.assertNotIn('Butterfly Narrow Grip', self.search_names('row'))++ def test_search_finds_a_correctly_spelled_name_from_a_typo(self):+ self.assertIn('Bench Press', self.search_names('bech press'))++ def test_search_keeps_an_explicitly_requested_ordering(self):+ names = self.search_names('curl', ordering='-id')++ self.assertEqual(names[:1], ['Barbell Reverse Wrist Curl'])