2013-02-05 14:16:51 -05:00
|
|
|
module Search
|
|
|
|
|
|
|
|
def self.per_facet
|
|
|
|
5
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.facets
|
|
|
|
%w(topic category user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.user_query_sql
|
|
|
|
"SELECT 'user' AS type,
|
|
|
|
u.username_lower AS id,
|
|
|
|
'/users/' || u.username_lower AS url,
|
|
|
|
u.username AS title,
|
|
|
|
u.email,
|
2013-03-14 09:16:57 -04:00
|
|
|
NULL AS color,
|
|
|
|
NULL AS text_color
|
2013-02-28 14:03:52 -05:00
|
|
|
FROM users AS u
|
|
|
|
JOIN users_search s on s.id = u.id
|
|
|
|
WHERE s.search_data @@ TO_TSQUERY(:locale, :query)
|
2013-04-01 20:57:50 -04:00
|
|
|
ORDER BY CASE WHEN u.username_lower = lower(:orig) then 0 else 1 end, last_posted_at desc
|
2013-02-28 14:03:52 -05:00
|
|
|
LIMIT :limit
|
2013-02-05 14:16:51 -05:00
|
|
|
"
|
|
|
|
end
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
def self.post_query(guardian, type, args)
|
2013-05-12 20:48:32 -04:00
|
|
|
builder = SqlBuilder.new <<SQL
|
|
|
|
/*select*/
|
2013-02-05 14:16:51 -05:00
|
|
|
FROM topics AS ft
|
2013-05-12 20:48:32 -04:00
|
|
|
/*join*/
|
2013-02-05 14:16:51 -05:00
|
|
|
JOIN posts_search s on s.id = p.id
|
2013-05-12 20:48:32 -04:00
|
|
|
LEFT JOIN categories c ON c.id = ft.category_id
|
|
|
|
/*where*/
|
2013-02-25 11:42:20 -05:00
|
|
|
ORDER BY
|
2013-02-28 14:03:52 -05:00
|
|
|
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
|
2013-05-12 20:48:32 -04:00
|
|
|
SQL
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-05-12 20:48:32 -04:00
|
|
|
builder.select "'topic' AS type"
|
|
|
|
builder.select("CAST(ft.id AS VARCHAR)")
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-12 20:48:32 -04:00
|
|
|
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
|
2013-02-05 14:16:51 -05:00
|
|
|
AND ft.visible
|
|
|
|
AND ft.archetype <> '#{Archetype.private_message}'
|
2013-05-12 20:48:32 -04:00
|
|
|
SQL
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
add_allowed_categories(builder, guardian)
|
|
|
|
|
|
|
|
builder.exec(args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_allowed_categories(builder, guardian)
|
2013-05-12 20:48:32 -04:00
|
|
|
allowed_categories = nil
|
2013-05-13 04:04:03 -04:00
|
|
|
allowed_categories = guardian.secure_category_ids
|
2013-05-12 20:48:32 -04:00
|
|
|
if allowed_categories.present?
|
|
|
|
builder.where("(c.id IS NULL OR c.secure = 'f' OR c.id in (:category_ids))", category_ids: allowed_categories)
|
|
|
|
else
|
|
|
|
builder.where("(c.id IS NULL OR c.secure = 'f')")
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-12 20:48:32 -04:00
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
def self.category_query(guardian, args)
|
|
|
|
builder = SqlBuilder.new <<SQL
|
|
|
|
SELECT 'category' AS type,
|
2013-02-05 14:16:51 -05:00
|
|
|
c.name AS id,
|
|
|
|
'/category/' || c.slug AS url,
|
|
|
|
c.name AS title,
|
|
|
|
NULL AS email,
|
2013-03-14 09:16:57 -04:00
|
|
|
c.color,
|
|
|
|
c.text_color
|
2013-02-05 14:16:51 -05:00
|
|
|
FROM categories AS c
|
|
|
|
JOIN categories_search s on s.id = c.id
|
2013-05-13 04:04:03 -04:00
|
|
|
/*where*/
|
2013-02-05 14:16:51 -05:00
|
|
|
ORDER BY topics_month desc
|
2013-02-28 14:03:52 -05:00
|
|
|
LIMIT :limit
|
2013-05-13 04:04:03 -04:00
|
|
|
SQL
|
|
|
|
|
|
|
|
builder.where "s.search_data @@ TO_TSQUERY(:locale, :query)"
|
|
|
|
add_allowed_categories(builder,guardian)
|
|
|
|
|
|
|
|
builder.exec(args)
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
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-13 17:04:41 -04:00
|
|
|
# If we're searching for a single topic
|
|
|
|
def self.single_topic(id, guardian)
|
|
|
|
topic = Topic.where(id: id).first
|
|
|
|
return nil unless guardian.can_see?(topic)
|
|
|
|
|
|
|
|
results = [{'type' => 'topic',
|
|
|
|
'id' => topic.id,
|
|
|
|
'url' => topic.relative_url,
|
|
|
|
'title' => topic.title }]
|
|
|
|
group_db_result(results, 'topic')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Query a term
|
2013-05-13 04:04:03 -04:00
|
|
|
def self.query(term, guardian, type_filter=nil, min_search_term_length=3)
|
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
return nil if term.blank?
|
|
|
|
|
|
|
|
# If no guardian supplied, assume anonymous
|
2013-05-13 04:04:03 -04:00
|
|
|
guardian ||= Guardian.new(nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
term = term.to_s
|
|
|
|
|
|
|
|
# If the term is a number or url to a topic, just include that topic
|
|
|
|
if type_filter == 'topic'
|
|
|
|
|
|
|
|
begin
|
|
|
|
route = Rails.application.routes.recognize_path(term)
|
|
|
|
return single_topic(route[:topic_id], guardian) if route[:topic_id].present?
|
|
|
|
rescue ActionController::RoutingError
|
|
|
|
end
|
|
|
|
|
|
|
|
return single_topic(term.to_i, guardian) if term =~ /^\d+$/
|
|
|
|
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-03-11 12:18:25 -04:00
|
|
|
sanitized_term = PG::Connection.escape_string(term.gsub(/[:()&!]/,''))
|
2013-05-13 17:04:41 -04:00
|
|
|
query_string(sanitized_term, guardian, type_filter, min_search_term_length)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
# Search for a string term
|
|
|
|
def self.query_string(term, guardian, type_filter, min_search_term_length)
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
# really short terms are totally pointless
|
|
|
|
return nil if term.length < min_search_term_length
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
args = {orig: term,
|
|
|
|
query: term.split.map {|t| "#{t}:*"}.join(" & "),
|
|
|
|
locale: current_locale_long}
|
2013-05-12 20:48:32 -04:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
if type_filter.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
raise Discourse::InvalidAccess.new("invalid type filter") unless Search.facets.include?(type_filter)
|
2013-05-13 17:04:41 -04:00
|
|
|
args.merge!(limit: Search.per_facet * Search.facets.size)
|
|
|
|
db_result = case type_filter.to_s
|
|
|
|
when 'topic'
|
|
|
|
post_query(guardian, type_filter.to_sym, args)
|
|
|
|
when 'category'
|
|
|
|
category_query(guardian, args)
|
|
|
|
else
|
|
|
|
ActiveRecord::Base.exec_sql(Search.send("#{type_filter}_query_sql"), args)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
else
|
2013-05-13 17:04:41 -04:00
|
|
|
args.merge!(limit: (Search.per_facet + 1))
|
2013-02-05 14:16:51 -05:00
|
|
|
db_result = []
|
2013-05-13 17:04:41 -04:00
|
|
|
db_result += ActiveRecord::Base.exec_sql(user_query_sql, args).to_a
|
|
|
|
db_result += category_query(guardian, args).to_a
|
|
|
|
db_result += post_query(guardian, :topic, args).to_a
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
db_result = db_result.to_a
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
expected_topics = 0
|
|
|
|
expected_topics = Search.facets.size unless type_filter.present?
|
2013-02-11 01:03:21 -05:00
|
|
|
expected_topics = Search.per_facet * Search.facets.size if type_filter == 'topic'
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
if expected_topics > 0
|
2013-02-05 14:16:51 -05:00
|
|
|
db_result.each do |row|
|
|
|
|
expected_topics -= 1 if row['type'] == 'topic'
|
|
|
|
end
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
|
|
|
|
if expected_topics > 0
|
2013-05-13 17:04:41 -04:00
|
|
|
tmp = post_query(guardian, :post, args.merge(limit: expected_topics * 3)).to_a
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
topic_ids = Set.new db_result.map{|r| r["id"]}
|
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
tmp.reject! do |i|
|
|
|
|
if topic_ids.include?(i["id"])
|
2013-02-05 14:16:51 -05:00
|
|
|
true
|
|
|
|
else
|
|
|
|
topic_ids << i["id"]
|
|
|
|
false
|
|
|
|
end
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
db_result += tmp[0..expected_topics-1]
|
|
|
|
end
|
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
group_db_result(db_result, type_filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Group the results by type
|
|
|
|
def self.group_db_result(db_result, type_filter)
|
2013-02-05 14:16:51 -05:00
|
|
|
grouped = {}
|
|
|
|
db_result.each do |row|
|
|
|
|
type = row.delete('type')
|
|
|
|
|
|
|
|
# Add the slug for topics
|
2013-03-11 12:18:25 -04:00
|
|
|
if type == 'topic'
|
|
|
|
new_slug = Slug.for(row['title'])
|
|
|
|
new_slug = "topic" if new_slug.blank?
|
|
|
|
row['url'].gsub!('slug', new_slug)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
# Add avatars for users
|
|
|
|
row['avatar_template'] = User.avatar_template(row['email']) if type == 'user'
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Remove attributes when we know they don't matter
|
2013-02-25 11:42:20 -05:00
|
|
|
row.delete('email')
|
2013-02-05 14:16:51 -05:00
|
|
|
row.delete('color') unless type == 'category'
|
2013-03-14 09:16:57 -04:00
|
|
|
row.delete('text_color') unless type == 'category'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
grouped[type] ||= []
|
|
|
|
grouped[type] << row
|
|
|
|
end
|
|
|
|
|
2013-05-13 17:04:41 -04:00
|
|
|
grouped.map do |type, results|
|
2013-02-17 02:52:21 -05:00
|
|
|
more = type_filter.blank? && (results.size > Search.per_facet)
|
|
|
|
results = results[0..([results.length, Search.per_facet].min - 1)] if type_filter.blank?
|
2013-03-07 10:52:01 -05:00
|
|
|
{
|
|
|
|
type: type,
|
|
|
|
name: I18n.t("search.types.#{type}"),
|
|
|
|
more: more,
|
|
|
|
results: results
|
|
|
|
}
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|