diff --git a/lib/search.rb b/lib/search.rb index 4de9c8a528b..af5dc057502 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -297,7 +297,12 @@ class Search if category_id posts.where("topics.category_id = ?", category_id) else - posts.where("1 = 0") + 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]) end end diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 128c7764629..c6ffc4eea27 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -536,7 +536,7 @@ describe Search do end - it 'supports category slug' do + it 'supports category slug and tags' do # main category category = Fabricate(:category, name: 'category 24', slug: 'category-24') topic = Fabricate(:topic, created_at: 3.months.ago, category: category) @@ -552,6 +552,11 @@ describe Search do expect(Search.execute('testing again #category-24:sub-category').posts.length).to eq(1) expect(Search.execute('testing again #sub-category').posts.length).to eq(0) + + # tags + topic.tags = [Fabricate(:tag, name: 'alpha')] + expect(Search.execute('this is a test #alpha').posts.map(&:id)).to eq([post.id]) + expect(Search.execute('this is a test #beta').posts.size).to eq(0) end it "can find with tag" do