searchable: standardize the <field>_matches operator for text fields.

Standardize it so that all fields of type `text` are searchable with
`search[<field>_matches]`.

Before, the `<field>_matches` param was handled manually and some fields
were left out or handled inconsistently. Now it applies to all columns
of type `text`.

This does a full-text search on the field, so for example, searching
`/artist_commentaries?search[translated_description_matches]=smiling`
will match translated commentaries containing either the word "smiling",
"smiles", "smiled", or "smile".

Note that this only applies to columns defined as type `text`, not to
columns defined as `character varying`. The difference is that `text` is
used for fields containing free-form natural language, such as comments,
notes, forum posts, wiki pages, pool descriptions, etc, while `character
varying` is used for short strings not containing free-form language,
such as tag names, wiki page titles, urls, status fields, etc.

API changes:

* Add the `search[original_title_matches]`, `search[original_description_matches]`,
  `search[translated_title_matches]`, `search[translated_description_matches]` params
  to /artist_commentaries and /artist_commentary_versions.
* Remove the `search[name_matches]` and `search[group_name_matches]` params from /artist_versions.
* Remove the `search[title_matches]` param from /wiki_page_versions.
* Change the `search[name_matches]` param on /pools, /favorite_groups, and /pool_versions
  to do a full-text search instead of a substring match.
This commit is contained in:
evazion
2022-09-21 19:32:25 -05:00
parent a6e0872ce4
commit 3114ef3daf
21 changed files with 18 additions and 28 deletions

View File

@@ -188,7 +188,7 @@ module Searchable
end
end
def text_attribute_matches(columns, query)
def where_text_matches(columns, query)
columns = Array.wrap(columns)
if query.nil?
@@ -264,7 +264,9 @@ module Searchable
end
case type
when :string, :text
when :string # :string is for columns of type `character varying` in the database
search_string_attribute(name)
when :text # :text is for columns of type `text` in the database
search_text_attribute(name)
when :uuid
search_uuid_attribute(name)
@@ -323,7 +325,7 @@ module Searchable
relation
end
def search_text_attribute(attr)
def search_string_attribute(attr)
relation = self.relation
if params[attr].present?
@@ -397,6 +399,16 @@ module Searchable
relation
end
def search_text_attribute(attr)
relation = search_string_attribute(attr)
if params[:"#{attr}_matches"].present?
relation = visible(relation, attr).where_text_matches(attr, params[:"#{attr}_matches"])
end
relation
end
def search_uuid_attribute(attr)
relation = self.relation

View File

@@ -18,8 +18,6 @@ class ArtistVersion < ApplicationRecord
module SearchMethods
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :is_banned, :name, :group_name, :urls, :other_names, :updater, :artist)
q = q.text_attribute_matches(:name, params[:name_matches])
q = q.text_attribute_matches(:group_name, params[:group_name_matches])
if params[:order] == "name"
q = q.order("artist_versions.name").default_order

View File

@@ -20,7 +20,6 @@ class Ban < ApplicationRecord
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :duration, :reason, :user, :banner)
q = q.text_attribute_matches(:reason, params[:reason_matches])
q = q.expired if params[:expired].to_s.truthy?
q = q.unexpired if params[:expired].to_s.falsy?

View File

@@ -31,7 +31,6 @@ class BulkUpdateRequest < ApplicationRecord
module SearchMethods
def search(params = {})
q = search_attributes(params, :id, :created_at, :updated_at, :script, :tags, :user, :forum_topic, :forum_post, :approver)
q = q.text_attribute_matches(:script, params[:script_matches])
if params[:status].present?
q = q.where(status: params[:status].split(","))

View File

@@ -34,7 +34,6 @@ 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])
case params[:order]
when "post_id", "post_id_desc"

View File

@@ -106,9 +106,7 @@ 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[:body_matches])
q = q.text_attribute_matches([:title, :body], params[:message_matches])
q = q.where_text_matches([:title, :body], params[:message_matches])
q = q.folder_matches(params[:folder])

View File

@@ -58,7 +58,6 @@ 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])
if params[:linked_to].present?
q = q.wiki_link_matches(params[:linked_to])

View File

@@ -89,7 +89,6 @@ 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])
if params[:is_private].to_s.truthy?
q = q.private_only

View File

@@ -36,7 +36,6 @@ class IpBan < ApplicationRecord
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :ip_addr, :reason, :is_deleted, :category, :hit_count, :last_hit_at, :creator)
q = q.text_attribute_matches(:reason, params[:reason_matches])
case params[:order]
when /\A(created_at|updated_at|last_hit_at)(?:_(asc|desc))?\z/i

View File

@@ -80,7 +80,6 @@ class ModAction < ApplicationRecord
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :category, :description, :creator)
q = q.text_attribute_matches(:description, params[:description_matches])
case params[:order]
when "created_at_asc"

View File

@@ -84,7 +84,6 @@ class ModerationReport < ApplicationRecord
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :creator, :model, :status)
q = q.text_attribute_matches(:reason, params[:reason_matches])
if params[:recipient_id].present?
q = q.received_by(User.search(id: params[:recipient_id]))

View File

@@ -21,7 +21,6 @@ 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])
q.apply_default_order(params)
end

View File

@@ -7,7 +7,6 @@ class NoteVersion < ApplicationRecord
def self.search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_active, :x, :y, :width, :height, :body, :version, :updater, :note, :post)
q = q.text_attribute_matches(:body, params[:body_matches])
q.apply_default_order(params)
end

View File

@@ -39,7 +39,6 @@ class Pool < ApplicationRecord
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :name, :description, :post_ids, :dtext_links)
q = q.text_attribute_matches(:description, params[:description_matches])
if params[:post_tags_match]
q = q.post_tags_match(params[:post_tags_match])

View File

@@ -25,7 +25,6 @@ class PostAppeal < ApplicationRecord
module SearchMethods
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status, :creator, :post)
q = q.text_attribute_matches(:reason, params[:reason_matches])
q.apply_default_order(params)
end

View File

@@ -36,7 +36,6 @@ class PostDisapproval < ApplicationRecord
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :message, :reason, :post)
q = q.text_attribute_matches(:message, params[:message_matches])
q = q.with_message if params[:has_message].to_s.truthy?
q = q.without_message if params[:has_message].to_s.falsy?

View File

@@ -60,7 +60,6 @@ class PostFlag < ApplicationRecord
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :reason, :status, :post, :creator)
q = q.text_attribute_matches(:reason, params[:reason_matches])
if params[:category]
q = q.category_matches(params[:category])

View File

@@ -34,7 +34,6 @@ class UserFeedback < ApplicationRecord
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :category, :body, :is_deleted, :creator, :user)
q = q.text_attribute_matches(:body, params[:body_matches])
q.apply_default_order(params)
end

View File

@@ -63,8 +63,7 @@ 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])
q = q.text_attribute_matches([:title, :body], params[:title_or_body_matches])
q = q.where_text_matches([:title, :body], params[:title_or_body_matches])
if params[:title_normalize].present?
q = q.where_like(:title, normalize_title(params[:title_normalize]))

View File

@@ -9,8 +9,6 @@ class WikiPageVersion < ApplicationRecord
module SearchMethods
def search(params)
q = search_attributes(params, :id, :created_at, :updated_at, :title, :body, :other_names, :is_locked, :is_deleted, :updater, :wiki_page, :tag)
q = q.text_attribute_matches(:title, params[:title_matches])
q = q.text_attribute_matches(:body, params[:body_matches])
q.apply_default_order(params)
end