Skip to content

Commit 10a8322

Browse files
committed
[DOP-28706] Include external_id in location search index
1 parent 59b1ba1 commit 10a8322

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# SPDX-FileCopyrightText: 2024-2025 MTS PJSC
2+
# SPDX-License-Identifier: Apache-2.0
3+
"""Include external_id to location search index
4+
5+
Revision ID: 85592fce8fb0
6+
Revises: 17481d3b2466
7+
Create Date: 2025-09-16 11:54:45.571184
8+
9+
"""
10+
11+
import sqlalchemy as sa
12+
from alembic import op
13+
14+
# revision identifiers, used by Alembic.
15+
revision = "85592fce8fb0"
16+
down_revision = "52fb9d8765fd"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade() -> None:
22+
op.execute(sa.text("ALTER TABLE location DROP COLUMN search_vector"))
23+
op.execute(
24+
sa.text(
25+
"""
26+
ALTER TABLE location ADD COLUMN search_vector tsvector NOT NULL
27+
GENERATED ALWAYS AS (
28+
to_tsvector(
29+
'simple'::regconfig,
30+
type || ' ' ||
31+
name || ' ' || (translate(name, '/.', ' ')) || ' ' ||
32+
COALESCE(external_id, ''::text) || ' ' || (translate(COALESCE(external_id, ''::text), '/.', ' '))
33+
)
34+
) STORED
35+
""",
36+
),
37+
)
38+
op.create_index(
39+
op.f("ix__location__search_vector"),
40+
"location",
41+
["search_vector"],
42+
unique=False,
43+
postgresql_using="gin",
44+
)
45+
46+
47+
def downgrade() -> None:
48+
op.execute(sa.text("ALTER TABLE location DROP COLUMN search_vector"))
49+
op.execute(
50+
sa.text(
51+
"""
52+
ALTER TABLE location ADD COLUMN search_vector tsvector NOT NULL
53+
GENERATED ALWAYS AS (
54+
to_tsvector('simple'::regconfig, name || ' ' || (translate(name, '/.', ' ')) || ' ' || type)
55+
) STORED
56+
""",
57+
),
58+
)
59+
op.create_index(
60+
op.f("ix__location__search_vector"),
61+
"location",
62+
["search_vector"],
63+
unique=False,
64+
postgresql_using="gin",
65+
)

data_rentgen/db/models/location.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ class Location(Base):
5757
# so name like 'my.host.name' is converted to tsvector `'host':2 'name':3`,
5858
# which does not match a tsquery like 'my:* & host:* & name:*'.
5959
# Instead prefer 'simple' dictionary as it does not use stemming.
60-
"to_tsvector('simple', name || ' ' || (translate(name, '/.', ' ')) || ' ' || type)",
60+
"""
61+
to_tsvector(
62+
'simple'::regconfig,
63+
type || ' ' ||
64+
name || ' ' || (translate(name, '/.', ' ')) || ' ' ||
65+
COALESCE(external_id, ''::text) || ' ' || (translate(COALESCE(external_id, ''::text), '/.', ' '))
66+
)
67+
""",
6168
persisted=True,
6269
),
6370
nullable=False,

0 commit comments

Comments
 (0)