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