2013-05-23 14:26:51 -04:00
|
|
|
require_dependency 'search/grouped_search_results'
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
class Search
|
2017-08-16 07:38:34 -04:00
|
|
|
INDEX_VERSION = 1.freeze
|
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
|
|
|
|
|
2017-07-31 15:28:48 -04:00
|
|
|
def self.ts_config(locale = SiteSetting.default_locale)
|
|
|
|
# if adding a text search configuration, you should check PG beforehand:
|
|
|
|
# SELECT cfgname FROM pg_ts_config;
|
|
|
|
# As an aside, dictionaries can be listed by `\dFd`, the
|
|
|
|
# physical locations are in /usr/share/postgresql/<version>/tsearch_data.
|
|
|
|
# But it may not appear there based on pg extension configuration.
|
2014-06-24 03:10:56 -04:00
|
|
|
# base docker config
|
|
|
|
#
|
2017-07-31 15:28:48 -04:00
|
|
|
case locale.to_sym
|
2017-07-27 21:20:09 -04:00
|
|
|
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'
|
2017-07-31 15:28:48 -04:00
|
|
|
else 'simple' # use the 'simple' stemmer for other languages
|
2013-02-28 14:14:22 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-31 15:28:48 -04:00
|
|
|
def self.prepare_data(search_data, purpose = :query)
|
2014-06-24 03:10:56 -04:00
|
|
|
data = search_data.squish
|
2017-07-31 15:28:48 -04:00
|
|
|
# TODO cppjieba_rb 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
|
2017-07-31 15:28:48 -04:00
|
|
|
unless defined? CppjiebaRb
|
|
|
|
require 'cppjieba_rb'
|
2014-06-24 03:10:56 -04:00
|
|
|
end
|
2017-07-31 15:28:48 -04:00
|
|
|
mode = (purpose == :query ? :query : :mix)
|
|
|
|
data = CppjiebaRb.segment(search_data, mode: mode)
|
|
|
|
data = CppjiebaRb.filter_stop_word(data).join(' ')
|
2014-06-24 03:10:56 -04:00
|
|
|
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
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
return if day == 0 || month == 0 || day > 31 || month > 12
|
2016-03-14 08:27:02 -04:00
|
|
|
|
|
|
|
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
|
2017-07-27 21:20:09 -04:00
|
|
|
.order('id desc')
|
|
|
|
.offset(SiteSetting.search_recent_posts_size - 1)
|
|
|
|
.limit(2)
|
|
|
|
.pluck(:id)
|
2016-08-12 00:36:38 -04:00
|
|
|
|
|
|
|
has_more ? offset : 0
|
2016-08-10 15:40:58 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.min_post_id(opts = nil)
|
2016-08-10 15:40:58 -04:00
|
|
|
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
|
2017-06-21 15:51:15 -04:00
|
|
|
attr_reader :clean_term
|
2016-08-09 14:48:39 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(term, opts = nil)
|
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
|
2015-07-27 02:13:11 -04:00
|
|
|
@blurb_length = @opts[:blurb_length]
|
2016-08-09 14:48:39 -04:00
|
|
|
@valid = true
|
2017-07-20 12:12:34 -04:00
|
|
|
@page = @opts[:page]
|
2014-12-04 18:22:39 -05:00
|
|
|
|
2017-06-07 02:23:48 -04:00
|
|
|
# Removes any zero-width characters from search terms
|
|
|
|
term.to_s.gsub!(/[\u200B-\u200D\uFEFF]/, '')
|
2017-06-21 15:51:15 -04:00
|
|
|
@clean_term = term.to_s.dup
|
|
|
|
|
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?
|
2017-06-07 02:23:48 -04:00
|
|
|
@term = Search.prepare_data(term)
|
2015-06-26 03:36:28 -04:00
|
|
|
@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
|
|
|
|
|
2017-07-17 11:57:13 -04:00
|
|
|
@results = GroupedSearchResults.new(
|
|
|
|
@opts[:type_filter],
|
|
|
|
clean_term,
|
|
|
|
@search_context,
|
|
|
|
@include_blurbs,
|
|
|
|
@blurb_length
|
|
|
|
)
|
2014-09-02 05:15:08 -04:00
|
|
|
end
|
|
|
|
|
2017-07-20 12:12:34 -04:00
|
|
|
def limit
|
|
|
|
if @opts[:type_filter].present?
|
|
|
|
Search.per_filter + 1
|
|
|
|
else
|
|
|
|
Search.per_facet + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def offset
|
|
|
|
if @page && @opts[:type_filter].present?
|
|
|
|
(@page - 1) * Search.per_filter
|
|
|
|
else
|
|
|
|
0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-09 14:48:39 -04:00
|
|
|
def valid?
|
|
|
|
@valid
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.execute(term, opts = nil)
|
2014-09-02 05:15:08 -04:00
|
|
|
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
|
2017-07-13 13:34:31 -04:00
|
|
|
if SiteSetting.log_search_queries?
|
2017-07-17 11:57:13 -04:00
|
|
|
status, search_log_id = SearchLog.log(
|
2017-07-13 13:34:31 -04:00
|
|
|
term: @term,
|
|
|
|
search_type: @opts[:search_type],
|
|
|
|
ip_address: @opts[:ip_address],
|
|
|
|
user_id: @opts[:user_id]
|
|
|
|
)
|
2017-07-17 11:57:13 -04:00
|
|
|
@results.search_log_id = search_log_id unless status == :error
|
2017-07-13 13:34:31 -04:00
|
|
|
end
|
2016-08-09 14:48:39 -04:00
|
|
|
|
2017-07-25 20:51:44 -04:00
|
|
|
unless @filters.present? || @opts[:search_for_id]
|
2016-08-09 14:48:39 -04:00
|
|
|
min_length = @opts[:min_search_term_length] || SiteSetting.min_search_term_length
|
2017-07-27 21:20:09 -04:00
|
|
|
terms = (@term || '').split(/\s(?=(?:[^"]|"[^"]*")*$)/).reject { |t| t.length < min_length }
|
2016-08-09 14:48:39 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.advanced_filter(trigger, &block)
|
2015-06-22 22:14:06 -04:00
|
|
|
(@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
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
advanced_filter(/in:wiki/) do |posts, match|
|
2016-05-13 04:26:53 -04:00
|
|
|
posts.where(wiki: true)
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -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
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
advanced_filter(/in:(watching|tracking)/) do |posts, match|
|
2015-06-22 22:14:06 -04:00
|
|
|
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
|
|
|
|
|
2017-06-09 07:16:50 -04:00
|
|
|
advanced_filter(/with:images/) do |posts|
|
2017-06-07 14:13:36 -04:00
|
|
|
posts.where("posts.image_url IS NOT NULL")
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -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-07-27 21:20:09 -04:00
|
|
|
advanced_filter(/^\#([a-zA-Z0-9\-:=]+)/) do |posts, match|
|
2017-02-07 15:53:37 -05:00
|
|
|
|
|
|
|
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
|
2017-08-01 18:15:04 -04:00
|
|
|
# try a possible tag match
|
|
|
|
tag_id = Tag.where(name: slug[0]).pluck(:id).first
|
|
|
|
if (tag_id)
|
|
|
|
posts.where("topics.id IN (
|
|
|
|
SELECT DISTINCT(tt.topic_id)
|
|
|
|
FROM topic_tags tt
|
|
|
|
WHERE tt.tag_id = ?
|
|
|
|
)", tag_id)
|
|
|
|
else
|
|
|
|
# a bit yucky but we got to add the term back in
|
|
|
|
if match.to_s.length >= SiteSetting.min_search_term_length
|
|
|
|
posts.where("posts.id IN (
|
|
|
|
SELECT post_id FROM post_search_data pd1
|
|
|
|
WHERE pd1.search_data @@ #{Search.ts_query("##{match}")})")
|
|
|
|
end
|
|
|
|
end
|
2016-05-11 05:53:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
advanced_filter(/group:(.+)/) do |posts, match|
|
2015-09-15 03:39:14 -04:00
|
|
|
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
|
|
|
|
|
2017-07-27 21:20:09 -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
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
advanced_filter(/^\@([a-zA-Z0-9_\-.]+)/) do |posts, match|
|
2016-05-12 04:43:44 -04:00
|
|
|
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
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
advanced_filter(/before:(.*)/) do |posts, match|
|
2016-03-14 08:27:02 -04:00
|
|
|
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-07-27 21:20:09 -04:00
|
|
|
advanced_filter(/after:(.*)/) do |posts, match|
|
2016-03-14 08:27:02 -04:00
|
|
|
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 11:32:29 -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
|
|
|
posts.where("topics.id IN (
|
2017-05-31 11:32:29 -04:00
|
|
|
SELECT tt.topic_id
|
2017-05-31 10:36:15 -04:00
|
|
|
FROM topic_tags tt, tags
|
|
|
|
WHERE tt.tag_id = tags.id
|
|
|
|
GROUP BY tt.topic_id
|
2017-07-31 15:28:48 -04:00
|
|
|
HAVING to_tsvector(#{default_ts_config}, array_to_string(array_agg(tags.name), ' ')) @@ to_tsquery(#{default_ts_config}, ?)
|
2017-05-31 10:36:15 -04:00
|
|
|
)", tags.join('&'))
|
|
|
|
else
|
2017-05-31 11:32:29 -04:00
|
|
|
tags = match.split(",")
|
2017-05-31 10:36:15 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-06-20 15:20:06 -04:00
|
|
|
advanced_filter(/filetypes?:([a-zA-Z0-9,\-_]+)/) do |posts, match|
|
2017-07-06 13:11:32 -04:00
|
|
|
file_extensions = match.split(",").map(&:downcase)
|
2017-06-20 15:20:06 -04:00
|
|
|
|
|
|
|
posts.where("posts.id IN (
|
|
|
|
SELECT post_id FROM topic_links
|
2017-07-06 13:11:32 -04:00
|
|
|
WHERE extension IN (:file_extensions)
|
2017-07-04 11:50:08 -04:00
|
|
|
UNION
|
|
|
|
SELECT post_uploads.post_id FROM uploads
|
|
|
|
JOIN post_uploads ON post_uploads.upload_id = uploads.id
|
2017-07-06 13:11:32 -04:00
|
|
|
WHERE lower(uploads.extension) IN (:file_extensions)
|
2017-07-31 11:59:16 -04:00
|
|
|
)", file_extensions: file_extensions)
|
2017-06-20 15:20:06 -04:00
|
|
|
end
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
private
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2014-09-03 07:54:10 -04:00
|
|
|
def process_advanced_search!(term)
|
2014-10-17 23:54:11 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
term.to_s.scan(/(([^" \t\n\x0B\f\r]+)?(("[^"]+")?))/).to_a.map do |(word, _)|
|
2015-09-15 03:21:46 -04:00
|
|
|
next if word.blank?
|
2015-06-22 22:14:06 -04:00
|
|
|
|
|
|
|
found = false
|
|
|
|
|
|
|
|
Search.advanced_filters.each do |matcher, block|
|
2017-07-27 21:20:09 -04:00
|
|
|
cleaned = word.gsub(/["']/, "")
|
2015-09-15 03:21:46 -04:00
|
|
|
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
|
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)
|
2017-07-27 21:20:09 -04:00
|
|
|
.where("category_search_data.search_data @@ #{ts_query}")
|
|
|
|
.references(:category_search_data)
|
|
|
|
.order("topics_month DESC")
|
|
|
|
.secured(@guardian)
|
2017-07-31 20:28:16 -04:00
|
|
|
.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)
|
2017-07-27 21:20:09 -04:00
|
|
|
.references(:user_search_data)
|
|
|
|
.where(active: true)
|
|
|
|
.where(staged: false)
|
|
|
|
.where("user_search_data.search_data @@ #{ts_query("simple")}")
|
|
|
|
.order("CASE WHEN username_lower = '#{@original_term.downcase}' THEN 0 ELSE 1 END")
|
|
|
|
.order("last_posted_at DESC")
|
2017-07-31 20:28:16 -04:00
|
|
|
.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
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def posts_query(limit, opts = nil)
|
2014-08-22 16:55:19 -04:00
|
|
|
opts ||= {}
|
2015-09-25 11:43:04 -04:00
|
|
|
posts = Post.where(post_type: Topic.visible_post_types(@guardian.user))
|
2017-07-27 21:20:09 -04:00
|
|
|
.joins(:post_search_data, :topic)
|
|
|
|
.joins("LEFT JOIN categories ON categories.id = topics.category_id")
|
|
|
|
.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?)
|
2017-07-27 21:20:09 -04:00
|
|
|
posts = posts.where("topics.archetype = ?", Archetype.private_message)
|
2014-12-03 21:46:52 -05:00
|
|
|
|
|
|
|
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
|
2017-07-27 21:20:09 -04:00
|
|
|
posts = posts.where("topics.archetype <> ?", Archetype.private_message)
|
2014-12-03 21:46:52 -05:00
|
|
|
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}")
|
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-07-27 21:20:09 -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
|
2017-07-31 15:28:48 -04:00
|
|
|
posts = posts.order("TS_RANK_CD(TO_TSVECTOR(#{default_ts_config}, topics.title), #{ts_query}) DESC")
|
2014-09-03 08:10:18 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-07-20 12:12:34 -04:00
|
|
|
posts = posts.offset(offset)
|
2013-05-24 16:17:09 -04:00
|
|
|
posts.limit(limit)
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
|
|
|
|
2017-07-31 15:28:48 -04:00
|
|
|
def self.default_ts_config
|
|
|
|
"'#{Search.ts_config}'"
|
2013-05-23 11:13:23 -04:00
|
|
|
end
|
|
|
|
|
2017-07-31 15:28:48 -04:00
|
|
|
def default_ts_config
|
|
|
|
self.class.default_ts_config
|
2014-05-15 10:31:45 -04:00
|
|
|
end
|
|
|
|
|
2017-07-31 15:28:48 -04:00
|
|
|
def self.ts_query(term, ts_config = nil, joiner = "&")
|
2015-08-10 03:41:14 -04:00
|
|
|
|
2017-07-31 15:28:48 -04:00
|
|
|
data = Post.exec_sql("SELECT TO_TSVECTOR(:config, :term)",
|
|
|
|
config: 'simple',
|
|
|
|
term: term).values[0][0]
|
2015-08-10 03:41:14 -04:00
|
|
|
|
2017-07-31 15:28:48 -04:00
|
|
|
ts_config = Post.sanitize(ts_config) if ts_config
|
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!
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
query = Post.sanitize(all_terms.map { |t| "'#{PG::Connection.escape_string(t)}':*" }.join(" #{joiner} "))
|
2017-07-31 15:28:48 -04:00
|
|
|
"TO_TSQUERY(#{ts_config || default_ts_config}, #{query})"
|
2014-05-15 10:31:45 -04:00
|
|
|
end
|
|
|
|
|
2017-07-31 15:28:48 -04:00
|
|
|
def ts_query(ts_config = nil)
|
2015-08-10 03:41:14 -04:00
|
|
|
@ts_query_cache ||= {}
|
2017-07-31 15:28:48 -04:00
|
|
|
@ts_query_cache["#{ts_config || default_ts_config} #{@term}"] ||= Search.ts_query(@term, ts_config)
|
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
|
2017-07-20 12:12:34 -04:00
|
|
|
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
|
2017-07-20 12:12:34 -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
|
|
|
|
2017-05-30 19:14:09 -04:00
|
|
|
posts_eager_loads(Post)
|
2016-08-12 13:04:46 -04:00
|
|
|
.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
|
|
|
|
|
2017-07-31 20:28:16 -04:00
|
|
|
if added < limit
|
2017-07-27 21:20:09 -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)
|
2017-07-20 12:12:34 -04:00
|
|
|
posts = posts_eager_loads(posts_query(limit))
|
2017-05-30 19:14:09 -04:00
|
|
|
.where('posts.topic_id = ?', @search_context.id)
|
|
|
|
|
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
|
|
|
|
|
2017-05-30 19:14:09 -04:00
|
|
|
def posts_eager_loads(query)
|
|
|
|
query = query.includes(:user)
|
|
|
|
topic_eager_loads = [:category]
|
|
|
|
|
|
|
|
if SiteSetting.tagging_enabled
|
|
|
|
topic_eager_loads << :tags
|
|
|
|
end
|
|
|
|
|
|
|
|
query.includes(topic: topic_eager_loads)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|