Refactor full-text search to get rid of tsvector columns.

Refactor full-text search on several tables (comments, dmails,
forum_posts, forum_topics, notes, and wiki_pages) to use to_tsvector
expression indexes instead of dedicated tsvector columns. This way
full-text search works the same way across all tables.

API changes:

* Changed /wiki_pages.json?search[body_matches] to match against only
  the body. Before `body_matches` matched against both the title and the body.

* Added /wiki_pages.json?search[title_or_body_matches] to match against
  both the title and the body.

* Fixed /dmails.json?search[message_matches] to match against both the
  title and body when doing a wildcard search. Before a wildcard search
  only matched against the body.

* Added /dmails.json?search[body_matches] to match against only the dmail body.
This commit is contained in:
evazion
2021-10-16 05:38:07 -05:00
parent 300bc6941e
commit e3b836b506
10 changed files with 125 additions and 18 deletions

View File

@@ -0,0 +1,28 @@
class AddTsvectorIndexToMultiple < ActiveRecord::Migration[6.1]
disable_ddl_transaction!
def up
add_index :comments, "to_tsvector('pg_catalog.english', body)", using: :gin, algorithm: :concurrently, name: "index_comments_on_body_tsvector"
add_index :dmails, "(to_tsvector('pg_catalog.english', title) || to_tsvector('pg_catalog.english', body))", using: :gin, algorithm: :concurrently, name: "index_dmails_on_title_and_body_tsvector"
add_index :forum_posts, "to_tsvector('pg_catalog.english', body)", using: :gin, algorithm: :concurrently, name: "index_forum_posts_on_body_tsvector"
add_index :forum_topics, "to_tsvector('pg_catalog.english', title)", using: :gin, algorithm: :concurrently, name: "index_forum_topics_on_title_tsvector"
add_index :notes, "to_tsvector('pg_catalog.english', body)", using: :gin, algorithm: :concurrently, name: "index_notes_on_body_tsvector"
add_index :wiki_pages, "(to_tsvector('pg_catalog.english', title) || to_tsvector('pg_catalog.english', body))", using: :gin, algorithm: :concurrently, name: "index_wiki_pages_on_title_and_body_tsvector"
execute("VACUUM (VERBOSE, ANALYZE) comments")
execute("VACUUM (VERBOSE, ANALYZE) dmails")
execute("VACUUM (VERBOSE, ANALYZE) forum_posts")
execute("VACUUM (VERBOSE, ANALYZE) forum_topics")
execute("VACUUM (VERBOSE, ANALYZE) notes")
execute("VACUUM (VERBOSE, ANALYZE) wiki_pages")
end
def down
remove_index :comments, algorithm: :concurrently, name: "index_comments_on_body_tsvector"
remove_index :dmails, algorithm: :concurrently, name: "index_dmails_on_title_and_body_tsvector"
remove_index :forum_posts, algorithm: :concurrently, name: "index_forum_posts_on_body_tsvector"
remove_index :forum_topics, algorithm: :concurrently, name: "index_forum_topics_on_title_tsvector"
remove_index :notes, algorithm: :concurrently, name: "index_notes_on_body_tsvector"
remove_index :wiki_pages, algorithm: :concurrently, name: "index_wiki_pages_on_title_and_body_tsvector"
end
end