From 7dc52a9655477e96c789be8005e9c834e161e4c4 Mon Sep 17 00:00:00 2001 From: swaggboi Date: Fri, 27 Oct 2023 14:34:17 -0400 Subject: [PATCH] Create migrations for FTS --- migrations/15/down.sql | 7 +++++++ migrations/15/up.sql | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 migrations/15/down.sql create mode 100644 migrations/15/up.sql 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);