From 97fa64d8468ab91265c9a8b1b9f42e62c40ca8de Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 1 Aug 2017 18:15:04 -0400 Subject: [PATCH] FIX: non tag/category # searches should be passed through --- lib/search.rb | 22 ++++++++++++++++------ spec/components/search_spec.rb | 12 ++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/search.rb b/lib/search.rb index a8c8282c598..3fc89f5fc38 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -425,12 +425,22 @@ class Search end posts.where("topics.category_id IN (?)", category_ids) else - posts.where("topics.id IN ( - SELECT DISTINCT(tt.topic_id) - FROM topic_tags tt, tags - WHERE tt.tag_id = tags.id - AND tags.name = ? - )", slug[0]) + # try a possible tag match + tag_id = Tag.where(name: slug[0]).pluck(:id).first + if (tag_id) + posts.where("topics.id IN ( + SELECT DISTINCT(tt.topic_id) + FROM topic_tags tt + WHERE tt.tag_id = ? + )", tag_id) + else + # a bit yucky but we got to add the term back in + if match.to_s.length >= SiteSetting.min_search_term_length + posts.where("posts.id IN ( + SELECT post_id FROM post_search_data pd1 + WHERE pd1.search_data @@ #{Search.ts_query("##{match}")})") + end + end end end diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index b78cec8fb10..0b858163954 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -730,6 +730,18 @@ describe Search do expect(Search.execute('this is a test #beta').posts.size).to eq(0) end + it 'correctly handles #symbol when no tag or category match' do + Fabricate(:post, raw: 'testing #1 #9998') + results = Search.new('testing #1').execute + expect(results.posts.length).to eq(1) + + results = Search.new('#9998').execute + expect(results.posts.length).to eq(1) + + results = Search.new('#777').execute + expect(results.posts.length).to eq(0) + end + context 'tags' do let(:tag1) { Fabricate(:tag, name: 'lunch') } let(:tag2) { Fabricate(:tag, name: 'eggs') }