From 6c5689984fc54bdc8152bdf889e3223f4f505833 Mon Sep 17 00:00:00 2001 From: Josh Moore Date: Fri, 28 Jun 2019 10:19:57 +0200 Subject: [PATCH] FEATURE: in:tagged search (srv side) (#7822) * FEATURE: in:tagged and in:untagged advanced search filters Similar to in:solved or in:unsolved, the filters check for an existence of the topic_id in the topic_tags table. see: https://meta.discourse.org/t/how-to-search-filter-untagged-topics/119641/2 --- lib/search.rb | 12 ++++++++++++ spec/components/search_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/lib/search.rb b/lib/search.rb index aa3bdb02b9c..395ea955746 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -265,6 +265,18 @@ class Search @advanced_filters end + advanced_filter(/^in:tagged$/) do |posts| + posts + .where('EXISTS (SELECT 1 FROM topic_tags WHERE topic_tags.topic_id = posts.topic_id)') + end + + advanced_filter(/^in:untagged$/) do |posts| + posts + .joins("LEFT JOIN topic_tags ON + topic_tags.topic_id = posts.topic_id") + .where("topic_tags.id IS NULL") + end + advanced_filter(/^status:open$/) do |posts| posts.where('NOT topics.closed AND NOT topics.archived') end diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 7e6e45c2b77..d1d7bde413c 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -1377,4 +1377,32 @@ describe Search do end + context 'in:tagged' do + it 'allows for searching by presence of any tags' do + topic = Fabricate(:topic, title: 'I am testing a tagged search') + _post = Fabricate(:post, topic: topic, raw: 'this is the first post') + tag = Fabricate(:tag) + topic_tag = Fabricate(:topic_tag, topic: topic, tag: tag) + + results = Search.execute('in:untagged') + expect(results.posts.length).to eq(0) + + results = Search.execute('in:tagged') + expect(results.posts.length).to eq(1) + end + end + + context 'in:untagged' do + it 'allows for searching by presence of no tags' do + topic = Fabricate(:topic, title: 'I am testing a untagged search') + _post = Fabricate(:post, topic: topic, raw: 'this is the first post') + + results = Search.execute('in:untagged') + expect(results.posts.length).to eq(1) + + results = Search.execute('in:tagged') + expect(results.posts.length).to eq(0) + end + end + end