Create migrations for FTS

This commit is contained in:
swaggboi 2023-10-27 14:34:17 -04:00
parent 682ef0e7c0
commit 7dc52a9655
2 changed files with 34 additions and 0 deletions

7
migrations/15/down.sql Normal file
View File

@ -0,0 +1,7 @@
DROP EXTENSION pg_trgm;
ALTER TABLE threads
DROP COLUMN search_tokens;
ALTER TABLE remarks
DROP COLUMN search_tokens;

27
migrations/15/up.sql Normal file
View File

@ -0,0 +1,27 @@
-- Fuzzy search
-- https://hevodata.com/blog/postgresql-full-text-search-setup/#Fuzzy_Search_vs_Full_Text_Search
CREATE EXTENSION pg_trgm;
-- Create column for seearch tokens
ALTER TABLE threads
ADD COLUMN search_tokens tsvector
GENERATED ALWAYS AS
(to_tsvector('english', thread_author) ||
to_tsvector('english', thread_title ) ||
to_tsvector('english', thread_body )) STORED;
-- Create GIN index for search tokens
CREATE INDEX threads_search_idx
ON threads
USING GIN(search_tokens);
-- Same for remarks
ALTER TABLE remarks
ADD COLUMN search_tokens tsvector
GENERATED ALWAYS AS
(to_tsvector('english', remark_author) ||
to_tsvector('english', remark_body )) STORED;
CREATE INDEX remarks_search_idx
ON remarks
USING GIN(search_tokens);