FIX: Handle case where `Post#raw` is blank in `Topic.similar_to`.

FIX: Handle case where Post#raw is blank in Topic.similar_to.

diff --git a/app/models/topic.rb b/app/models/topic.rb
index b6b128a..caedcd6 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -581,17 +581,21 @@ class Topic < ActiveRecord::Base
     return [] if title.blank?
     raw = raw.presence || ""
 
-    title_tsquery = Search.set_tsquery_weight_filter(
+    tsquery = Search.set_tsquery_weight_filter(
       Search.prepare_data(title.strip),
       'A'
     )
 
-    raw_tsquery = Search.set_tsquery_weight_filter(
-      Search.prepare_data(raw[0...MAX_SIMILAR_BODY_LENGTH].strip),
-      'B'
-    )
+    if raw.present?
+      raw_tsquery = Search.set_tsquery_weight_filter(
+        Search.prepare_data(raw[0...MAX_SIMILAR_BODY_LENGTH].strip),
+        'B'
+      )
+
+      tsquery = "#{tsquery} & #{raw_tsquery}"
+    end
 
-    tsquery = Search.to_tsquery(term: "#{title_tsquery} & #{raw_tsquery}", joiner: "|")
+    tsquery = Search.to_tsquery(term: tsquery, joiner: "|")
 
     candidates = Topic
       .visible
diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb
index 29af224..66faccd 100644
--- a/spec/models/topic_spec.rb
+++ b/spec/models/topic_spec.rb
@@ -531,6 +531,10 @@ describe Topic do
         expect(Topic.similar_to("has evil trout made any topics?", "i am wondering has evil trout made any topics?")).to eq([topic])
       end
 
+      it 'returns the similar topic even if raw is blank' do
+        expect(Topic.similar_to("has evil trout made any topics?", "")).to eq([topic])
+      end
+
       it 'matches title against title and raw against raw when searching for topics' do
         topic.update!(title: '1 2 3 numbered titles')
         post.update!(raw: 'random toy poodle')

GitHub sha: 4b21b5aa