50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
|
|
"""full-text relevance search on video titles
|
||
|
|
|
||
|
|
Revision ID: 0031_title_fts
|
||
|
|
Revises: 0030_search_finds
|
||
|
|
Create Date: 2026-06-30
|
||
|
|
|
||
|
|
Adds a PostgreSQL full-text search index over video titles so the feed search box can rank by
|
||
|
|
relevance (a YouTube-like "fuzzy" search: word-order-independent, multi-word AND, prefix on the
|
||
|
|
word being typed) instead of a whole-phrase substring match.
|
||
|
|
|
||
|
|
A custom text-search configuration `unaccent_simple` = the `simple` config (no stemming, no
|
||
|
|
stopwords — right for a multilingual HU/EN/DE catalog) plus the `unaccent` dictionary, so the
|
||
|
|
search stays accent-insensitive ("tiesto" matches "Tiësto"). A GIN expression index on
|
||
|
|
`to_tsvector('public.unaccent_simple', coalesce(title,''))` makes the @@ match fast; the feed
|
||
|
|
query uses the exact same expression so the planner picks the index. `unaccent` is already
|
||
|
|
enabled (migration 0029). The config-based 2-arg to_tsvector is IMMUTABLE, so it's index-safe.
|
||
|
|
"""
|
||
|
|
from typing import Sequence, Union
|
||
|
|
|
||
|
|
from alembic import op
|
||
|
|
|
||
|
|
revision: str = "0031_title_fts"
|
||
|
|
down_revision: Union[str, None] = "0030_search_finds"
|
||
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
||
|
|
depends_on: Union[str, Sequence[str], None] = None
|
||
|
|
|
||
|
|
|
||
|
|
def upgrade() -> None:
|
||
|
|
op.execute(
|
||
|
|
"CREATE TEXT SEARCH CONFIGURATION public.unaccent_simple (COPY = pg_catalog.simple)"
|
||
|
|
)
|
||
|
|
op.execute(
|
||
|
|
"""
|
||
|
|
ALTER TEXT SEARCH CONFIGURATION public.unaccent_simple
|
||
|
|
ALTER MAPPING FOR asciiword, asciihword, hword_asciipart, word, hword, hword_part
|
||
|
|
WITH unaccent, simple
|
||
|
|
"""
|
||
|
|
)
|
||
|
|
op.execute(
|
||
|
|
"""
|
||
|
|
CREATE INDEX ix_videos_title_fts ON videos
|
||
|
|
USING gin (to_tsvector('public.unaccent_simple', coalesce(title, '')))
|
||
|
|
"""
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def downgrade() -> None:
|
||
|
|
op.execute("DROP INDEX IF EXISTS ix_videos_title_fts")
|
||
|
|
op.execute("DROP TEXT SEARCH CONFIGURATION IF EXISTS public.unaccent_simple")
|