diff --git a/lib/search.rb b/lib/search.rb index e39fda688..4cb1711c4 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -516,8 +516,18 @@ class Search if @term.present? if is_topic_search + + term_without_quote = @term + if @term =~ /"(.+)"/ + term_without_quote = $1 + end + + if @term =~ /'(.+)'/ + term_without_quote = $1 + end + posts = posts.joins('JOIN users u ON u.id = posts.user_id') - posts = posts.where("posts.raw || ' ' || u.username || ' ' || COALESCE(u.name, '') ilike ?", "%#{@term}%") + posts = posts.where("posts.raw || ' ' || u.username || ' ' || COALESCE(u.name, '') ilike ?", "%#{term_without_quote}%") else posts = posts.where("post_search_data.search_data @@ #{ts_query}") exact_terms = @term.scan(/"([^"]+)"/).flatten diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 06b8bda18..698fb568c 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -213,6 +213,10 @@ describe Search do # stop words should work results = Search.execute('this', search_context: post1.topic) expect(results.posts.length).to eq(4) + + # phrase search works as expected + results = Search.execute('"fourth post I am posting"', search_context: post1.topic) + expect(results.posts.length).to eq(1) end end