Skip to content

Commit 2826db8

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 2826db8

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
dependencies = [
8+
("core", "0025_alter_user_short_name"),
9+
]
10+
11+
operations = [
12+
migrations.RunSQL(
13+
sql="""
14+
CREATE OR REPLACE FUNCTION public.immutable_unaccent(regdictionary, text)
15+
RETURNS text
16+
LANGUAGE c IMMUTABLE PARALLEL SAFE STRICT AS
17+
'$libdir/unaccent', 'unaccent_dict';
18+
19+
CREATE OR REPLACE FUNCTION public.f_unaccent(text)
20+
RETURNS text
21+
LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT
22+
RETURN public.immutable_unaccent(regdictionary 'public.unaccent', $1);
23+
24+
CREATE INDEX IF NOT EXISTS user_email_unaccent_trgm_idx
25+
ON impress_user
26+
USING gin (f_unaccent(email) gin_trgm_ops);
27+
28+
CREATE INDEX IF NOT EXISTS user_full_name_unaccent_trgm_idx
29+
ON impress_user
30+
USING gin (f_unaccent(full_name) gin_trgm_ops);
31+
""",
32+
reverse_sql="""
33+
DROP INDEX IF EXISTS user_email_unaccent_trgm_idx;
34+
DROP INDEX IF EXISTS user_full_name_unaccent_trgm_idx;
35+
""",
36+
),
37+
]

0 commit comments

Comments
 (0)