2013-05-23 14:26:51 -04:00
|
|
|
require_dependency 'search/search_result'
|
|
|
|
require_dependency 'search/search_result_type'
|
|
|
|
require_dependency 'search/grouped_search_results'
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
class Search
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def self.per_facet
|
|
|
|
5
|
|
|
|
end
|
|
|
|
|
2013-05-27 15:18:55 -04:00
|
|
|
# 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
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.facets
|
|
|
|
%w(topic category user)
|
|
|
|
end
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
def self.long_locale
|
2014-06-24 03:10:56 -04:00
|
|
|
# if adding a language see:
|
|
|
|
# /usr/share/postgresql/9.3/tsearch_data for possible options
|
|
|
|
# Do not add languages that are missing without amending the
|
|
|
|
# base docker config
|
|
|
|
#
|
|
|
|
case SiteSetting.default_locale.to_sym
|
|
|
|
when :da then 'danish'
|
|
|
|
when :de then 'german'
|
|
|
|
when :en then 'english'
|
|
|
|
when :es then 'spanish'
|
|
|
|
when :fr then 'french'
|
|
|
|
when :it then 'italian'
|
|
|
|
when :nl then 'dutch'
|
|
|
|
when :nb_NO then 'norwegian'
|
|
|
|
when :pt then 'portuguese'
|
|
|
|
when :pt_BR then 'portuguese'
|
|
|
|
when :sv then 'swedish'
|
|
|
|
when :ru then 'russian'
|
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
|
|
|
|
|
2014-06-24 03:10:56 -04:00
|
|
|
def self.rebuild_problem_posts(limit = 10000)
|
|
|
|
posts = Post.joins(:topic)
|
2014-07-01 08:00:57 -04:00
|
|
|
.where('posts.id IN (
|
|
|
|
SELECT p2.id FROM posts p2
|
|
|
|
LEFT JOIN post_search_data pd ON locale = ? AND p2.id = pd.post_id
|
|
|
|
WHERE pd.post_id IS NULL
|
2014-06-24 03:10:56 -04:00
|
|
|
)', SiteSetting.default_locale).limit(10000)
|
|
|
|
|
|
|
|
posts.each do |post|
|
2014-06-30 20:09:55 -04:00
|
|
|
# force indexing
|
2014-06-24 03:10:56 -04:00
|
|
|
post.cooked += " "
|
|
|
|
SearchObserver.index(post)
|
|
|
|
end
|
|
|
|
|
2014-08-08 01:50:26 -04:00
|
|
|
posts = Post.joins(:topic)
|
|
|
|
.where('posts.id IN (
|
|
|
|
SELECT p2.id FROM posts p2
|
|
|
|
LEFT JOIN topic_search_data pd ON locale = ? AND p2.topic_id = pd.topic_id
|
|
|
|
WHERE pd.topic_id IS NULL AND p2.post_number = 1
|
|
|
|
)', SiteSetting.default_locale).limit(10000)
|
|
|
|
|
|
|
|
posts.each do |post|
|
|
|
|
# force indexing
|
|
|
|
post.cooked += " "
|
|
|
|
SearchObserver.index(post)
|
|
|
|
end
|
|
|
|
|
2014-06-24 03:10:56 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.prepare_data(search_data)
|
|
|
|
data = search_data.squish
|
|
|
|
# TODO rmmseg is designed for chinese, we need something else for Korean / Japanese
|
|
|
|
if ['zh_TW', 'zh_CN', 'ja', 'ko'].include?(SiteSetting.default_locale)
|
|
|
|
unless defined? RMMSeg
|
|
|
|
require 'rmmseg'
|
|
|
|
RMMSeg::Dictionary.load_dictionaries
|
|
|
|
end
|
|
|
|
|
|
|
|
algo = RMMSeg::Algorithm.new(search_data)
|
|
|
|
|
|
|
|
data = ""
|
|
|
|
while token = algo.next_token
|
|
|
|
data << token.text << " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
data.force_encoding("UTF-8")
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
def initialize(term, opts=nil)
|
2013-05-23 14:26:51 -04:00
|
|
|
if term.present?
|
2014-06-24 03:10:56 -04:00
|
|
|
@term = Search.prepare_data(term.to_s)
|
2013-05-23 14:26:51 -04:00
|
|
|
@original_term = PG::Connection.escape_string(@term)
|
|
|
|
end
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
@opts = opts || {}
|
|
|
|
@guardian = @opts[:guardian] || Guardian.new
|
2013-05-24 14:03:45 -04:00
|
|
|
@search_context = @opts[:search_context]
|
2014-04-11 16:07:39 -04:00
|
|
|
@include_blurbs = @opts[:include_blurbs] || false
|
2013-05-23 14:26:51 -04:00
|
|
|
@limit = Search.per_facet * Search.facets.size
|
|
|
|
@results = GroupedSearchResults.new(@opts[:type_filter])
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Query a term
|
2013-05-22 14:36:14 -04:00
|
|
|
def execute
|
2013-05-23 14:26:51 -04:00
|
|
|
return nil if @term.blank? || @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-23 14:26:51 -04:00
|
|
|
if @results.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)
|
2013-05-23 14:26:51 -04:00
|
|
|
return single_topic(route[:topic_id]).as_json if route[:topic_id].present?
|
2013-05-13 17:04:41 -04:00
|
|
|
rescue ActionController::RoutingError
|
|
|
|
end
|
|
|
|
end
|
2013-03-01 12:45:25 -05:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
find_grouped_results.as_json
|
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-23 14:26:51 -04:00
|
|
|
def find_grouped_results
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
if @results.type_filter.present?
|
|
|
|
raise Discourse::InvalidAccess.new("invalid type filter") unless Search.facets.include?(@results.type_filter)
|
|
|
|
send("#{@results.type_filter}_search")
|
2013-05-22 14:36:14 -04:00
|
|
|
else
|
2013-05-23 14:26:51 -04:00
|
|
|
@limit = Search.per_facet + 1
|
2014-06-17 03:53:45 -04:00
|
|
|
unless @search_context
|
|
|
|
user_search
|
|
|
|
category_search
|
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
topic_search
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
add_more_topics_if_expected
|
|
|
|
@results
|
2014-03-07 14:59:29 -05:00
|
|
|
rescue ActiveRecord::StatementInvalid
|
|
|
|
# In the event of a PG:Error return nothing, it is likely they used a foreign language whose
|
|
|
|
# locale is not supported by postgres
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
# Add more topics if we expected them
|
|
|
|
def add_more_topics_if_expected
|
|
|
|
expected_topics = 0
|
|
|
|
expected_topics = Search.facets.size unless @results.type_filter.present?
|
|
|
|
expected_topics = Search.per_facet * Search.facets.size if @results.type_filter == 'topic'
|
|
|
|
expected_topics -= @results.topic_count
|
|
|
|
if expected_topics > 0
|
2013-05-29 17:52:09 -04:00
|
|
|
extra_posts = posts_query(expected_topics * Search.burst_factor)
|
|
|
|
extra_posts = extra_posts.where("posts.topic_id NOT in (?)", @results.topic_ids) if @results.topic_ids.present?
|
2013-05-27 15:18:55 -04:00
|
|
|
extra_posts.each do |p|
|
2014-04-11 16:07:39 -04:00
|
|
|
@results.add_result(SearchResult.from_post(p, @search_context, @term, @include_blurbs))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
# If we're searching for a single topic
|
|
|
|
def single_topic(id)
|
2014-05-06 09:41:59 -04:00
|
|
|
topic = Topic.find_by(id: id)
|
2013-05-22 14:36:14 -04:00
|
|
|
return nil unless @guardian.can_see?(topic)
|
2013-05-13 17:04:41 -04:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
@results.add_result(SearchResult.from_topic(topic))
|
|
|
|
@results
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
def secure_category_ids
|
|
|
|
return @secure_category_ids unless @secure_category_ids.nil?
|
|
|
|
@secure_category_ids = @guardian.secure_category_ids
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
def category_search
|
2013-12-13 03:00:48 -05:00
|
|
|
# scope is leaking onto Category, this is not good and probably a bug in Rails
|
|
|
|
# the secure_category_ids will invoke the same method on User, it calls Category.where
|
|
|
|
# however the scope from the query below is leaking in to Category, this works around
|
|
|
|
# the issue while we figure out what is up in Rails
|
|
|
|
secure_category_ids
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
categories = Category.includes(:category_search_data)
|
|
|
|
.where("category_search_data.search_data @@ #{ts_query}")
|
2013-08-25 17:18:11 -04:00
|
|
|
.references(:category_search_data)
|
2013-05-23 14:26:51 -04:00
|
|
|
.order("topics_month DESC")
|
|
|
|
.secured(@guardian)
|
|
|
|
.limit(@limit)
|
2013-05-13 17:04:41 -04:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
categories.each do |c|
|
|
|
|
@results.add_result(SearchResult.from_category(c))
|
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
def user_search
|
|
|
|
users = User.includes(:user_search_data)
|
2014-07-07 22:41:38 -04:00
|
|
|
.where("user_search_data.search_data @@ #{ts_query("simple")}")
|
2013-05-23 14:26:51 -04:00
|
|
|
.order("CASE WHEN username_lower = '#{@original_term.downcase}' THEN 0 ELSE 1 END")
|
|
|
|
.order("last_posted_at DESC")
|
|
|
|
.limit(@limit)
|
2013-08-16 08:53:40 -04:00
|
|
|
.references(:user_search_data)
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
users.each do |u|
|
|
|
|
@results.add_result(SearchResult.from_user(u))
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
def posts_query(limit)
|
|
|
|
posts = Post.includes(:post_search_data, {:topic => :category})
|
|
|
|
.where("topics.deleted_at" => nil)
|
|
|
|
.where("topics.visible")
|
|
|
|
.where("topics.archetype <> ?", Archetype.private_message)
|
2013-08-16 08:53:40 -04:00
|
|
|
.references(:post_search_data, {:topic => :category})
|
2013-08-01 20:31:36 -04:00
|
|
|
|
2014-06-20 01:48:34 -04:00
|
|
|
if @search_context.present? && @search_context.is_a?(Topic)
|
|
|
|
posts = posts.where("posts.raw ilike ?", "%#{@term}%")
|
|
|
|
else
|
|
|
|
posts = posts.where("post_search_data.search_data @@ #{ts_query}")
|
|
|
|
end
|
|
|
|
|
2013-05-24 16:17:09 -04:00
|
|
|
# If we have a search context, prioritize those posts first
|
2013-05-24 14:03:45 -04:00
|
|
|
if @search_context.present?
|
|
|
|
|
|
|
|
if @search_context.is_a?(User)
|
2014-06-17 03:53:45 -04:00
|
|
|
posts = posts.where("posts.user_id = #{@search_context.id}")
|
2013-05-24 14:03:45 -04:00
|
|
|
elsif @search_context.is_a?(Category)
|
2014-06-17 03:53:45 -04:00
|
|
|
posts = posts.where("topics.category_id = #{@search_context.id}")
|
2014-02-16 21:54:51 -05:00
|
|
|
elsif @search_context.is_a?(Topic)
|
2014-06-17 03:53:45 -04:00
|
|
|
posts = posts.where("topics.id = #{@search_context.id}")
|
|
|
|
.order("posts.post_number")
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-05-24 16:17:09 -04:00
|
|
|
posts = posts.order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC")
|
|
|
|
.order("TS_RANK_CD(post_search_data.search_data, #{ts_query}) DESC")
|
|
|
|
.order("topics.bumped_at DESC")
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
if secure_category_ids.present?
|
2013-08-25 17:18:11 -04:00
|
|
|
posts = posts.where("(categories.id IS NULL) OR (NOT categories.read_restricted) OR (categories.id IN (?))", secure_category_ids).references(:categories)
|
2013-05-22 14:36:14 -04:00
|
|
|
else
|
2013-08-25 17:18:11 -04:00
|
|
|
posts = posts.where("(categories.id IS NULL) OR (NOT categories.read_restricted)").references(:categories)
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2013-05-24 16:17:09 -04:00
|
|
|
posts.limit(limit)
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
|
|
|
|
2014-05-15 10:31:45 -04:00
|
|
|
def self.query_locale
|
2013-05-23 14:26:51 -04:00
|
|
|
@query_locale ||= Post.sanitize(Search.long_locale)
|
2013-05-23 11:13:23 -04:00
|
|
|
end
|
|
|
|
|
2014-05-15 10:31:45 -04:00
|
|
|
def query_locale
|
|
|
|
self.class.query_locale
|
|
|
|
end
|
|
|
|
|
2014-08-07 22:12:53 -04:00
|
|
|
def self.ts_query(term, locale = nil, joiner = "&")
|
2014-07-04 19:11:41 -04:00
|
|
|
locale = Post.sanitize(locale) if locale
|
2014-08-07 22:12:53 -04:00
|
|
|
all_terms = term.gsub(/[*:()&!'"]/,'').squish.split
|
|
|
|
query = Post.sanitize(all_terms.map {|t| "#{PG::Connection.escape_string(t)}:*"}.join(" #{joiner} "))
|
2014-07-04 19:11:41 -04:00
|
|
|
"TO_TSQUERY(#{locale || query_locale}, #{query})"
|
2014-05-15 10:31:45 -04:00
|
|
|
end
|
|
|
|
|
2014-07-07 22:41:38 -04:00
|
|
|
def ts_query(locale=nil)
|
|
|
|
if !locale
|
|
|
|
@ts_query ||= begin
|
|
|
|
Search.ts_query(@term, locale)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Search.ts_query(@term, locale)
|
2013-05-23 11:13:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
def topic_search
|
2013-05-24 17:44:35 -04:00
|
|
|
|
2014-02-16 21:54:51 -05:00
|
|
|
posts = if @search_context.is_a?(User)
|
|
|
|
# If we have a user filter, search all posts by default with a higher limit
|
|
|
|
posts_query(@limit * Search.burst_factor)
|
|
|
|
elsif @search_context.is_a?(Topic)
|
|
|
|
posts_query(@limit).where('posts.post_number = 1 OR posts.topic_id = ?', @search_context.id)
|
|
|
|
else
|
|
|
|
posts_query(@limit).where(post_number: 1)
|
|
|
|
end
|
|
|
|
|
2013-05-24 17:44:35 -04:00
|
|
|
|
|
|
|
posts.each do |p|
|
2014-04-11 16:07:39 -04:00
|
|
|
@results.add_result(SearchResult.from_post(p, @search_context, @term, @include_blurbs))
|
2013-05-23 11:13:23 -04:00
|
|
|
end
|
2014-02-16 21:54:51 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|