2013-05-23 14:26:51 -04:00
|
|
|
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
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
def self.per_filter
|
|
|
|
50
|
|
|
|
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
|
2014-12-03 21:46:52 -05:00
|
|
|
%w(topic category user private_messages)
|
2013-02-05 14:16:51 -05:00
|
|
|
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 += " "
|
2016-12-21 21:13:14 -05:00
|
|
|
SearchIndexer.index(post)
|
2014-06-24 03:10:56 -04:00
|
|
|
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 += " "
|
2016-12-21 21:13:14 -05:00
|
|
|
SearchIndexer.index(post)
|
2014-08-08 01:50:26 -04:00
|
|
|
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
|
2015-11-27 00:35:27 -05:00
|
|
|
if ['zh_TW', 'zh_CN', 'ja', 'ko'].include?(SiteSetting.default_locale) || SiteSetting.search_tokenize_chinese_japanese_korean
|
2014-06-24 03:10:56 -04:00
|
|
|
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
|
|
|
|
|
2016-03-14 08:27:02 -04:00
|
|
|
def self.word_to_date(str)
|
|
|
|
|
|
|
|
if str =~ /^[0-9]{1,3}$/
|
|
|
|
return Time.zone.now.beginning_of_day.days_ago(str.to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
if str =~ /^([12][0-9]{3})(-([0-1]?[0-9]))?(-([0-3]?[0-9]))?$/
|
|
|
|
year = $1.to_i
|
|
|
|
month = $2 ? $3.to_i : 1
|
|
|
|
day = $4 ? $5.to_i : 1
|
|
|
|
|
|
|
|
return if day==0 || month==0 || day > 31 || month > 12
|
|
|
|
|
|
|
|
return Time.zone.parse("#{year}-#{month}-#{day}") rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if str.downcase == "yesterday"
|
|
|
|
return Time.zone.now.beginning_of_day.yesterday
|
|
|
|
end
|
|
|
|
|
|
|
|
titlecase = str.downcase.titlecase
|
|
|
|
|
|
|
|
if Date::DAYNAMES.include?(titlecase)
|
|
|
|
return Time.zone.now.beginning_of_week(str.downcase.to_sym)
|
|
|
|
end
|
|
|
|
|
|
|
|
if idx = (Date::MONTHNAMES.find_index(titlecase) ||
|
|
|
|
Date::ABBR_MONTHNAMES.find_index(titlecase))
|
|
|
|
delta = Time.zone.now.month - idx
|
|
|
|
delta += 12 if delta < 0
|
|
|
|
Time.zone.now.beginning_of_month.months_ago(delta)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-10 15:40:58 -04:00
|
|
|
def self.min_post_id_no_cache
|
|
|
|
return 0 unless SiteSetting.search_prefer_recent_posts?
|
|
|
|
|
|
|
|
|
2016-08-12 00:36:38 -04:00
|
|
|
offset, has_more = Post.unscoped
|
|
|
|
.order('id desc')
|
|
|
|
.offset(SiteSetting.search_recent_posts_size-1)
|
|
|
|
.limit(2)
|
|
|
|
.pluck(:id)
|
|
|
|
|
|
|
|
has_more ? offset : 0
|
2016-08-10 15:40:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.min_post_id(opts=nil)
|
|
|
|
return 0 unless SiteSetting.search_prefer_recent_posts?
|
|
|
|
|
|
|
|
# It can be quite slow to count all the posts so let's cache it
|
2016-08-11 14:06:07 -04:00
|
|
|
Rails.cache.fetch("search-min-post-id:#{SiteSetting.search_recent_posts_size}", expires_in: 1.week) do
|
2016-08-10 15:40:58 -04:00
|
|
|
min_post_id_no_cache
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-09 14:48:39 -04:00
|
|
|
attr_accessor :term
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
def initialize(term, opts=nil)
|
|
|
|
@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
|
2015-07-27 02:13:11 -04:00
|
|
|
@blurb_length = @opts[:blurb_length]
|
2014-09-02 05:15:08 -04:00
|
|
|
@limit = Search.per_facet
|
2016-08-09 14:48:39 -04:00
|
|
|
@valid = true
|
2014-12-04 18:22:39 -05:00
|
|
|
|
2015-06-26 03:36:28 -04:00
|
|
|
term = process_advanced_search!(term)
|
2015-09-15 03:21:46 -04:00
|
|
|
|
2015-06-26 03:36:28 -04:00
|
|
|
if term.present?
|
|
|
|
@term = Search.prepare_data(term.to_s)
|
|
|
|
@original_term = PG::Connection.escape_string(@term)
|
|
|
|
end
|
|
|
|
|
2014-12-04 18:22:39 -05:00
|
|
|
if @search_pms && @guardian.user
|
|
|
|
@opts[:type_filter] = "private_messages"
|
|
|
|
@search_context = @guardian.user
|
|
|
|
end
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
if @opts[:type_filter].present?
|
|
|
|
@limit = Search.per_filter
|
|
|
|
end
|
|
|
|
|
2015-07-27 02:13:11 -04:00
|
|
|
@results = GroupedSearchResults.new(@opts[:type_filter], term, @search_context, @include_blurbs, @blurb_length)
|
2014-09-02 05:15:08 -04:00
|
|
|
end
|
|
|
|
|
2016-08-09 14:48:39 -04:00
|
|
|
def valid?
|
|
|
|
@valid
|
|
|
|
end
|
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
def self.execute(term, opts=nil)
|
|
|
|
self.new(term, opts).execute
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Query a term
|
2013-05-22 14:36:14 -04:00
|
|
|
def execute
|
2016-08-09 14:48:39 -04:00
|
|
|
|
|
|
|
unless @filters.present?
|
|
|
|
min_length = @opts[:min_search_term_length] || SiteSetting.min_search_term_length
|
|
|
|
terms = (@term || '').split(/\s(?=(?:[^"]|"[^"]*")*$)/).reject {|t| t.length < min_length }
|
|
|
|
|
|
|
|
if terms.blank?
|
|
|
|
@term = ''
|
|
|
|
@valid = false
|
|
|
|
return
|
|
|
|
end
|
2015-06-22 22:14:06 -04:00
|
|
|
end
|
2013-05-13 17:04:41 -04:00
|
|
|
|
|
|
|
# If the term is a number or url to a topic, just include that topic
|
2014-08-28 15:42:29 -04:00
|
|
|
if @opts[:search_for_id] && @results.type_filter == 'topic'
|
2014-09-02 05:15:08 -04:00
|
|
|
if @term =~ /^\d+$/
|
|
|
|
single_topic(@term.to_i)
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
route = Rails.application.routes.recognize_path(@term)
|
|
|
|
single_topic(route[:topic_id]) if route[:topic_id].present?
|
|
|
|
rescue ActionController::RoutingError
|
|
|
|
end
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
|
|
|
end
|
2013-03-01 12:45:25 -05:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
find_grouped_results unless @results.posts.present?
|
|
|
|
|
|
|
|
@results
|
2013-05-13 17:04:41 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-06-22 22:14:06 -04:00
|
|
|
def self.advanced_filter(trigger,&block)
|
|
|
|
(@advanced_filters ||= {})[trigger] = block
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.advanced_filters
|
|
|
|
@advanced_filters
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/status:open/) do |posts|
|
|
|
|
posts.where('NOT topics.closed AND NOT topics.archived')
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/status:closed/) do |posts|
|
|
|
|
posts.where('topics.closed')
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/status:archived/) do |posts|
|
|
|
|
posts.where('topics.archived')
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/status:noreplies/) do |posts|
|
|
|
|
posts.where("topics.posts_count = 1")
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/status:single_user/) do |posts|
|
|
|
|
posts.where("topics.participant_count = 1")
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/posts_count:(\d+)/) do |posts, match|
|
|
|
|
posts.where("topics.posts_count = ?", match.to_i)
|
|
|
|
end
|
|
|
|
|
2016-11-28 09:57:18 -05:00
|
|
|
advanced_filter(/min_post_count:(\d+)/) do |posts, match|
|
|
|
|
posts.where("topics.posts_count >= ?", match.to_i)
|
|
|
|
end
|
|
|
|
|
2015-06-22 23:39:40 -04:00
|
|
|
advanced_filter(/in:first/) do |posts|
|
|
|
|
posts.where("posts.post_number = 1")
|
|
|
|
end
|
2015-06-22 22:14:06 -04:00
|
|
|
|
2016-03-18 01:26:54 -04:00
|
|
|
advanced_filter(/in:pinned/) do |posts|
|
|
|
|
posts.where("topics.pinned_at IS NOT NULL")
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/in:unpinned/) do |posts|
|
|
|
|
if @guardian.user
|
|
|
|
posts.where("topics.pinned_at IS NOT NULL AND topics.id IN (
|
|
|
|
SELECT topic_id FROM topic_users WHERE user_id = ? AND cleared_pinned_at IS NOT NULL
|
|
|
|
)", @guardian.user.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-13 04:26:53 -04:00
|
|
|
advanced_filter(/in:wiki/) do |posts,match|
|
|
|
|
posts.where(wiki: true)
|
|
|
|
end
|
|
|
|
|
2015-09-15 04:47:27 -04:00
|
|
|
advanced_filter(/badge:(.*)/) do |posts,match|
|
2015-09-15 03:21:46 -04:00
|
|
|
badge_id = Badge.where('name ilike ? OR id = ?', match, match.to_i).pluck(:id).first
|
|
|
|
if badge_id
|
|
|
|
posts.where('posts.user_id IN (SELECT ub.user_id FROM user_badges ub WHERE ub.badge_id = ?)', badge_id)
|
|
|
|
else
|
|
|
|
posts.where("1 = 0")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-22 22:14:06 -04:00
|
|
|
advanced_filter(/in:(likes|bookmarks)/) do |posts, match|
|
|
|
|
if @guardian.user
|
|
|
|
post_action_type = PostActionType.types[:like] if match == "likes"
|
|
|
|
post_action_type = PostActionType.types[:bookmark] if match == "bookmarks"
|
|
|
|
|
|
|
|
posts.where("posts.id IN (
|
|
|
|
SELECT pa.post_id FROM post_actions pa
|
|
|
|
WHERE pa.user_id = #{@guardian.user.id} AND
|
2015-06-22 23:58:23 -04:00
|
|
|
pa.post_action_type_id = #{post_action_type} AND
|
|
|
|
deleted_at IS NULL
|
2015-06-22 22:14:06 -04:00
|
|
|
)")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/in:posted/) do |posts|
|
|
|
|
posts.where("posts.user_id = #{@guardian.user.id}") if @guardian.user
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/in:(watching|tracking)/) do |posts,match|
|
|
|
|
if @guardian.user
|
|
|
|
level = TopicUser.notification_levels[match.to_sym]
|
|
|
|
posts.where("posts.topic_id IN (
|
|
|
|
SELECT tu.topic_id FROM topic_users tu
|
2017-03-08 07:37:29 -05:00
|
|
|
WHERE tu.user_id = :user_id AND
|
|
|
|
tu.notification_level >= :level
|
|
|
|
)", user_id: @guardian.user.id, level: level)
|
2015-06-22 22:14:06 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-08 09:46:23 -05:00
|
|
|
advanced_filter(/in:seen/) do |posts|
|
|
|
|
if @guardian.user
|
|
|
|
posts
|
|
|
|
.joins("INNER JOIN post_timings ON
|
|
|
|
post_timings.topic_id = posts.topic_id
|
|
|
|
AND post_timings.post_number = posts.post_number
|
|
|
|
AND post_timings.user_id = #{Post.sanitize(@guardian.user.id)}
|
|
|
|
")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
advanced_filter(/in:unseen/) do |posts|
|
|
|
|
if @guardian.user
|
|
|
|
posts
|
|
|
|
.joins("LEFT JOIN post_timings ON
|
|
|
|
post_timings.topic_id = posts.topic_id
|
|
|
|
AND post_timings.post_number = posts.post_number
|
|
|
|
AND post_timings.user_id = #{Post.sanitize(@guardian.user.id)}
|
|
|
|
")
|
|
|
|
.where("post_timings.user_id IS NULL")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-22 22:14:06 -04:00
|
|
|
advanced_filter(/category:(.+)/) do |posts,match|
|
2017-02-07 15:53:37 -05:00
|
|
|
exact = false
|
|
|
|
|
|
|
|
if match[0] == "="
|
|
|
|
exact = true
|
|
|
|
match = match[1..-1]
|
|
|
|
end
|
|
|
|
|
|
|
|
category_ids = Category.where('slug ilike ? OR name ilike ? OR id = ?',
|
|
|
|
match, match, match.to_i).pluck(:id)
|
2016-06-08 13:50:33 -04:00
|
|
|
if category_ids.present?
|
2017-02-07 15:53:37 -05:00
|
|
|
|
|
|
|
unless exact
|
|
|
|
category_ids +=
|
|
|
|
Category.where('parent_category_id = ?', category_ids.first).pluck(:id)
|
|
|
|
end
|
|
|
|
|
2016-06-08 13:50:33 -04:00
|
|
|
posts.where("topics.category_id IN (?)", category_ids)
|
2015-08-13 21:53:16 -04:00
|
|
|
else
|
|
|
|
posts.where("1 = 0")
|
|
|
|
end
|
2015-06-22 22:14:06 -04:00
|
|
|
end
|
|
|
|
|
2017-02-07 15:53:37 -05:00
|
|
|
advanced_filter(/^\#([a-zA-Z0-9\-:=]+)/) do |posts,match|
|
|
|
|
|
|
|
|
exact = true
|
|
|
|
|
2016-05-11 05:53:54 -04:00
|
|
|
slug = match.to_s.split(":")
|
|
|
|
if slug[1]
|
|
|
|
# sub category
|
|
|
|
parent_category_id = Category.where(slug: slug[0].downcase, parent_category_id: nil).pluck(:id).first
|
|
|
|
category_id = Category.where(slug: slug[1].downcase, parent_category_id: parent_category_id).pluck(:id).first
|
|
|
|
else
|
|
|
|
# main category
|
2017-02-07 15:53:37 -05:00
|
|
|
if slug[0][0] == "="
|
|
|
|
slug[0] = slug[0][1..-1]
|
|
|
|
else
|
|
|
|
exact = false
|
|
|
|
end
|
|
|
|
|
|
|
|
category_id = Category.where(slug: slug[0].downcase)
|
|
|
|
.order('case when parent_category_id is null then 0 else 1 end')
|
|
|
|
.pluck(:id)
|
|
|
|
.first
|
2016-05-11 05:53:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if category_id
|
2017-02-07 15:53:37 -05:00
|
|
|
category_ids = [category_id]
|
|
|
|
|
|
|
|
unless exact
|
|
|
|
category_ids +=
|
|
|
|
Category.where('parent_category_id = ?', category_id).pluck(:id)
|
|
|
|
end
|
|
|
|
posts.where("topics.category_id IN (?)", category_ids)
|
2016-05-11 05:53:54 -04:00
|
|
|
else
|
2016-06-02 08:41:24 -04:00
|
|
|
posts.where("topics.id IN (
|
|
|
|
SELECT DISTINCT(tt.topic_id)
|
|
|
|
FROM topic_tags tt, tags
|
|
|
|
WHERE tt.tag_id = tags.id
|
|
|
|
AND tags.name = ?
|
|
|
|
)", slug[0])
|
2016-05-11 05:53:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-15 03:39:14 -04:00
|
|
|
advanced_filter(/group:(.+)/) do |posts,match|
|
|
|
|
group_id = Group.where('name ilike ? OR (id = ? AND id > 0)', match, match.to_i).pluck(:id).first
|
|
|
|
if group_id
|
|
|
|
posts.where("posts.user_id IN (select gu.user_id from group_users gu where gu.group_id = ?)", group_id)
|
|
|
|
else
|
|
|
|
posts.where("1 = 0")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-22 22:14:06 -04:00
|
|
|
advanced_filter(/user:(.+)/) do |posts,match|
|
2015-11-18 15:06:59 -05:00
|
|
|
user_id = User.where(staged: false).where('username_lower = ? OR id = ?', match.downcase, match.to_i).pluck(:id).first
|
2015-08-13 21:53:16 -04:00
|
|
|
if user_id
|
|
|
|
posts.where("posts.user_id = #{user_id}")
|
|
|
|
else
|
|
|
|
posts.where("1 = 0")
|
|
|
|
end
|
2015-06-22 22:14:06 -04:00
|
|
|
end
|
|
|
|
|
2016-05-12 04:43:44 -04:00
|
|
|
advanced_filter(/^\@([a-zA-Z0-9_\-.]+)/) do |posts,match|
|
|
|
|
user_id = User.where(staged: false).where(username_lower: match.downcase).pluck(:id).first
|
|
|
|
if user_id
|
|
|
|
posts.where("posts.user_id = #{user_id}")
|
|
|
|
else
|
|
|
|
posts.where("1 = 0")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-14 08:27:02 -04:00
|
|
|
advanced_filter(/before:(.*)/) do |posts,match|
|
|
|
|
if date = Search.word_to_date(match)
|
|
|
|
posts.where("posts.created_at < ?", date)
|
|
|
|
else
|
|
|
|
posts
|
|
|
|
end
|
2015-06-22 23:21:50 -04:00
|
|
|
end
|
|
|
|
|
2016-03-14 08:27:02 -04:00
|
|
|
advanced_filter(/after:(.*)/) do |posts,match|
|
|
|
|
if date = Search.word_to_date(match)
|
|
|
|
posts.where("posts.created_at > ?", date)
|
|
|
|
else
|
|
|
|
posts
|
|
|
|
end
|
2015-06-22 23:21:50 -04:00
|
|
|
end
|
|
|
|
|
2017-05-31 10:36:15 -04:00
|
|
|
advanced_filter(/tags?:([a-zA-Z0-9,\-_|]+)/) do |posts, match|
|
|
|
|
if match.include?(',')
|
|
|
|
tags = match.split(",")
|
2016-04-25 15:55:15 -04:00
|
|
|
|
2017-05-31 10:36:15 -04:00
|
|
|
# TODO use ts_query function
|
|
|
|
posts.where("topics.id IN (
|
|
|
|
SELECT DISTINCT(tt.topic_id)
|
|
|
|
FROM topic_tags tt, tags
|
|
|
|
WHERE tt.tag_id = tags.id
|
|
|
|
GROUP BY tt.topic_id
|
|
|
|
HAVING to_tsvector('simple',array_to_string(array_agg(tags.name), ' ')) @@ to_tsquery('simple', ?)
|
|
|
|
)", tags.join('&'))
|
|
|
|
else
|
|
|
|
tags = match.split("|")
|
|
|
|
|
|
|
|
posts.where("topics.id IN (
|
2016-05-04 14:02:47 -04:00
|
|
|
SELECT DISTINCT(tt.topic_id)
|
|
|
|
FROM topic_tags tt, tags
|
|
|
|
WHERE tt.tag_id = tags.id
|
|
|
|
AND tags.name in (?)
|
2016-04-25 15:55:15 -04:00
|
|
|
)", tags)
|
2017-05-31 10:36:15 -04:00
|
|
|
end
|
2016-04-25 15:55:15 -04:00
|
|
|
end
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
private
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2015-06-22 22:14:06 -04:00
|
|
|
|
2014-09-03 07:54:10 -04:00
|
|
|
def process_advanced_search!(term)
|
2014-10-17 23:54:11 -04:00
|
|
|
|
2015-09-15 03:21:46 -04:00
|
|
|
term.to_s.scan(/(([^" \t\n\x0B\f\r]+)?(("[^"]+")?))/).to_a.map do |(word,_)|
|
|
|
|
next if word.blank?
|
2015-06-22 22:14:06 -04:00
|
|
|
|
|
|
|
found = false
|
|
|
|
|
|
|
|
Search.advanced_filters.each do |matcher, block|
|
2015-09-15 03:21:46 -04:00
|
|
|
cleaned = word.gsub(/["']/,"")
|
|
|
|
if cleaned =~ matcher
|
2015-06-22 22:14:06 -04:00
|
|
|
(@filters ||= []) << [block, $1]
|
|
|
|
found = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 11:24:41 -04:00
|
|
|
if word == 'order:latest' || word == 'l'
|
2014-09-03 08:10:18 -04:00
|
|
|
@order = :latest
|
|
|
|
nil
|
2017-03-29 13:33:23 -04:00
|
|
|
elsif word == 'order:latest_topic'
|
|
|
|
@order = :latest_topic
|
|
|
|
nil
|
2015-06-26 03:36:28 -04:00
|
|
|
elsif word =~ /topic:(\d+)/
|
|
|
|
topic_id = $1.to_i
|
|
|
|
if topic_id > 1
|
|
|
|
topic = Topic.find_by(id: topic_id)
|
|
|
|
if @guardian.can_see?(topic)
|
|
|
|
@search_context = topic
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
2014-10-18 00:38:58 -04:00
|
|
|
elsif word == 'order:views'
|
|
|
|
@order = :views
|
|
|
|
nil
|
2015-09-15 03:21:46 -04:00
|
|
|
elsif word == 'order:likes'
|
|
|
|
@order = :likes
|
|
|
|
nil
|
2014-12-04 18:22:39 -05:00
|
|
|
elsif word == 'in:private'
|
|
|
|
@search_pms = true
|
|
|
|
nil
|
2016-08-19 14:58:51 -04:00
|
|
|
elsif word =~ /^private_messages:(.+)$/
|
|
|
|
@search_pms = true
|
|
|
|
nil
|
2014-09-03 07:54:10 -04:00
|
|
|
else
|
2015-06-22 22:14:06 -04:00
|
|
|
found ? nil : word
|
2014-09-03 07:54:10 -04:00
|
|
|
end
|
|
|
|
end.compact.join(' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
|
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
|
2015-06-22 22:14:06 -04:00
|
|
|
user_search if @term.present?
|
|
|
|
category_search if @term.present?
|
2014-06-17 03:53:45 -04:00
|
|
|
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'
|
2014-09-02 05:15:08 -04:00
|
|
|
expected_topics -= @results.posts.length
|
2013-05-23 14:26:51 -04:00
|
|
|
if expected_topics > 0
|
2013-05-29 17:52:09 -04:00
|
|
|
extra_posts = posts_query(expected_topics * Search.burst_factor)
|
2014-09-02 05:15:08 -04:00
|
|
|
extra_posts = extra_posts.where("posts.topic_id NOT in (?)", @results.posts.map(&:topic_id)) if @results.posts.present?
|
|
|
|
extra_posts.each do |post|
|
|
|
|
@results.add(post)
|
|
|
|
expected_topics -= 1
|
|
|
|
break if expected_topics == 0
|
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-09-02 05:15:08 -04:00
|
|
|
post = Post.find_by(topic_id: id, post_number: 1)
|
|
|
|
return nil unless @guardian.can_see?(post)
|
2013-05-13 17:04:41 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
@results.add(post)
|
2013-05-23 14:26:51 -04:00
|
|
|
@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
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
categories.each do |category|
|
|
|
|
@results.add(category)
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
def user_search
|
2015-10-28 14:56:08 -04:00
|
|
|
return if SiteSetting.hide_user_profiles_from_public && !@guardian.user
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
users = User.includes(:user_search_data)
|
2015-11-04 17:04:37 -05:00
|
|
|
.references(:user_search_data)
|
2015-11-18 15:06:59 -05:00
|
|
|
.where(active: true)
|
|
|
|
.where(staged: false)
|
2015-11-04 17:04:37 -05: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-05-22 14:36:14 -04:00
|
|
|
|
2014-09-02 05:15:08 -04:00
|
|
|
users.each do |user|
|
|
|
|
@results.add(user)
|
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
|
|
|
|
2014-08-22 16:55:19 -04:00
|
|
|
def posts_query(limit, opts=nil)
|
|
|
|
opts ||= {}
|
2015-09-25 11:43:04 -04:00
|
|
|
posts = Post.where(post_type: Topic.visible_post_types(@guardian.user))
|
2014-12-03 21:46:52 -05:00
|
|
|
.joins(:post_search_data, :topic)
|
|
|
|
.joins("LEFT JOIN categories ON categories.id = topics.category_id")
|
2013-05-23 14:26:51 -04:00
|
|
|
.where("topics.deleted_at" => nil)
|
|
|
|
.where("topics.visible")
|
2014-12-03 21:46:52 -05:00
|
|
|
|
2015-02-18 20:56:49 -05:00
|
|
|
is_topic_search = @search_context.present? && @search_context.is_a?(Topic)
|
|
|
|
|
|
|
|
if opts[:private_messages] || (is_topic_search && @search_context.private_message?)
|
2014-12-03 21:46:52 -05:00
|
|
|
posts = posts.where("topics.archetype = ?", Archetype.private_message)
|
|
|
|
|
|
|
|
unless @guardian.is_admin?
|
2017-05-11 15:58:43 -04:00
|
|
|
posts = posts.private_posts_for_user(@guardian.user)
|
2014-12-03 21:46:52 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
posts = posts.where("topics.archetype <> ?", Archetype.private_message)
|
|
|
|
end
|
2013-08-01 20:31:36 -04:00
|
|
|
|
2015-06-22 22:14:06 -04:00
|
|
|
if @term.present?
|
|
|
|
if is_topic_search
|
2016-07-25 01:06:25 -04:00
|
|
|
|
|
|
|
term_without_quote = @term
|
|
|
|
if @term =~ /"(.+)"/
|
|
|
|
term_without_quote = $1
|
|
|
|
end
|
|
|
|
|
|
|
|
if @term =~ /'(.+)'/
|
|
|
|
term_without_quote = $1
|
|
|
|
end
|
|
|
|
|
2015-06-22 22:14:06 -04:00
|
|
|
posts = posts.joins('JOIN users u ON u.id = posts.user_id')
|
2016-07-25 01:06:25 -04:00
|
|
|
posts = posts.where("posts.raw || ' ' || u.username || ' ' || COALESCE(u.name, '') ilike ?", "%#{term_without_quote}%")
|
2015-06-22 22:14:06 -04:00
|
|
|
else
|
|
|
|
posts = posts.where("post_search_data.search_data @@ #{ts_query}")
|
2016-08-10 15:40:58 -04:00
|
|
|
|
2015-08-10 03:41:14 -04:00
|
|
|
exact_terms = @term.scan(/"([^"]+)"/).flatten
|
|
|
|
exact_terms.each do |exact|
|
|
|
|
posts = posts.where("posts.raw ilike ?", "%#{exact}%")
|
|
|
|
end
|
2014-10-18 00:19:08 -04:00
|
|
|
end
|
2015-06-22 22:14:06 -04:00
|
|
|
end
|
2014-10-18 00:19:08 -04:00
|
|
|
|
2015-06-22 22:14:06 -04:00
|
|
|
@filters.each do |block, match|
|
|
|
|
if block.arity == 1
|
|
|
|
posts = instance_exec(posts, &block) || posts
|
|
|
|
else
|
|
|
|
posts = instance_exec(posts, match, &block) || posts
|
2014-10-18 00:34:05 -04:00
|
|
|
end
|
2015-06-22 22:14:06 -04:00
|
|
|
end if @filters
|
2014-10-18 00:19:08 -04:00
|
|
|
|
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-12-03 21:46:52 -05:00
|
|
|
|
|
|
|
if opts[:private_messages]
|
2017-05-11 15:58:43 -04:00
|
|
|
posts = posts.private_posts_for_user(@search_context)
|
2014-12-03 21:46:52 -05:00
|
|
|
else
|
|
|
|
posts = posts.where("posts.user_id = #{@search_context.id}")
|
|
|
|
end
|
|
|
|
|
2013-05-24 14:03:45 -04:00
|
|
|
elsif @search_context.is_a?(Category)
|
2017-03-10 15:58:47 -05:00
|
|
|
category_ids = [@search_context.id] + Category.where(parent_category_id: @search_context.id).pluck(:id)
|
|
|
|
posts = posts.where("topics.category_id in (?)", category_ids)
|
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}")
|
2017-05-24 11:24:41 -04:00
|
|
|
.order("posts.post_number #{@order == :latest ? "DESC" : ""}")
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-09-20 17:59:52 -04:00
|
|
|
if @order == :latest || (@term.blank? && !@order)
|
2014-09-03 08:10:18 -04:00
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(posts.created_at) DESC")
|
|
|
|
else
|
2017-05-24 11:24:41 -04:00
|
|
|
posts = posts.reorder("posts.created_at DESC")
|
2014-09-03 08:10:18 -04:00
|
|
|
end
|
2017-03-29 13:33:23 -04:00
|
|
|
elsif @order == :latest_topic
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(topics.created_at) DESC")
|
|
|
|
else
|
|
|
|
posts = posts.order("topics.created_at DESC")
|
|
|
|
end
|
2014-10-18 00:38:58 -04:00
|
|
|
elsif @order == :views
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(topics.views) DESC")
|
|
|
|
else
|
|
|
|
posts = posts.order("topics.views DESC")
|
|
|
|
end
|
2015-09-15 03:21:46 -04:00
|
|
|
elsif @order == :likes
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(posts.like_count) DESC")
|
|
|
|
else
|
|
|
|
posts = posts.order("posts.like_count DESC")
|
|
|
|
end
|
2014-08-22 16:55:19 -04:00
|
|
|
else
|
2014-09-03 08:10:18 -04:00
|
|
|
posts = posts.order("TS_RANK_CD(TO_TSVECTOR(#{query_locale}, topics.title), #{ts_query}) DESC")
|
|
|
|
|
|
|
|
data_ranking = "TS_RANK_CD(post_search_data.search_data, #{ts_query})"
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("SUM(#{data_ranking}) DESC")
|
|
|
|
else
|
|
|
|
posts = posts.order("#{data_ranking} DESC")
|
|
|
|
end
|
|
|
|
posts = posts.order("topics.bumped_at DESC")
|
2014-08-22 16:55:19 -04:00
|
|
|
end
|
2013-05-24 16:17:09 -04:00
|
|
|
|
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
|
2017-05-24 11:24:41 -04:00
|
|
|
|
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
|
2016-11-24 19:34:59 -05:00
|
|
|
"'#{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 = "&")
|
2015-08-10 03:41:14 -04:00
|
|
|
|
|
|
|
data = Post.exec_sql("SELECT to_tsvector(:locale, :term)",
|
2016-10-06 21:40:57 -04:00
|
|
|
locale: 'simple',
|
2015-08-10 03:41:14 -04:00
|
|
|
term: term
|
|
|
|
).values[0][0]
|
|
|
|
|
2014-07-04 19:11:41 -04:00
|
|
|
locale = Post.sanitize(locale) if locale
|
2015-08-10 03:41:14 -04:00
|
|
|
all_terms = data.scan(/'([^']+)'\:\d+/).flatten
|
2015-08-13 03:55:10 -04:00
|
|
|
all_terms.map! do |t|
|
|
|
|
t.split(/[\)\(&']/)[0]
|
|
|
|
end.compact!
|
|
|
|
|
2015-08-18 19:27:47 -04:00
|
|
|
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)
|
2015-08-10 03:41:14 -04:00
|
|
|
@ts_query_cache ||= {}
|
|
|
|
@ts_query_cache[(locale || query_locale) + " " + @term] ||= Search.ts_query(@term, locale)
|
2013-05-23 11:13:23 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 13:04:46 -04:00
|
|
|
def wrap_rows(query)
|
|
|
|
"SELECT *, row_number() over() row_number FROM (#{query.to_sql}) xxx"
|
|
|
|
end
|
2014-08-28 03:15:53 -04:00
|
|
|
|
2016-08-12 13:04:46 -04:00
|
|
|
def aggregate_post_sql(opts)
|
2015-09-18 03:16:37 -04:00
|
|
|
min_or_max = @order == :latest ? "max" : "min"
|
|
|
|
|
2016-08-12 13:04:46 -04:00
|
|
|
query =
|
2015-09-18 03:49:57 -04:00
|
|
|
if @order == :likes
|
|
|
|
# likes are a pain to aggregate so skip
|
|
|
|
posts_query(@limit, private_messages: opts[:private_messages])
|
2017-03-08 09:46:23 -05:00
|
|
|
.select('topics.id', "posts.post_number")
|
2015-09-18 03:49:57 -04:00
|
|
|
else
|
2016-08-12 13:04:46 -04:00
|
|
|
posts_query(@limit, aggregate_search: true, private_messages: opts[:private_messages])
|
2017-03-08 09:46:23 -05:00
|
|
|
.select('topics.id', "#{min_or_max}(posts.post_number) post_number")
|
2015-09-18 03:16:37 -04:00
|
|
|
.group('topics.id')
|
2015-09-18 03:49:57 -04:00
|
|
|
end
|
2014-09-01 03:04:40 -04:00
|
|
|
|
2016-08-12 13:04:46 -04:00
|
|
|
min_id = Search.min_post_id
|
|
|
|
if min_id > 0
|
|
|
|
low_set = query.dup.where("post_search_data.post_id < #{min_id}")
|
|
|
|
high_set = query.where("post_search_data.post_id >= #{min_id}")
|
|
|
|
|
|
|
|
return { default: wrap_rows(high_set), remaining: wrap_rows(low_set) }
|
|
|
|
end
|
|
|
|
|
2014-09-02 22:13:13 -04:00
|
|
|
# double wrapping so we get correct row numbers
|
2016-08-12 13:04:46 -04:00
|
|
|
{ default: wrap_rows(query) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def aggregate_posts(post_sql)
|
|
|
|
return [] unless post_sql
|
2014-09-02 22:13:13 -04:00
|
|
|
|
2016-08-12 13:04:46 -04:00
|
|
|
Post.includes(:topic => :category)
|
|
|
|
.includes(:user)
|
|
|
|
.joins("JOIN (#{post_sql}) x ON x.id = posts.topic_id AND x.post_number = posts.post_number")
|
|
|
|
.order('row_number')
|
|
|
|
end
|
|
|
|
|
|
|
|
def aggregate_search(opts = {})
|
|
|
|
post_sql = aggregate_post_sql(opts)
|
2014-09-01 03:04:40 -04:00
|
|
|
|
2016-08-12 17:18:12 -04:00
|
|
|
added = 0
|
2017-03-08 09:46:23 -05:00
|
|
|
|
2016-08-12 17:18:12 -04:00
|
|
|
aggregate_posts(post_sql[:default]).each do |p|
|
|
|
|
@results.add(p)
|
|
|
|
added += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if added < @limit
|
2016-08-12 13:04:46 -04:00
|
|
|
aggregate_posts(post_sql[:remaining]).each {|p| @results.add(p) }
|
2014-08-22 16:55:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-03 21:46:52 -05:00
|
|
|
def private_messages_search
|
|
|
|
raise Discourse::InvalidAccess.new("anonymous can not search PMs") unless @guardian.user
|
|
|
|
|
|
|
|
aggregate_search(private_messages: true)
|
|
|
|
end
|
|
|
|
|
2013-05-23 14:26:51 -04:00
|
|
|
def topic_search
|
2014-09-02 05:15:08 -04:00
|
|
|
if @search_context.is_a?(Topic)
|
2016-08-09 13:41:25 -04:00
|
|
|
posts = posts_query(@limit).where('posts.topic_id = ?', @search_context.id)
|
|
|
|
.includes(:topic => :category)
|
|
|
|
.includes(:user)
|
2014-09-02 05:15:08 -04:00
|
|
|
posts.each do |post|
|
|
|
|
@results.add(post)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
aggregate_search
|
2013-05-23 11:13:23 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|