A platform for community discussion. Free, open, simple.
FIX: sort by topic activity when searching in:title (#36630)
Régis Hanol3997ce5e1c0b
diff --git a/lib/search.rb b/lib/search.rbindex 67c56a53fbd..c38ef231a40 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -844,13 +844,17 @@ class Searchtype_filter: "all_topics")if @order == :latest- if aggregate_search+ if @in_title+ posts = posts.order("topics.bumped_at DESC")+ elsif aggregate_searchposts = posts.order("MAX(posts.created_at) DESC")elseposts = posts.reorder("posts.created_at DESC")endelsif @order == :oldest- if aggregate_search+ if @in_title+ posts = posts.order("topics.bumped_at ASC")+ elsif aggregate_searchposts = posts.order("MAX(posts.created_at) ASC")elseposts = posts.reorder("posts.created_at ASC")diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex ed365fb4ec6..3df973329bb 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -2841,6 +2841,33 @@ RSpec.describe Search doresults = Search.execute("in:title status:open Discourse")expect(results.posts.length).to eq(1)end++ it "sorts by topic bumped_at" do+ old_bumped_topic =+ Fabricate(:topic, title: "Old bumped topic about Discourse", bumped_at: 1.day.ago)+ new_bumped_topic =+ Fabricate(:topic, title: "New bumped topic about Discourse", bumped_at: 1.hour.ago)++ Fabricate(+ :post,+ topic: old_bumped_topic,+ raw: "This is the first post",+ created_at: 1.hour.ago,+ )++ Fabricate(+ :post,+ topic: new_bumped_topic,+ raw: "This is the first post",+ created_at: 1.day.ago,+ )++ results = Search.execute("Discourse in:title order:latest")+ expect(results.posts.map(&:topic_id)).to eq([new_bumped_topic.id, old_bumped_topic.id])++ results = Search.execute("Discourse in:title order:oldest")+ expect(results.posts.map(&:topic_id)).to eq([old_bumped_topic.id, new_bumped_topic.id])+ endenddescribe "include:invisible / include:unlisted" do