FIX: When using a search context, *prefer* the context's results, don't restrict to only them.

This commit is contained in:
Robin Ward 2013-05-24 16:17:09 -04:00
parent 3037e9adf6
commit 1313c0f094
2 changed files with 21 additions and 13 deletions

View File

@ -138,30 +138,34 @@ class Search
.where("topics.deleted_at" => nil)
.where("topics.visible")
.where("topics.archetype <> ?", Archetype.private_message)
.order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC")
.order("TS_RANK_CD(post_search_data.search_data, #{ts_query}) DESC")
.order("topics.bumped_at DESC")
.limit(limit)
# Search context post results
# If we have a search context, prioritize those posts first
if @search_context.present?
if @search_context.is_a?(User)
# If the context is a user, restrict posts to that user
posts = posts.where(user_id: @search_context.id)
# If the context is a user, prioritize that user's posts
posts = posts.order("CASE WHEN posts.user_id = #{@search_context.id} THEN 0 ELSE 1 END")
elsif @search_context.is_a?(Category)
# If the context is a category, restrict posts to that category
posts = posts.where('topics.category_id' => @search_context.id)
posts = posts.order("CASE WHEN topics.category_id = #{@search_context.id} THEN 0 ELSE 1 END")
end
end
posts = posts.order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC")
.order("TS_RANK_CD(post_search_data.search_data, #{ts_query}) DESC")
.order("topics.bumped_at DESC")
if secure_category_ids.present?
posts = posts.where("(categories.id IS NULL) OR (NOT categories.secure) OR (categories.id IN (?))", secure_category_ids)
else
posts = posts.where("(categories.id IS NULL) OR (NOT categories.secure)")
end
posts
posts.limit(limit)
end
def query_locale

View File

@ -239,11 +239,14 @@ describe Search do
context 'user as a search context' do
let(:search_user) { Search.new('hello', search_context: post.user).execute }
let(:search_coding_horror) { Search.new('hello', search_context: Fabricate(:coding_horror)).execute }
let(:coding_horror) { Fabricate(:coding_horror) }
let(:search_coding_horror) { Search.new('hello', search_context: coding_horror).execute }
Given!(:post) { Fabricate(:post) }
Given!(:coding_horror_post) { Fabricate(:post, user: coding_horror )}
Then { first_of_type(search_user, 'topic')['id'] == post.topic_id }
And { first_of_type(search_coding_horror, 'topic').should be_blank }
And { first_of_type(search_user, 'topic')['id'] == coding_horror_post.topic_id }
end
context 'category as a search context' do
@ -251,10 +254,11 @@ describe Search do
let(:search_cat) { Search.new('hello', search_context: category).execute }
let(:search_other_cat) { Search.new('hello', search_context: Fabricate(:category) ).execute }
let(:topic) { Fabricate(:topic, category: category) }
let(:topic_no_cat) { Fabricate(:topic) }
Given!(:post) { Fabricate(:post, topic: topic, user: topic.user ) }
Then { first_of_type(search_cat, 'topic')['id'] == post.topic_id }
Then { first_of_type(search_other_cat, 'topic').should be_blank }
Then { first_of_type(search_cat, 'topic')['id'] == topic.id }
Then { first_of_type(search_cat, 'topic')['id'] == topic_no_cat.id }
end