A platform for community discussion. Free, open, simple.
FIX: Improve filtering with multiple tag filters (#34120)
Gabriel Grubba1f09720ba25d
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rbindex 5c923b202fc..0d085f7ec70 100644--- a/lib/topics_filter.rb+++ b/lib/topics_filter.rb@@ -34,7 +34,6 @@ class TopicsFiltervalues = hash["values"]filter_values = extract_and_validate_value_for(filter, values)-case filterwhen "activity-before"filter_by_activity(before: filter_values)@@ -579,11 +578,6 @@ class TopicsFilterdef filter_tags(values:)return if !SiteSetting.tagging_enabled?- exclude_all_tags = []- exclude_any_tags = []- include_any_tags = []- include_all_tags = []-values.each do |key_prefix, value|break if key_prefix && key_prefix != "-"@@ -597,45 +591,25 @@ class TopicsFiltertrueend- (- case [key_prefix, match_all]- in ["-", true]- exclude_all_tags- in ["-", false]- exclude_any_tags- in [nil, true]- include_all_tags- in [nil, false]- include_any_tags+ tags = tag_names.split(delimiter)+ tag_ids = tag_ids_from_tag_names(tags)++ case [key_prefix, match_all]+ in ["-", false]+ exclude_topics_with_any_tags(tag_ids)+ in ["-", true]+ exclude_topics_with_all_tags(tag_ids)+ in [nil, false]+ include_topics_with_any_tags(tag_ids)+ in [nil, true]+ has_invalid_tags = tag_ids.length < tags.length++ if has_invalid_tags+ @scope = @scope.none+ else+ include_topics_with_all_tags(tag_ids)end- ).concat(tag_names.split(delimiter))- end- end-- if exclude_all_tags.present?- exclude_topics_with_all_tags(tag_ids_from_tag_names(exclude_all_tags))- end-- if exclude_any_tags.present?- exclude_topics_with_any_tags(tag_ids_from_tag_names(exclude_any_tags))- end-- if include_any_tags.present?- include_topics_with_any_tags(tag_ids_from_tag_names(include_any_tags))- end-- if include_all_tags.present?- has_invalid_tags = false-- all_tag_ids =- tag_ids_from_tag_names(include_all_tags) do |tag_ids, _|- has_invalid_tags = tag_ids.length < include_all_tags.lengthend-- if has_invalid_tags- @scope = @scope.none- else- include_topics_with_all_tags(all_tag_ids)endendend@@ -673,7 +647,6 @@ class TopicsFilterdef include_topics_with_all_tags(tag_ids)tag_ids.each do |tag_id|sql_alias = topic_tags_alias-@scope =@scope.joins("INNER JOIN topic_tags #{sql_alias} ON #{sql_alias}.topic_id = topics.id AND #{sql_alias}.tag_id = #{tag_id}",diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex 672ca8149c2..38d86e4e1eb 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -878,6 +878,101 @@ RSpec.describe TopicsFilter do).to contain_exactly(topic_with_tag_and_tag2.id, topic_with_tag_and_tag2_and_tag3.id)end+ describe "when query string is `tags:front-end,back-end tags:pri-high,pri-low`" do+ fab!(:front_end) { Fabricate(:tag, name: "front-end") }+ fab!(:back_end) { Fabricate(:tag, name: "back-end") }+ fab!(:pri_high) { Fabricate(:tag, name: "pri-high") }+ fab!(:pri_low) { Fabricate(:tag, name: "pri-low") }++ it "should only return topics that are tagged with front-end+pri-high, front-end+pri-low, back-end+pri-high, back-end+pri-low" do+ topic_with_front_end_pri_high = Fabricate(:topic, tags: [front_end, pri_high])+ topic_with_front_end_pri_low = Fabricate(:topic, tags: [front_end, pri_low])+ topic_with_back_end_pri_high = Fabricate(:topic, tags: [back_end, pri_high])+ topic_with_back_end_pri_low = Fabricate(:topic, tags: [back_end, pri_low])++ Fabricate(:topic, tags: [pri_low, pri_high])+ Fabricate(:topic, tags: [front_end, back_end])++ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string(+ "tags:#{front_end.name},#{back_end.name} tags:#{pri_high.name},#{pri_low.name}",+ )+ .pluck(:id),+ ).to contain_exactly(+ topic_with_front_end_pri_high.id,+ topic_with_front_end_pri_low.id,+ topic_with_back_end_pri_high.id,+ topic_with_back_end_pri_low.id,+ )+ end++ it "should return topics that are tagged with front-end+back-end+pri-low or front-end+back-end+pri-high" do+ topic_with_front_end_back_end_pri_low =+ Fabricate(:topic, tags: [front_end, back_end, pri_low])+ topic_with_front_end_back_end_pri_high =+ Fabricate(:topic, tags: [front_end, back_end, pri_high])++ Fabricate(:topic, tags: [pri_low, pri_high])+ Fabricate(:topic, tags: [front_end, back_end])++ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string(+ "tags:#{front_end.name},#{back_end.name} tags:#{pri_low.name},#{pri_high.name}",+ )+ .pluck(:id),+ ).to contain_exactly(+ topic_with_front_end_back_end_pri_low.id,+ topic_with_front_end_back_end_pri_high.id,+ )+ end+ end++ describe "when query string is `tags:front-end tags:pri-high,pri-low`" do+ fab!(:front_end) { Fabricate(:tag, name: "front-end") }+ fab!(:pri_high) { Fabricate(:tag, name: "pri-high") }+ fab!(:pri_low) { Fabricate(:tag, name: "pri-low") }++ it "should only return topics tagged with front-end and or pri-high or pri-low" do+ topic_with_front_end_pri_high = Fabricate(:topic, tags: [front_end, pri_high])+ topic_with_front_end_pri_low = Fabricate(:topic, tags: [front_end, pri_low])+ topic_with_front_end_pri_high_pri_low =+ Fabricate(:topic, tags: [front_end, pri_high, pri_low])++ Fabricate(:topic, tags: [pri_low, pri_high])+ Fabricate(:topic, tags: [pri_high, pri_low])++ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string(+ "tags:#{front_end.name} tags:#{pri_high.name},#{pri_low.name}",+ )+ .pluck(:id),+ ).to contain_exactly(+ topic_with_front_end_pri_high.id,+ topic_with_front_end_pri_low.id,+ topic_with_front_end_pri_high_pri_low.id,+ )++ expect(+ TopicsFilter+ .new(guardian: Guardian.new)+ .filter_from_query_string(+ "tags:#{pri_high.name},#{pri_low.name} tags:#{front_end.name}",+ )+ .pluck(:id),+ ).to contain_exactly(+ topic_with_front_end_pri_high.id,+ topic_with_front_end_pri_low.id,+ topic_with_front_end_pri_high_pri_low.id,+ )+ end+ end+describe "when query string is `tags:tag1,tag2,tag3`" doit "should only return topics that are tagged with either tag1, tag2 or tag3" dotopic_with_tag3 = Fabricate(:topic, tags: [tag3])