Skip to content

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

FIX: `in:title` search should only search through topic first posts.

Guo Xiang Tanaf8791117896
diff --git a/lib/search.rb b/lib/search.rb
index 34893db6d05..0caf6e31335 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -847,6 +847,7 @@ class Search
# C is for tags
# D is for cooked
weights = @in_title ? 'A' : (SiteSetting.tagging_enabled ? 'ABCD' : 'ABD')
+ posts = posts.where(post_number: 1) if @in_title
posts = posts.where("post_search_data.search_data @@ #{ts_query(weight_filter: weights)}")
exact_terms = @term.scan(Regexp.new(PHRASE_MATCH_REGEXP_PATTERN)).flatten
diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb
index 652aa95f909..bc4239f87ed 100644
--- a/spec/components/search_spec.rb
+++ b/spec/components/search_spec.rb
@@ -1513,19 +1513,14 @@ describe Search do
context 'in:title' do
it 'allows for search in title' do
topic = Fabricate(:topic, title: 'I am testing a title search')
- _post = Fabricate(:post, topic: topic, raw: 'this is the first post')
+ post2 = Fabricate(:post, topic: topic, raw: 'this is the second post', post_number: 2)
+ post = Fabricate(:post, topic: topic, raw: 'this is the first post', post_number: 1)
results = Search.execute('title in:title')
- expect(results.posts.length).to eq(1)
-
- results = Search.execute('title t')
- expect(results.posts.length).to eq(1)
+ expect(results.posts.map(&:id)).to eq([post.id])
results = Search.execute('first in:title')
- expect(results.posts.length).to eq(0)
-
- results = Search.execute('first t')
- expect(results.posts.length).to eq(0)
+ expect(results.posts).to eq([])
end
it 'works irrespective of the order' do