diff --git a/lib/search.rb b/lib/search.rb index afb1a54b4a7..b21ba0fe7c0 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -153,12 +153,17 @@ class Search def posts_query(limit) posts = Post.includes(:post_search_data, {:topic => :category}) - .where("post_search_data.search_data @@ #{ts_query}") .where("topics.deleted_at" => nil) .where("topics.visible") .where("topics.archetype <> ?", Archetype.private_message) .references(:post_search_data, {:topic => :category}) + if @search_context.present? && @search_context.is_a?(Topic) + posts = posts.where("posts.raw ilike ?", "%#{@term}%") + else + posts = posts.where("post_search_data.search_data @@ #{ts_query}") + end + # If we have a search context, prioritize those posts first if @search_context.present? diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index cf25bd0cecf..1695910cf32 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -142,12 +142,13 @@ describe Search do topic2 = Fabricate(:topic) new_post('this is the other post I am posting', topic2) + new_post('this is my fifth post I am posting', topic2) + post1 = new_post('this is the other post I am posting', topic) post2 = new_post('this is my first post I am posting', topic) post3 = new_post('this is a real long and complicated bla this is my second post I am Posting birds with more stuff bla bla', topic) post4 = new_post('this is my fourth post I am posting', topic) - new_post('this is my fifth post I am posting', topic2) # update posts_count topic.reload @@ -164,6 +165,13 @@ describe Search do "_#{post3.id}", "_#{post4.id}"] + # stop words should work + results = Search.new('this', search_context: post1.topic).execute.find do |r| + r[:type] == "topic" + end[:results] + + results.length.should == 4 + end end