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 SELECT
'form' AS component, 'form' AS component,
'entries/search.sql' AS "action",
'Entries' AS title, 'Entries' AS title,
'' AS validate, '' AS validate,
'GET' AS method; 'GET' AS method;
@ -13,6 +12,7 @@ SELECT
'query' AS name, 'query' AS name,
'' AS label, '' AS label,
'Search query...' AS placeholder, 'Search query...' AS placeholder,
$query as value,
10 AS width, 10 AS width,
TRUE AS autofocus; TRUE AS autofocus;
@ -20,10 +20,11 @@ SELECT
'search' AS name, 'search' AS name,
'' AS label, '' AS label,
'Search' AS value, 'Search' AS value,
2 AS width,
'btn btn-info' AS class, 'btn btn-info' AS class,
2 AS width,
'submit' AS "type"; 'submit' AS "type";
SELECT SELECT
'new' AS name, 'new' AS name,
'' AS label, '' AS label,
@ -47,7 +48,9 @@ SELECT
text AS description text AS description
FROM FROM
public.entries public.entries
WHERE
($query IS NULL OR search_vector @@ to_tsquery('es_en', $query))
ORDER BY ORDER BY
created_at created_at
LIMIT 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;