FIX: Removes some duplicates in search results when the search context is a user.

This commit is contained in:
Robin Ward 2013-05-27 15:18:55 -04:00
parent 62a20f5655
commit 20e88f18ee
3 changed files with 15 additions and 11 deletions

View File

@ -8,6 +8,12 @@ class Search
5
end
# Sometimes we want more topics than are returned due to exclusion of dupes. This is the
# factor of extra results we'll ask for.
def self.burst_factor
3
end
def self.facets
%w(topic category user)
end
@ -83,13 +89,9 @@ class Search
expected_topics = Search.per_facet * Search.facets.size if @results.type_filter == 'topic'
expected_topics -= @results.topic_count
if expected_topics > 0
topic_ids = @results.topic_ids
posts_query(expected_topics * 3).where("post_number > 1").each do |p|
if (expected_topics > 0) && (!topic_ids.include?(p.topic_id))
extra_posts = posts_query(expected_topics * Search.burst_factor).where("posts.topic_id NOT in (?)", @results.topic_ids)
extra_posts.each do |p|
@results.add_result(SearchResult.from_post(p))
topic_ids << p.topic_id
expected_topics -= 1
end
end
end
end
@ -139,7 +141,6 @@ class Search
.where("topics.visible")
.where("topics.archetype <> ?", Archetype.private_message)
# If we have a search context, prioritize those posts first
if @search_context.present?
@ -181,7 +182,7 @@ class Search
# If we have a user filter, search all posts by default with a higher limit
posts = if @search_context.present? and @search_context.is_a?(User)
posts_query(@limit * 3)
posts_query(@limit * Search.burst_factor)
else
posts_query(@limit).where(post_number: 1)
end

View File

@ -12,7 +12,7 @@ class Search
def topic_ids
topic_results = @by_type[:topic]
return Set.new if topic_results.blank?
Set.new(topic_results.results.map(&:id))
return topic_results.result_ids
end
def as_json

View File

@ -1,11 +1,12 @@
class Search
class SearchResultType
attr_accessor :more, :results
attr_accessor :more, :results, :result_ids
def initialize(type)
@type = type
@results = []
@result_ids = Set.new
@more = false
end
@ -14,7 +15,9 @@ class Search
end
def add(result)
return if @result_ids.include?(result.id)
@results << result
@result_ids << result.id
end
def as_json