From 2845164872020f6a0e34a2fbad7de7ed66e415da Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 16 Oct 2021 18:01:38 -0500 Subject: [PATCH] search: support quoted phrases, OR, and NOT operators in full-text search. Make all full-text search fields support quoted phrases and OR and NOT operators. This affects all text search fields (any search field that looks like `*_matches`). Examples: * hakurei reimu - matches anything containing the words "hakurei" and "reimu", in any order. * hakuri or reimu - matches either "hakurei" or "reimu". * hakurei -reimu - matches "hakurei" but not "reimu" * "hakurei reimu" - matches the exact phrase "hakurei reimu" * "reimu hakurei" - matches the exact phrase "reimu hakurei" * https://danbooru.donmai.us/notes?search[body_matches]=reimu+hakurei * https://danbooru.donmai.us/notes?search[body_matches]=reimu+or+hakurei * https://danbooru.donmai.us/notes?search[body_matches]=reimu+-hakurei * https://danbooru.donmai.us/notes?search[body_matches]="hakurei+reimu" * https://danbooru.donmai.us/notes?search[body_matches]="reimu+hakurei" The phrase search ability partially fixes #4536 (Inconsistent behavior of search function for comments/forums). See `websearch_to_tsquery` [1] for full details of the search syntax. [1]: https://www.postgresql.org/docs/current/textsearch-controls.html#TEXTSEARCH-PARSING-QUERIES --- app/logical/concerns/searchable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/logical/concerns/searchable.rb b/app/logical/concerns/searchable.rb index 691cff8a3..b1b134ebd 100644 --- a/app/logical/concerns/searchable.rb +++ b/app/logical/concerns/searchable.rb @@ -167,7 +167,7 @@ module Searchable to_tsvector("pg_catalog.english", arel_table[column]) end.reduce(:concat) - where("(#{tsvectors.to_sql}) @@ plainto_tsquery('pg_catalog.english', :query)", query: query) + where("(#{tsvectors.to_sql}) @@ websearch_to_tsquery('pg_catalog.english', :query)", query: query) end def search_boolean_attribute(attr, params)