feat: add full text search to entries

This commit is contained in:
Alexander Navarro 2025-02-07 19:13:44 -03:00
parent fff2bc3b3a
commit 6288969c4d
2 changed files with 33 additions and 3 deletions

View file

@ -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;

View file

@ -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;