mirror of
https://github.com/discourse/discourse.git
synced 2025-06-03 02:48:28 +08:00
Search code now uses ActiveRecord instead of SQL.
This commit is contained in:
39
lib/search/grouped_search_results.rb
Normal file
39
lib/search/grouped_search_results.rb
Normal file
@ -0,0 +1,39 @@
|
||||
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?
|
||||
Set.new(topic_results.results.map(&:id))
|
||||
end
|
||||
|
||||
def as_json
|
||||
@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
|
49
lib/search/search_result.rb
Normal file
49
lib/search/search_result.rb
Normal file
@ -0,0 +1,49 @@
|
||||
class Search
|
||||
|
||||
class SearchResult
|
||||
attr_accessor :type, :id
|
||||
|
||||
# Category attributes
|
||||
attr_accessor :color, :text_color
|
||||
|
||||
# User attributes
|
||||
attr_accessor :avatar_template
|
||||
|
||||
def initialize(row)
|
||||
row.symbolize_keys!
|
||||
@type = row[:type].to_sym
|
||||
@url, @id, @title = row[:url], row[:id].to_i, row[:title]
|
||||
end
|
||||
|
||||
def as_json
|
||||
json = {id: @id, title: @title, url: @url}
|
||||
json[:avatar_template] = @avatar_template if @avatar_template.present?
|
||||
json[:color] = @color if @color.present?
|
||||
json[:text_color] = @text_color if @text_color.present?
|
||||
json
|
||||
end
|
||||
|
||||
def self.from_category(c)
|
||||
SearchResult.new(type: :category, id: c.id, title: c.name, url: "/category/#{c.slug}").tap do |r|
|
||||
r.color = c.color
|
||||
r.text_color = c.text_color
|
||||
end
|
||||
end
|
||||
|
||||
def self.from_user(u)
|
||||
SearchResult.new(type: :user, id: u.username_lower, title: u.username, url: "/users/#{u.username_lower}").tap do |r|
|
||||
r.avatar_template = User.avatar_template(u.email)
|
||||
end
|
||||
end
|
||||
|
||||
def self.from_topic(t)
|
||||
SearchResult.new(type: :topic, id: t.id, title: t.title, url: t.relative_url)
|
||||
end
|
||||
|
||||
def self.from_post(p)
|
||||
SearchResult.from_topic(p.topic)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
28
lib/search/search_result_type.rb
Normal file
28
lib/search/search_result_type.rb
Normal file
@ -0,0 +1,28 @@
|
||||
class Search
|
||||
|
||||
class SearchResultType
|
||||
attr_accessor :more, :results
|
||||
|
||||
def initialize(type)
|
||||
@type = type
|
||||
@results = []
|
||||
@more = false
|
||||
end
|
||||
|
||||
def size
|
||||
@results.size
|
||||
end
|
||||
|
||||
def add(result)
|
||||
@results << result
|
||||
end
|
||||
|
||||
def as_json
|
||||
{ type: @type.to_s,
|
||||
name: I18n.t("search.types.#{@type.to_s}"),
|
||||
more: @more,
|
||||
results: @results.map(&:as_json) }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Reference in New Issue
Block a user