2013-05-23 14:26:51 -04:00
|
|
|
class Search
|
|
|
|
|
|
|
|
class GroupedSearchResults
|
|
|
|
attr_reader :topic_count, :type_filter
|
|
|
|
|
|
|
|
def initialize(type_filter)
|
|
|
|
@type_filter = type_filter
|
|
|
|
@by_type = {}
|
|
|
|
@topic_count = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def topic_ids
|
|
|
|
topic_results = @by_type[:topic]
|
|
|
|
return Set.new if topic_results.blank?
|
2013-05-27 15:18:55 -04:00
|
|
|
return topic_results.result_ids
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
|
|
|
|
2013-11-30 00:43:44 -05:00
|
|
|
def as_json(options = nil)
|
2013-05-23 14:26:51 -04:00
|
|
|
@by_type.values.map do |grouped_result|
|
|
|
|
grouped_result.as_json
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_result(result)
|
|
|
|
grouped_result = @by_type[result.type] || (@by_type[result.type] = SearchResultType.new(result.type))
|
|
|
|
|
|
|
|
# Limit our results if there is no filter
|
|
|
|
if @type_filter.present? or (grouped_result.size < Search.per_facet)
|
|
|
|
@topic_count += 1 if (result.type == :topic)
|
|
|
|
|
|
|
|
grouped_result.add(result)
|
|
|
|
else
|
|
|
|
grouped_result.more = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|