From 70d74f8fc17f3e516fd4c8b0623b24446ee62c7b Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Fri, 28 Sep 2018 05:27:08 -0400 Subject: [PATCH] FIX: advanced search ordering broken when using tags --- lib/search.rb | 4 ++-- spec/components/search_spec.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/search.rb b/lib/search.rb index 3cd0a20a143..80c6df50532 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -508,7 +508,7 @@ class Search WHERE tt.tag_id = tags.id GROUP BY tt.topic_id HAVING to_tsvector(#{default_ts_config}, array_to_string(array_agg(tags.name), ' ')) @@ to_tsquery(#{default_ts_config}, ?) - )", tags.join('&')).order("id") + )", tags.join('&')) else tags = match.split(",") @@ -516,7 +516,7 @@ class Search SELECT DISTINCT(tt.topic_id) FROM topic_tags tt, tags WHERE tt.tag_id = tags.id AND tags.name IN (?) - )", tags).order("id") + )", tags) end end diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 1b59d8a1186..912415a4a12 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -894,6 +894,22 @@ describe Search do expect(Search.execute('tags:eggs -tags:lunch,sandwiches').posts) .to contain_exactly(post1, post2) end + + it 'orders posts correctly when combining tags with categories or terms' do + cat1 = Fabricate(:category, name: 'food') + topic6 = Fabricate(:topic, tags: [tag1, tag2], category: cat1) + topic7 = Fabricate(:topic, tags: [tag1, tag2, tag3], category: cat1) + post7 = Fabricate(:post, topic: topic6, raw: "Wakey, wakey, eggs and bakey.", like_count: 5) + post8 = Fabricate(:post, topic: topic7, raw: "Bakey, bakey, eggs to makey.", like_count: 2) + + expect(Search.execute('bakey tags:lunch order:latest').posts.map(&:id)) + .to eq([post8.id, post7.id]) + expect(Search.execute('#food tags:lunch order:latest').posts.map(&:id)) + .to eq([post8.id, post7.id]) + expect(Search.execute('#food tags:lunch order:likes').posts.map(&:id)) + .to eq([post7.id, post8.id]) + end + end it "can find posts which contains filetypes" do