diff --git a/lib/search.rb b/lib/search.rb index ae6fa466420..115fcd80762 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -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)) - @results.add_result(SearchResult.from_post(p)) - topic_ids << p.topic_id - expected_topics -= 1 - end + 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)) 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 diff --git a/lib/search/grouped_search_results.rb b/lib/search/grouped_search_results.rb index fb35b926330..6ba6267a561 100644 --- a/lib/search/grouped_search_results.rb +++ b/lib/search/grouped_search_results.rb @@ -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 diff --git a/lib/search/search_result_type.rb b/lib/search/search_result_type.rb index 744fe5cb4f1..d720792209a 100644 --- a/lib/search/search_result_type.rb +++ b/lib/search/search_result_type.rb @@ -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