From e01802a13bdad41b24c5499333f4943cbea0fdf1 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Mon, 25 Jul 2016 15:06:25 +1000
Subject: [PATCH] FIX: strip quote from search term when searching within topic

---
 lib/search.rb                  | 12 +++++++++++-
 spec/components/search_spec.rb |  4 ++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/lib/search.rb b/lib/search.rb
index e39fda688a7..4cb1711c4e6 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 06b8bda183f..698fb568c74 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