diff --git a/migrations/15/down.sql b/migrations/15/down.sql new file mode 100644 index 0000000..404e499 --- /dev/null +++ b/migrations/15/down.sql @@ -0,0 +1,7 @@ +DROP EXTENSION pg_trgm; + +ALTER TABLE threads + DROP COLUMN search_tokens; + +ALTER TABLE remarks + DROP COLUMN search_tokens; diff --git a/migrations/15/up.sql b/migrations/15/up.sql new file mode 100644 index 0000000..6f8e0ca --- /dev/null +++ b/migrations/15/up.sql @@ -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);