Skip to content

Commit

Permalink
FIX: Do not wrap unaccent around tsqueries (discourse#16284)
Browse files Browse the repository at this point in the history
tsqueries use quotes and having other characters that when unaccented
become quotes results in invalid tsqueries.
  • Loading branch information
nbianca authored Mar 25, 2022
1 parent 96719cb commit 6eb3d65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1144,9 +1144,18 @@ def self.ts_query(term: , ts_config: nil, joiner: nil, weight_filter: nil)

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

Expand Down
5 changes: 5 additions & 0 deletions spec/models/topic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ def build_topic_with_title(title)
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
Expand Down

0 comments on commit 6eb3d65

Please sign in to comment.