about summary refs log tree commit diff
path: root/migrations
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-04-14T18·27+0200
committerVincent Ambo <github@tazj.in>2018-04-14T20·21+0200
commit2d8db520107c25c5bc7d1ba861047fa9b7eaea5f (patch)
tree3fd6ab26754606921118bad7fec0d9b54d9ad1bb /migrations
parent529da884da2a208ad91aebf052522517a704c742 (diff)
feat(migrations): Add materialized view & index for full text search
Adds a materialized view to be used for full-text searches that
indexes the tsvector documents for each post.
Diffstat (limited to 'migrations')
-rw-r--r--migrations/2018-04-14-170750_search-index/down.sql2
-rw-r--r--migrations/2018-04-14-170750_search-index/up.sql21
2 files changed, 23 insertions, 0 deletions
diff --git a/migrations/2018-04-14-170750_search-index/down.sql b/migrations/2018-04-14-170750_search-index/down.sql
new file mode 100644
index 000000000000..c57e66290201
--- /dev/null
+++ b/migrations/2018-04-14-170750_search-index/down.sql
@@ -0,0 +1,2 @@
+DROP INDEX idx_fts_search;
+DROP MATERIALIZED VIEW search_index;
diff --git a/migrations/2018-04-14-170750_search-index/up.sql b/migrations/2018-04-14-170750_search-index/up.sql
new file mode 100644
index 000000000000..ed997d3e041b
--- /dev/null
+++ b/migrations/2018-04-14-170750_search-index/up.sql
@@ -0,0 +1,21 @@
+-- Prepare a materialised view containing the tsvector data for all
+-- threads and posts. This view is indexed using a GIN-index to enable
+-- performant full-text searches.
+--
+-- For now the query language is hardcoded to be English.
+
+CREATE MATERIALIZED VIEW search_index AS
+  SELECT p.id AS post_id,
+         p.author_name AS author,
+         t.id AS thread_id,
+         t.title AS title,
+         p.body AS body,
+         setweight(to_tsvector('english', t.title), 'A') ||
+         setweight(to_tsvector('english', p.body), 'B') ||
+         setweight(to_tsvector('simple', t.author_name), 'C') ||
+         setweight(to_tsvector('simple', p.author_name), 'C') AS document
+    FROM posts p
+    JOIN threads t
+    ON t.id = p.thread_id;
+
+CREATE INDEX idx_fts_search ON search_index USING gin(document);