Skip to content

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.rb
index 67c56a53fbd..c38ef231a40 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -844,13 +844,17 @@ class Search
type_filter: "all_topics"
)
if @order == :latest
- if aggregate_search
+ if @in_title
+ posts = posts.order("topics.bumped_at DESC")
+ elsif aggregate_search
posts = posts.order("MAX(posts.created_at) DESC")
else
posts = posts.reorder("posts.created_at DESC")
end
elsif @order == :oldest
- if aggregate_search
+ if @in_title
+ posts = posts.order("topics.bumped_at ASC")
+ elsif aggregate_search
posts = posts.order("MAX(posts.created_at) ASC")
else
posts = posts.reorder("posts.created_at ASC")
diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb
index ed365fb4ec6..3df973329bb 100644
--- a/spec/lib/search_spec.rb
+++ b/spec/lib/search_spec.rb
@@ -2841,6 +2841,33 @@ RSpec.describe Search do
results = 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])
+ end
end
describe "include:invisible / include:unlisted" do