FIX: Don't ignore category in search when using category filters.

FIX: Don’t ignore category in search when using category filters.

diff --git a/lib/search.rb b/lib/search.rb
index bf3a771..a78937e 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -392,6 +392,7 @@ class Search
           Category.where('parent_category_id = ?', category_ids.first).pluck(:id)
       end
 
+      @category_filter_matched ||= true
       posts.where("topics.category_id IN (?)", category_ids)
     else
       posts.where("1 = 0")
@@ -441,6 +442,8 @@ class Search
         category_ids +=
           Category.where('parent_category_id = ?', category_id).pluck(:id)
       end
+
+      @category_filter_matched ||= true
       posts.where("topics.category_id IN (?)", category_ids)
     else
       # try a possible tag match
@@ -804,7 +807,8 @@ class Search
             .order("posts.post_number #{@order == :latest ? "DESC" : ""}")
         end
       else
-        categories_ignored(posts)
+        posts = categories_ignored(posts) unless @category_filter_matched
+        posts
       end
 
     if @order == :latest || (@term.blank? && !@order)
diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb
index c862130..0116e74 100644
--- a/spec/components/search_spec.rb
+++ b/spec/components/search_spec.rb
@@ -420,6 +420,7 @@ describe Search do
     let!(:ignored_category) do
       Fabricate(:category,
         name: "monkey Category 1",
+        slug: "test",
         search_priority: Searchable::PRIORITIES[:ignore]
       )
     end
@@ -432,6 +433,10 @@ describe Search do
       )
 
       expect(search.posts).to contain_exactly(category.topic.first_post, post)
+
+      search = Search.execute("monkey #test")
+
+      expect(search.posts).to contain_exactly(ignored_category.topic.first_post)
     end
 
     describe "with child categories" do

GitHub sha: 64f20e7e

2 Likes