diff --git a/lib/search.rb b/lib/search.rb index 6b0f458148..323b0942de 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -474,7 +474,7 @@ class Search end end - if word == 'order:latest' + if word == 'order:latest' || word == 'l' @order = :latest nil elsif word == 'order:latest_topic' @@ -664,7 +664,7 @@ class Search posts = posts.where("topics.category_id in (?)", category_ids) elsif @search_context.is_a?(Topic) posts = posts.where("topics.id = #{@search_context.id}") - .order("posts.post_number") + .order("posts.post_number #{@order == :latest ? "DESC" : ""}") end end @@ -673,7 +673,7 @@ class Search if opts[:aggregate_search] posts = posts.order("MAX(posts.created_at) DESC") else - posts = posts.order("posts.created_at DESC") + posts = posts.reorder("posts.created_at DESC") end elsif @order == :latest_topic if opts[:aggregate_search] @@ -710,6 +710,7 @@ class Search else posts = posts.where("(categories.id IS NULL) OR (NOT categories.read_restricted)").references(:categories) end + posts.limit(limit) end diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index 056491b81b..d215c8835c 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -253,6 +253,9 @@ describe Search do results = Search.execute('posting', search_context: post1.topic) expect(results.posts.map(&:id)).to eq([post1.id, post2.id, post3.id, post4.id]) + results = Search.execute('posting l', search_context: post1.topic) + expect(results.posts.map(&:id)).to eq([post4.id, post3.id, post2.id, post1.id]) + # stop words should work results = Search.execute('this', search_context: post1.topic) expect(results.posts.length).to eq(4) @@ -633,6 +636,8 @@ describe Search do expect(Search.execute('sam').posts.map(&:id)).to eq([post1.id, post2.id]) expect(Search.execute('sam order:latest').posts.map(&:id)).to eq([post2.id, post1.id]) + expect(Search.execute('sam l').posts.map(&:id)).to eq([post2.id, post1.id]) + expect(Search.execute('l sam').posts.map(&:id)).to eq([post2.id, post1.id]) end it 'can order by topic creation' do