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

@@ -28,7 +28,7 @@ class Comment < ApplicationRecord
module SearchMethods
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_sticky, :do_not_bump_post, :body, :score, :post, :creator, :updater)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :body_index)
q = q.text_attribute_matches(:body, params[:body_matches])
case params[:order]
when "post_id", "post_id_desc"

View File

@@ -99,7 +99,8 @@ class Dmail < ApplicationRecord
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_read, :is_deleted, :title, :body, :to, :from)
q = q.text_attribute_matches(:title, params[:title_matches])
q = q.text_attribute_matches(:body, params[:message_matches], index_column: :message_index)
q = q.text_attribute_matches(:body, params[:body_matches])
q = q.text_attribute_matches([:title, :body], params[:message_matches])
q = q.folder_matches(params[:folder])

View File

@@ -53,7 +53,7 @@ class ForumPost < ApplicationRecord
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :body, :creator, :updater, :topic, :dtext_links, :votes, :tag_alias, :tag_implication, :bulk_update_request)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :text_index)
q = q.text_attribute_matches(:body, params[:body_matches])
if params[:linked_to].present?
q = q.wiki_link_matches(params[:linked_to])

View File

@@ -86,7 +86,7 @@ class ForumTopic < ApplicationRecord
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_sticky, :is_locked, :is_deleted, :category_id, :title, :response_count, :creator, :updater, :forum_posts, :bulk_update_requests, :tag_aliases, :tag_implications)
q = q.text_attribute_matches(:title, params[:title_matches], index_column: :text_index)
q = q.text_attribute_matches(:title, params[:title_matches])
if params[:is_private].to_s.truthy?
q = q.private_only

View File

@@ -19,7 +19,7 @@ class Note < ApplicationRecord
module SearchMethods
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version, :post)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :body_index)
q = q.text_attribute_matches(:body, params[:body_matches])
q.apply_default_order(params)
end

View File

@@ -70,7 +70,8 @@ class WikiPage < ApplicationRecord
def search(params = {})
q = search_attributes(params, :id, :created_at, :updated_at, :is_locked, :is_deleted, :body, :title, :other_names, :tag, :artist, :dtext_links)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :body_index)
q = q.text_attribute_matches(:body, params[:body_matches])
q = q.text_attribute_matches([:title, :body], params[:title_or_body_matches])
if params[:title_normalize].present?
q = q.where_like(:title, normalize_title(params[:title_normalize]))