2013-05-22 14:36:14 -04:00
|
|
|
class Search
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def self.per_facet
|
|
|
|
5
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.facets
|
|
|
|
%w(topic category user)
|
|
|
|
end
|
|
|
|
|
2013-02-28 14:14:22 -05:00
|
|
|
def self.current_locale_long
|
|
|
|
case I18n.locale # Currently-present in /conf/locales/* only, sorry :-( Add as needed
|
2013-04-10 13:45:29 -04:00
|
|
|
when :da then 'danish'
|
|
|
|
when :de then 'german'
|
|
|
|
when :en then 'english'
|
|
|
|
when :es then 'spanish'
|
2013-02-28 14:14:22 -05:00
|
|
|
when :fr then 'french'
|
2013-04-10 13:45:29 -04:00
|
|
|
when :it then 'italian'
|
2013-02-28 14:14:22 -05:00
|
|
|
when :nl then 'dutch'
|
2013-04-10 13:45:29 -04:00
|
|
|
when :pt then 'portuguese'
|
2013-02-28 14:14:22 -05:00
|
|
|
when :sv then 'swedish'
|
2013-04-10 13:45:29 -04:00
|
|
|
else 'simple' # use the 'simple' stemmer for other languages
|
2013-02-28 14:14:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
def initialize(term, opts=nil)
|
|
|
|
@term = term.to_s if term.present?
|
|
|
|
@opts = opts || {}
|
|
|
|
@guardian = @opts[:guardian] || Guardian.new
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Query a term
|
2013-05-22 14:36:14 -04:00
|
|
|
def execute
|
2013-05-13 17:04:41 -04:00
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
return nil if @term.blank?
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
# really short terms are totally pointless
|
|
|
|
return nil if @term.length < (@opts[:min_search_term_length] || SiteSetting.min_search_term_length)
|
2013-05-13 17:04:41 -04:00
|
|
|
|
|
|
|
# If the term is a number or url to a topic, just include that topic
|
2013-05-22 14:36:14 -04:00
|
|
|
if @opts[:type_filter] == 'topic'
|
2013-05-13 17:04:41 -04:00
|
|
|
|
|
|
|
begin
|
2013-05-22 14:36:14 -04:00
|
|
|
route = Rails.application.routes.recognize_path(@term)
|
|
|
|
return single_topic(route[:topic_id]) if route[:topic_id].present?
|
2013-05-13 17:04:41 -04:00
|
|
|
rescue ActionController::RoutingError
|
|
|
|
end
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
return single_topic(@term.to_i) if @term =~ /^\d+$/
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
2013-03-01 12:45:25 -05:00
|
|
|
|
2013-02-28 14:52:35 -05:00
|
|
|
# We are stripping only symbols taking place in FTS and simply sanitizing the rest.
|
2013-05-22 14:36:14 -04:00
|
|
|
@term = PG::Connection.escape_string(@term.gsub(/[:()&!]/,''))
|
|
|
|
|
|
|
|
query_string
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
private
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
# Search for a string term
|
|
|
|
def query_string
|
|
|
|
|
|
|
|
args = {orig: @term,
|
|
|
|
query: @term.split.map {|t| "#{t}:*"}.join(" & "),
|
|
|
|
locale: Search.current_locale_long}
|
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
results = GroupedSearchResults.new(@opts[:type_filter])
|
2013-05-22 14:36:14 -04:00
|
|
|
type_filter = @opts[:type_filter]
|
|
|
|
|
|
|
|
if type_filter.present?
|
|
|
|
raise Discourse::InvalidAccess.new("invalid type filter") unless Search.facets.include?(type_filter)
|
|
|
|
args.merge!(limit: Search.per_facet * Search.facets.size)
|
2013-05-23 11:13:23 -04:00
|
|
|
case type_filter.to_s
|
|
|
|
when 'topic'
|
|
|
|
results.add(post_query(type_filter.to_sym, args))
|
|
|
|
when 'category'
|
|
|
|
results.add(category_query(args))
|
|
|
|
when 'user'
|
|
|
|
results.add(user_query(args))
|
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
else
|
|
|
|
args.merge!(limit: (Search.per_facet + 1))
|
2013-05-23 11:13:23 -04:00
|
|
|
results.add(user_query(args).to_a)
|
|
|
|
results.add(category_query(args).to_a)
|
|
|
|
results.add(post_query(:topic, args).to_a)
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
expected_topics = 0
|
|
|
|
expected_topics = Search.facets.size unless type_filter.present?
|
|
|
|
expected_topics = Search.per_facet * Search.facets.size if type_filter == 'topic'
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
# Subtract how many topics we have
|
|
|
|
expected_topics -= results.topic_count
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
if expected_topics > 0
|
|
|
|
extra_topics = post_query(:post, args.merge(limit: expected_topics * 3)).to_a
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
topic_ids = results.topic_ids
|
|
|
|
extra_topics.reject! do |i|
|
|
|
|
new_topic_id = i['id'].to_i
|
|
|
|
if topic_ids.include?(new_topic_id)
|
2013-05-22 14:36:14 -04:00
|
|
|
true
|
|
|
|
else
|
2013-05-23 11:13:23 -04:00
|
|
|
topic_ids << new_topic_id
|
2013-05-22 14:36:14 -04:00
|
|
|
false
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 11:13:23 -04:00
|
|
|
results.add(extra_topics[0..expected_topics-1])
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
results.as_json
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
# If we're searching for a single topic
|
|
|
|
def single_topic(id)
|
|
|
|
topic = Topic.where(id: id).first
|
|
|
|
return nil unless @guardian.can_see?(topic)
|
2013-05-13 17:04:41 -04:00
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
results = GroupedSearchResults.new(@opts[:type_filter])
|
|
|
|
results.add('type' => 'topic',
|
|
|
|
'id' => topic.id,
|
|
|
|
'url' => topic.relative_url,
|
|
|
|
'title' => topic.title)
|
|
|
|
results.as_json
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_allowed_categories(builder)
|
|
|
|
allowed_categories = nil
|
|
|
|
allowed_categories = @guardian.secure_category_ids
|
|
|
|
if allowed_categories.present?
|
|
|
|
builder.where("(c.id IS NULL OR c.secure OR c.id in (:category_ids))", category_ids: allowed_categories)
|
|
|
|
else
|
|
|
|
builder.where("(c.id IS NULL OR (NOT c.secure))")
|
2013-03-11 12:18:25 -04:00
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
def category_query(args)
|
|
|
|
builder = SqlBuilder.new <<SQL
|
|
|
|
SELECT 'category' AS type,
|
|
|
|
c.name AS id,
|
|
|
|
'/category/' || c.slug AS url,
|
|
|
|
c.name AS title,
|
|
|
|
NULL AS email,
|
|
|
|
c.color,
|
|
|
|
c.text_color
|
|
|
|
FROM categories AS c
|
2013-05-22 15:33:33 -04:00
|
|
|
JOIN category_search_data s on s.category_id = c.id
|
2013-05-22 14:36:14 -04:00
|
|
|
/*where*/
|
|
|
|
ORDER BY topics_month desc
|
|
|
|
LIMIT :limit
|
|
|
|
SQL
|
|
|
|
|
|
|
|
builder.where "s.search_data @@ TO_TSQUERY(:locale, :query)"
|
|
|
|
add_allowed_categories(builder)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
builder.exec(args)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
def user_query(args)
|
|
|
|
sql = "SELECT 'user' AS type,
|
|
|
|
u.username_lower AS id,
|
|
|
|
'/users/' || u.username_lower AS url,
|
|
|
|
u.username AS title,
|
2013-05-23 11:13:23 -04:00
|
|
|
u.email
|
2013-05-22 14:36:14 -04:00
|
|
|
FROM users AS u
|
2013-05-22 15:33:33 -04:00
|
|
|
JOIN user_search_data s on s.user_id = u.id
|
2013-05-22 14:36:14 -04:00
|
|
|
WHERE s.search_data @@ TO_TSQUERY(:locale, :query)
|
|
|
|
ORDER BY CASE WHEN u.username_lower = lower(:orig) then 0 else 1 end, last_posted_at desc
|
|
|
|
LIMIT :limit"
|
|
|
|
ActiveRecord::Base.exec_sql(sql, args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_query(type, args)
|
|
|
|
builder = SqlBuilder.new <<SQL
|
|
|
|
/*select*/
|
|
|
|
FROM topics AS ft
|
|
|
|
/*join*/
|
2013-05-22 15:33:33 -04:00
|
|
|
JOIN post_search_data s on s.post_id = p.id
|
2013-05-22 14:36:14 -04:00
|
|
|
LEFT JOIN categories c ON c.id = ft.category_id
|
|
|
|
/*where*/
|
|
|
|
ORDER BY
|
|
|
|
TS_RANK_CD(TO_TSVECTOR(:locale, ft.title), TO_TSQUERY(:locale, :query)) desc,
|
|
|
|
TS_RANK_CD(search_data, TO_TSQUERY(:locale, :query)) desc,
|
|
|
|
bumped_at desc
|
|
|
|
LIMIT :limit
|
|
|
|
SQL
|
|
|
|
|
|
|
|
builder.select "'topic' AS type"
|
|
|
|
builder.select("CAST(ft.id AS VARCHAR)")
|
|
|
|
|
|
|
|
if type == :topic
|
|
|
|
builder.select "'/t/slug/' || ft.id AS url"
|
|
|
|
else
|
|
|
|
builder.select "'/t/slug/' || ft.id || '/' || p.post_number AS url"
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.select "ft.title, NULL AS email, NULL AS color, NULL AS text_color"
|
|
|
|
|
|
|
|
if type == :topic
|
|
|
|
builder.join "posts AS p ON p.topic_id = ft.id AND p.post_number = 1"
|
|
|
|
else
|
|
|
|
builder.join "posts AS p ON p.topic_id = ft.id AND p.post_number > 1"
|
|
|
|
end
|
|
|
|
|
|
|
|
builder.where <<SQL
|
|
|
|
s.search_data @@ TO_TSQUERY(:locale, :query)
|
|
|
|
AND ft.deleted_at IS NULL
|
|
|
|
AND p.deleted_at IS NULL
|
|
|
|
AND ft.visible
|
|
|
|
AND ft.archetype <> '#{Archetype.private_message}'
|
|
|
|
SQL
|
|
|
|
|
|
|
|
add_allowed_categories(builder)
|
|
|
|
|
|
|
|
builder.exec(args)
|
|
|
|
end
|
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
class SearchResult
|
|
|
|
attr_accessor :type, :id
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
def initialize(row)
|
|
|
|
@type = row['type'].to_sym
|
|
|
|
@url, @id, @title = row['url'], row['id'].to_i, row['title']
|
|
|
|
|
|
|
|
case @type
|
|
|
|
when :topic
|
|
|
|
# Some topics don't have slugs. In that case, use 'topic' as the slug.
|
2013-05-22 14:36:14 -04:00
|
|
|
new_slug = Slug.for(row['title'])
|
|
|
|
new_slug = "topic" if new_slug.blank?
|
2013-05-23 11:13:23 -04:00
|
|
|
@url.gsub!('slug', new_slug)
|
|
|
|
when :user
|
|
|
|
@avatar_template = User.avatar_template(row['email'])
|
|
|
|
when :category
|
|
|
|
@color, @text_color = row['color'], row['text_color']
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2013-05-23 11:13:23 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
def as_json
|
|
|
|
{ type: @type.to_s,
|
|
|
|
name: I18n.t("search.types.#{@type.to_s}"),
|
|
|
|
more: @more,
|
|
|
|
results: @results.map(&:as_json) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class GroupedSearchResults
|
|
|
|
|
|
|
|
attr_reader :topic_count
|
|
|
|
|
|
|
|
def initialize(type_filter)
|
|
|
|
@type_filter = type_filter
|
|
|
|
@by_type = {}
|
|
|
|
@topic_count = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def add(results)
|
|
|
|
results = [results] if results.is_a?(Hash)
|
|
|
|
|
|
|
|
results.each do |r|
|
|
|
|
add_result(SearchResult.new(r))
|
2013-05-22 15:33:33 -04:00
|
|
|
end
|
2013-05-23 11:13:23 -04:00
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
def topic_ids
|
|
|
|
topic_results = @by_type[:topic]
|
|
|
|
return Set.new if topic_results.blank?
|
|
|
|
|
|
|
|
Set.new(topic_results.results.map(&:id))
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
|
|
|
|
2013-05-23 11:13:23 -04:00
|
|
|
def as_json
|
|
|
|
@by_type.values.map do |grouped_result|
|
|
|
|
grouped_result.as_json
|
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2013-05-23 11:13:23 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|