mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 14:12:10 +08:00
FIX: Removes some duplicates in search results when the search context is a user.
This commit is contained in:
@ -8,6 +8,12 @@ class Search
|
|||||||
5
|
5
|
||||||
end
|
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
|
def self.facets
|
||||||
%w(topic category user)
|
%w(topic category user)
|
||||||
end
|
end
|
||||||
@ -83,13 +89,9 @@ class Search
|
|||||||
expected_topics = Search.per_facet * Search.facets.size if @results.type_filter == 'topic'
|
expected_topics = Search.per_facet * Search.facets.size if @results.type_filter == 'topic'
|
||||||
expected_topics -= @results.topic_count
|
expected_topics -= @results.topic_count
|
||||||
if expected_topics > 0
|
if expected_topics > 0
|
||||||
topic_ids = @results.topic_ids
|
extra_posts = posts_query(expected_topics * Search.burst_factor).where("posts.topic_id NOT in (?)", @results.topic_ids)
|
||||||
posts_query(expected_topics * 3).where("post_number > 1").each do |p|
|
extra_posts.each do |p|
|
||||||
if (expected_topics > 0) && (!topic_ids.include?(p.topic_id))
|
@results.add_result(SearchResult.from_post(p))
|
||||||
@results.add_result(SearchResult.from_post(p))
|
|
||||||
topic_ids << p.topic_id
|
|
||||||
expected_topics -= 1
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -139,7 +141,6 @@ class Search
|
|||||||
.where("topics.visible")
|
.where("topics.visible")
|
||||||
.where("topics.archetype <> ?", Archetype.private_message)
|
.where("topics.archetype <> ?", Archetype.private_message)
|
||||||
|
|
||||||
|
|
||||||
# If we have a search context, prioritize those posts first
|
# If we have a search context, prioritize those posts first
|
||||||
if @search_context.present?
|
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
|
# 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 = if @search_context.present? and @search_context.is_a?(User)
|
||||||
posts_query(@limit * 3)
|
posts_query(@limit * Search.burst_factor)
|
||||||
else
|
else
|
||||||
posts_query(@limit).where(post_number: 1)
|
posts_query(@limit).where(post_number: 1)
|
||||||
end
|
end
|
||||||
|
@ -12,7 +12,7 @@ class Search
|
|||||||
def topic_ids
|
def topic_ids
|
||||||
topic_results = @by_type[:topic]
|
topic_results = @by_type[:topic]
|
||||||
return Set.new if topic_results.blank?
|
return Set.new if topic_results.blank?
|
||||||
Set.new(topic_results.results.map(&:id))
|
return topic_results.result_ids
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_json
|
def as_json
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
class Search
|
class Search
|
||||||
|
|
||||||
class SearchResultType
|
class SearchResultType
|
||||||
attr_accessor :more, :results
|
attr_accessor :more, :results, :result_ids
|
||||||
|
|
||||||
def initialize(type)
|
def initialize(type)
|
||||||
@type = type
|
@type = type
|
||||||
@results = []
|
@results = []
|
||||||
|
@result_ids = Set.new
|
||||||
@more = false
|
@more = false
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -14,7 +15,9 @@ class Search
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add(result)
|
def add(result)
|
||||||
|
return if @result_ids.include?(result.id)
|
||||||
@results << result
|
@results << result
|
||||||
|
@result_ids << result.id
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_json
|
def as_json
|
||||||
|
Reference in New Issue
Block a user