Files
silo-server/migrations/138_search_number_word_normalization.down.sql
T
Silo Server Migration eeb1caee81 feat(catalog): normalize number words in title search
- Map number words and ordinals (e.g. "Two"/"2nd") to digit tokens in both SQL normalize_search_text and the Go mirror so "Dune: Part Two" and "Dune Part 2" match
- Rebuild title_normalized generated column and title FTS GIN indexes (migration 138)
2026-05-24 01:14:39 -04:00

39 lines
1.4 KiB
PL/PgSQL

-- Restore the pre-138 normalization: collapse non-alnum to spaces, lowercase,
-- and strip standalone "and" tokens, but do not normalize number words or
-- ordinal digit suffixes.
DROP INDEX IF EXISTS public.idx_media_items_search_title_fields;
DROP INDEX IF EXISTS public.idx_media_items_title_normalized_trgm;
ALTER TABLE public.media_items DROP COLUMN IF EXISTS title_normalized;
CREATE OR REPLACE FUNCTION public.normalize_search_text(input text)
RETURNS text
LANGUAGE sql
IMMUTABLE
PARALLEL SAFE
AS $$
SELECT BTRIM(REGEXP_REPLACE(
' ' || BTRIM(LOWER(REGEXP_REPLACE(COALESCE(input, ''), '[^[:alnum:]]+', ' ', 'g'))) || ' ',
' (and )+',
' ',
'g'
));
$$;
ALTER TABLE public.media_items
ADD COLUMN title_normalized text
GENERATED ALWAYS AS (public.normalize_search_text(title)) STORED;
CREATE INDEX IF NOT EXISTS idx_media_items_title_normalized_trgm
ON public.media_items USING gin (title_normalized public.gin_trgm_ops);
CREATE INDEX IF NOT EXISTS idx_media_items_search_title_fields
ON public.media_items USING gin ((
setweight(to_tsvector('simple', public.normalize_search_text(COALESCE(title, ''))), 'A') ||
setweight(to_tsvector('simple', public.normalize_search_text(COALESCE(original_title, ''))), 'A') ||
setweight(to_tsvector('simple', public.normalize_search_text(COALESCE(sort_title, ''))), 'B')
));
DROP FUNCTION IF EXISTS public.normalize_search_number_token(text);