Skip to content

Commit a98425a

Browse files
committed
🗃️(backend) add an index on user names and emails
This adds an unaccented gin index on users full_name an email fields. Using a manual migration because we need to make a wrapper around unaccent to make it IMMUTABLE (cf. https://stackoverflow.com/questions/9063402/ )
1 parent 565a9df commit a98425a

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/backend/core/api/viewsets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ def get_queryset(self):
210210

211211
return (
212212
queryset.annotate(
213-
sim_email=TrigramSimilarity(Unaccent("email"), query),
214-
sim_name=TrigramSimilarity(Unaccent("full_name"), query),
213+
sim_email=TrigramSimilarity("email", query),
214+
sim_name=TrigramSimilarity("full_name", query),
215215
)
216216
.annotate(similarity=Greatest("sim_email", "sim_name"))
217217
.filter(similarity__gt=0.2)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 5.2.8 on 2025-11-20 09:56
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("core", "0025_alter_user_short_name"),
10+
]
11+
12+
operations = [
13+
migrations.RunSQL(
14+
sql="""
15+
CREATE OR REPLACE FUNCTION public.immutable_unaccent(regdictionary, text)
16+
RETURNS text
17+
LANGUAGE c IMMUTABLE PARALLEL SAFE STRICT AS
18+
'$libdir/unaccent', 'unaccent_dict';
19+
20+
CREATE OR REPLACE FUNCTION public.f_unaccent(text)
21+
RETURNS text
22+
LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT
23+
RETURN public.immutable_unaccent(regdictionary 'public.unaccent', $1);
24+
25+
CREATE INDEX IF NOT EXISTS user_email_unaccent_trgm_idx
26+
ON impress_user
27+
USING gin (f_unaccent(email) gin_trgm_ops);
28+
29+
CREATE INDEX IF NOT EXISTS user_full_name_unaccent_trgm_idx
30+
ON impress_user
31+
USING gin (f_unaccent(full_name) gin_trgm_ops);
32+
""",
33+
reverse_sql="""
34+
DROP INDEX IF EXISTS user_email_unaccent_trgm_idx;
35+
DROP INDEX IF EXISTS user_full_name_unaccent_trgm_idx;
36+
""",
37+
),
38+
]

0 commit comments

Comments
 (0)