Self hosted FLOSS fitness/workout, nutrition and weight tracker
Improve indices used for the ingredient full text search
Roland Geider00d7a5df5c60
diff --git a/wger/exercises/api/filtersets.py b/wger/exercises/api/filtersets.pyindex aefab8763..bea22ebd6 100644--- a/wger/exercises/api/filtersets.py+++ b/wger/exercises/api/filtersets.py@@ -15,7 +15,6 @@# Django-from django.contrib.postgres.search import TrigramSimilarityfrom django.db.models import (Exists,OuterRef,@@ -53,12 +52,13 @@ class ExerciseFilterSet(filters.FilterSet):languages = [load_language(code) for code in set(languages_param.split(','))]if is_postgres_db():+ # Note: this uses the default value for pg_trgm.similarity_threshold (0.3)translation_subquery = Translation.objects.filter(exercise=OuterRef('pk'))if languages:translation_subquery = translation_subquery.filter(language__in=languages)- translation_subquery = translation_subquery.annotate(- similarity=TrigramSimilarity('name', value)- ).filter(Q(similarity__gt=0.15) | Q(alias__alias__icontains=value))+ translation_subquery = translation_subquery.filter(+ Q(name__trigram_similar=value) | Q(alias__alias__icontains=value)+ )qs = queryset.filter(Exists(translation_subquery))else:diff --git a/wger/exercises/migrations/0039_translation_alias_trigram_gin_index.py b/wger/exercises/migrations/0039_translation_alias_trigram_gin_index.pynew file mode 100644index 000000000..bf9d27298--- /dev/null+++ b/wger/exercises/migrations/0039_translation_alias_trigram_gin_index.py@@ -0,0 +1,44 @@+"""Replace plain GIN indexes on translation name and alias with trigram GIN indexes.++The existing GIN indexes do not use gin_trgm_ops, so they cannot accelerate+the % operator used by trigram_similar lookups. This migration replaces them+with proper trigram indexes.+"""++import django.contrib.postgres.indexes+from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ ('exercises', '0038_sync_model_changes'),+ ]++ operations = [+ # Translation.name+ migrations.RemoveIndex(+ model_name='translation',+ name='exercises_e_name_ac11f4_gin',+ ),+ migrations.AddIndex(+ model_name='translation',+ index=django.contrib.postgres.indexes.GinIndex(+ fields=['name'],+ name='idx_translation_name_trgm',+ opclasses=['gin_trgm_ops'],+ ),+ ),+ # Alias.alias+ migrations.RemoveIndex(+ model_name='alias',+ name='exercises_a_alias_227e38_gin',+ ),+ migrations.AddIndex(+ model_name='alias',+ index=django.contrib.postgres.indexes.GinIndex(+ fields=['alias'],+ name='idx_alias_alias_trgm',+ opclasses=['gin_trgm_ops'],+ ),+ ),+ ]diff --git a/wger/exercises/models/exercise_alias.py b/wger/exercises/models/exercise_alias.pyindex 93f2827a2..a1d9f2573 100644--- a/wger/exercises/models/exercise_alias.py+++ b/wger/exercises/models/exercise_alias.py@@ -59,7 +59,13 @@ class Alias(models.Model):"""Edit history"""class Meta:- indexes = (GinIndex(fields=['alias']),)+ indexes = (+ GinIndex(+ fields=['alias'],+ name='idx_alias_alias_trgm',+ opclasses=['gin_trgm_ops'],+ ),+ )def __str__(self):"""diff --git a/wger/exercises/models/translation.py b/wger/exercises/models/translation.pyindex 8b59d7eda..f6b3f80ff 100644--- a/wger/exercises/models/translation.py+++ b/wger/exercises/models/translation.py@@ -113,7 +113,13 @@ class Translation(AbstractLicenseModel, AbstractHistoryMixin, models.Model):ordering = ['name',]- indexes = (GinIndex(fields=['name'], name='exercises_e_name_ac11f4_gin'),)+ indexes = (+ GinIndex(+ fields=['name'],+ name='idx_translation_name_trgm',+ opclasses=['gin_trgm_ops'],+ ),+ )constraints = [models.UniqueConstraint(fields=['exercise', 'language'],diff --git a/wger/nutrition/api/filtersets.py b/wger/nutrition/api/filtersets.pyindex 0962fffb6..0c8d18438 100644--- a/wger/nutrition/api/filtersets.py+++ b/wger/nutrition/api/filtersets.py@@ -74,9 +74,16 @@ class IngredientFilterSet(filters.FilterSet):"""if 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')+return (- queryset.annotate(similarity=TrigramSimilarity('name', value))- .filter(similarity__gt=0.15)+ queryset.filter(name__trigram_similar=value)+ .annotate(similarity=TrigramSimilarity('name', value)).order_by('-similarity', 'name'))else:diff --git a/wger/nutrition/migrations/0034_ingredient_trigram_gin_index.py b/wger/nutrition/migrations/0034_ingredient_trigram_gin_index.pynew file mode 100644index 000000000..850145e62--- /dev/null+++ b/wger/nutrition/migrations/0034_ingredient_trigram_gin_index.py@@ -0,0 +1,31 @@+"""Replace plain GIN index on ingredient name with trigram GIN index.++The existing GIN index (nutrition_i_search__f274b7_gin) does not use+gin_trgm_ops, so it cannot accelerate SIMILARITY() / TrigramSimilarity+queries. This migration drops it and creates a proper trigram index.+"""++import django.contrib.postgres.indexes+from django.db import migrations+++class Migration(migrations.Migration):+ dependencies = [+ ('nutrition', '0033_finalize_weight_unit_merge'),+ ('exercises', '0029_full_text_search'),+ ]++ operations = [+ migrations.RemoveIndex(+ model_name='ingredient',+ name='nutrition_i_search__f274b7_gin',+ ),+ migrations.AddIndex(+ model_name='ingredient',+ index=django.contrib.postgres.indexes.GinIndex(+ fields=['name'],+ name='idx_ingredient_name_trgm',+ opclasses=['gin_trgm_ops'],+ ),+ ),+ ]diff --git a/wger/nutrition/models/ingredient.py b/wger/nutrition/models/ingredient.pyindex 709389142..578912841 100644--- a/wger/nutrition/models/ingredient.py+++ b/wger/nutrition/models/ingredient.py@@ -73,7 +73,8 @@ class Ingredient(AbstractLicenseModel, models.Model):indexes = (GinIndex(fields=['name'],- name='nutrition_i_search__f274b7_gin',+ name='idx_ingredient_name_trgm',+ opclasses=['gin_trgm_ops'],),models.Index(fields=['last_update'],