diff --git a/lib/search.rb b/lib/search.rb index 2dd6b100392..a22766ef190 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -462,7 +462,6 @@ class Search def self.ts_query(term, locale = nil, joiner = "&") - data = Post.exec_sql("SELECT to_tsvector(:locale, :term)", locale: locale || long_locale, term: term @@ -470,6 +469,10 @@ class Search locale = Post.sanitize(locale) if locale all_terms = data.scan(/'([^']+)'\:\d+/).flatten + all_terms.map! do |t| + t.split(/[\)\(&']/)[0] + end.compact! + query = Post.sanitize(all_terms.map {|t| "#{PG::Connection.escape_string(t)}:*"}.join(" #{joiner} ")) "TO_TSQUERY(#{locale || query_locale}, #{query})" end diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index f2b15a86d40..67af3e57eb5 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -443,5 +443,13 @@ describe Search do end end + it 'can parse complex strings using ts_query helper' do + str = " grigio:babel deprecated? " + str << "page page on Atmosphere](https://atmospherejs.com/grigio/babel)xxx:" + + ts_query = Search.ts_query(str, "simple") + Post.exec_sql("SELECT to_tsvector('bbb') @@ " << ts_query) + end + end