Skip to content

A platform for community discussion. Free, open, simple.

FIX: topic search order

Régis Hanola56321efb55c
diff --git a/lib/search.rb b/lib/search.rb
index eec10ddd455..a265a6ecbd8 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -1191,9 +1191,9 @@ class Search
posts.where("topics.category_id in (?)", category_ids)
elsif is_topic_search
- posts.where("topics.id = ?", @search_context.id).order(
- "posts.post_number #{@order == :latest ? "DESC" : ""}",
- )
+ posts = posts.where("topics.id = ?", @search_context.id)
+ posts = posts.order("posts.post_number ASC") unless @order
+ posts
elsif @search_context.is_a?(Tag)
posts =
posts.joins("LEFT JOIN topic_tags ON topic_tags.topic_id = topics.id").joins(
diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb
index 89f3bfe830e..566b95a06a6 100644
--- a/spec/lib/search_spec.rb
+++ b/spec/lib/search_spec.rb
@@ -2176,6 +2176,18 @@ RSpec.describe Search do
expect(Search.execute("Topic max_views:150").posts.map(&:id)).to eq([post.id])
end
+ it "can order by likes" do
+ raw = "Foo bar lorem ipsum"
+ topic = Fabricate(:topic)
+ post1 = Fabricate(:post, topic:, raw:, like_count: 1)
+ post2 = Fabricate(:post, topic:, raw:, like_count: 2)
+ post3 = Fabricate(:post, topic:, raw:, like_count: 3)
+
+ expect(Search.execute("topic:#{topic.id} bar order:likes").posts.map(&:id)).to eq(
+ [post3, post2, post1].map(&:id),
+ )
+ end
+
it "can search for terms with dots" do
post = Fabricate(:post, raw: "Will.2000 Will.Bob.Bill...")
expect(Search.execute("bill").posts.map(&:id)).to eq([post.id])