FIX: Do not wrap unaccent around tsqueries (#16284)

tsqueries use quotes and having other characters that when unaccented
become quotes results in invalid tsqueries.
This commit is contained in:
Bianca Nenciu 2022-03-25 19:10:05 +02:00 committed by GitHub
parent 96719cbf4f
commit 6eb3d658ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -1144,9 +1144,18 @@ class Search
def self.to_tsquery(ts_config: nil, term:, joiner: nil)
ts_config = ActiveRecord::Base.connection.quote(ts_config) if ts_config
escaped_term = Search.wrap_unaccent("'#{self.escape_string(term)}'")
tsquery = "TO_TSQUERY(#{ts_config || default_ts_config}, #{escaped_term})"
tsquery = "REPLACE(#{tsquery}::text, '&', '#{self.escape_string(joiner)}')::tsquery" if joiner
# unaccent can be used only when a joiner is present because the
# additional processing and the final conversion to tsquery does not
# work well with characters that are converted to quotes by unaccent.
if joiner
tsquery = "TO_TSQUERY(#{ts_config || default_ts_config}, '#{self.escape_string(term)}')"
tsquery = "REPLACE(#{tsquery}::text, '&', '#{self.escape_string(joiner)}')::tsquery"
else
escaped_term = Search.wrap_unaccent("'#{self.escape_string(term)}'")
tsquery = "TO_TSQUERY(#{ts_config || default_ts_config}, #{escaped_term})"
end
tsquery
end

View File

@ -580,6 +580,11 @@ describe Topic do
end
end
it 'does not result in a syntax error when removing accents' do
SiteSetting.search_ignore_accents = true
expect(Topic.similar_to('something', "it's")).to eq([])
end
it 'does not result in a syntax error when raw is blank after cooking' do
expect(Topic.similar_to('some title', '#')).to eq([])
end