From 6288969c4d29377fea6e095bf0f4d59146ff1559 Mon Sep 17 00:00:00 2001 From: aleidk Date: Fri, 7 Feb 2025 19:13:44 -0300 Subject: [PATCH] feat: add full text search to entries --- src/app/index.sql | 9 ++++--- ...50207213321_add_text_search_to_entries.sql | 27 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 src/db/migrations/20250207213321_add_text_search_to_entries.sql diff --git a/src/app/index.sql b/src/app/index.sql index 0d2c9e8..0a592e5 100644 --- a/src/app/index.sql +++ b/src/app/index.sql @@ -4,7 +4,6 @@ SELECT SELECT 'form' AS component, - 'entries/search.sql' AS "action", 'Entries' AS title, '' AS validate, 'GET' AS method; @@ -13,6 +12,7 @@ SELECT 'query' AS name, '' AS label, 'Search query...' AS placeholder, + $query as value, 10 AS width, TRUE AS autofocus; @@ -20,10 +20,11 @@ SELECT 'search' AS name, '' AS label, 'Search' AS value, - 2 AS width, 'btn btn-info' AS class, + 2 AS width, 'submit' AS "type"; + SELECT 'new' AS name, '' AS label, @@ -47,7 +48,9 @@ SELECT text AS description FROM public.entries +WHERE + ($query IS NULL OR search_vector @@ to_tsquery('es_en', $query)) ORDER BY created_at LIMIT - 10; + 30; diff --git a/src/db/migrations/20250207213321_add_text_search_to_entries.sql b/src/db/migrations/20250207213321_add_text_search_to_entries.sql new file mode 100644 index 0000000..3697826 --- /dev/null +++ b/src/db/migrations/20250207213321_add_text_search_to_entries.sql @@ -0,0 +1,27 @@ +-- migrate:up +CREATE TEXT SEARCH CONFIGURATION es_en (COPY = pg_catalog.english); + +ALTER TEXT SEARCH CONFIGURATION es_en ALTER MAPPING FOR asciiword, +asciihword, +hword_asciipart, +word, +hword, +hword_part WITH spanish_stem, +english_stem; + +ALTER TABLE + public.entries +ADD + COLUMN search_vector tsvector GENERATED ALWAYS AS ( + to_tsvector( + 'es_en', + coalesce(uid, '') || ' ' || coalesce(text, '') + ) + ) STORED; + +CREATE INDEX entry_full_text_search ON public.entries USING gin (search_vector); + +-- migrate:down +DROP INDEX IF EXISTS entry_full_text_search; + +DROP TEXT SEARCH CONFIGURATION IF EXISTS es_en;