mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
parent
96719cbf4f
commit
6eb3d658ca
@ -1144,9 +1144,18 @@ class Search
|
|||||||
|
|
||||||
def self.to_tsquery(ts_config: nil, term:, joiner: nil)
|
def self.to_tsquery(ts_config: nil, term:, joiner: nil)
|
||||||
ts_config = ActiveRecord::Base.connection.quote(ts_config) if ts_config
|
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})"
|
# unaccent can be used only when a joiner is present because the
|
||||||
tsquery = "REPLACE(#{tsquery}::text, '&', '#{self.escape_string(joiner)}')::tsquery" if joiner
|
# 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
|
tsquery
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -580,6 +580,11 @@ describe Topic do
|
|||||||
end
|
end
|
||||||
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
|
it 'does not result in a syntax error when raw is blank after cooking' do
|
||||||
expect(Topic.similar_to('some title', '#')).to eq([])
|
expect(Topic.similar_to('some title', '#')).to eq([])
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user