2013-05-23 14:26:51 -04:00
|
|
|
require_dependency 'search/grouped_search_results'
|
|
|
|
|
2013-05-22 14:36:14 -04:00
|
|
|
class Search
|
2018-02-19 22:41:00 -05:00
|
|
|
INDEX_VERSION = 2.freeze
|
2018-10-22 21:10:33 -04:00
|
|
|
DIACRITICS ||= /([\u0300-\u036f]|[\u1AB0-\u1AFF]|[\u1DC0-\u1DFF]|[\u20D0-\u20FF])/
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
def self.per_facet
|
|
|
|
5
|
|
|
|
end
|
|
|
|
|
2018-10-22 21:10:33 -04:00
|
|
|
def self.strip_diacritics(str)
|
|
|
|
s = str.unicode_normalize(:nfkd)
|
|
|
|
s.gsub!(DIACRITICS, "")
|
|
|
|
s.strip!
|
|
|
|
s
|
|
|
|
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
|
2017-08-25 11:52:18 -04:00
|
|
|
%w(topic category user private_messages tags)
|
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)
|
2018-11-06 17:41:55 -05:00
|
|
|
purpose ||= :query
|
2014-06-24 03:10:56 -04:00
|
|
|
|
2018-11-06 17:41:55 -05:00
|
|
|
data = search_data.dup
|
2014-06-24 03:10:56 -04:00
|
|
|
data.force_encoding("UTF-8")
|
2018-11-06 17:41:55 -05:00
|
|
|
if purpose != :topic
|
|
|
|
# TODO cppjieba_rb is designed for chinese, we need something else for Japanese
|
|
|
|
# Korean appears to be safe cause words are already space seperated
|
|
|
|
# For Japanese we should investigate using kakasi
|
|
|
|
if ['zh_TW', 'zh_CN', 'ja'].include?(SiteSetting.default_locale) || SiteSetting.search_tokenize_chinese_japanese_korean
|
|
|
|
require 'cppjieba_rb' unless defined? CppjiebaRb
|
|
|
|
mode = (purpose == :query ? :query : :mix)
|
|
|
|
data = CppjiebaRb.segment(search_data, mode: mode)
|
|
|
|
data = CppjiebaRb.filter_stop_word(data).join(' ')
|
|
|
|
else
|
|
|
|
data.squish!
|
|
|
|
end
|
|
|
|
|
|
|
|
if SiteSetting.search_ignore_accents
|
|
|
|
data = strip_diacritics(data)
|
|
|
|
end
|
2018-10-22 21:10:33 -04:00
|
|
|
end
|
2014-06-24 03:10:56 -04:00
|
|
|
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
|
|
|
|
2018-05-06 23:38:04 -04:00
|
|
|
return begin
|
|
|
|
Time.zone.parse("#{year}-#{month}-#{day}")
|
|
|
|
rescue ArgumentError
|
|
|
|
end
|
2016-03-14 08:27:02 -04:00
|
|
|
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
|
|
|
|
2018-08-23 20:00:51 -04:00
|
|
|
term = term.to_s.dup
|
|
|
|
|
2017-06-07 02:23:48 -04:00
|
|
|
# Removes any zero-width characters from search terms
|
2018-08-23 20:00:51 -04:00
|
|
|
term.gsub!(/[\u200B-\u200D\uFEFF]/, '')
|
2017-12-12 00:47:28 -05:00
|
|
|
# Replace curly quotes to regular quotes
|
2018-08-23 20:00:51 -04:00
|
|
|
term.gsub!(/[\u201c\u201d]/, '"')
|
|
|
|
|
|
|
|
@clean_term = term
|
2017-06-21 15:51:15 -04: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?
|
2018-11-06 17:41:55 -05:00
|
|
|
@term = Search.prepare_data(term, Topic === @search_context ? :topic : nil)
|
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
|
2018-01-16 05:29:22 -05:00
|
|
|
if SiteSetting.log_search_queries? && @opts[:search_type].present?
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
AND post_timings.user_id = #{ActiveRecord::Base.connection.quote(@guardian.user.id)}
|
2017-03-08 09:46:23 -05:00
|
|
|
")
|
|
|
|
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
|
2017-08-31 00:06:56 -04:00
|
|
|
AND post_timings.user_id = #{ActiveRecord::Base.connection.quote(@guardian.user.id)}
|
2017-03-08 09:46:23 -05:00
|
|
|
")
|
|
|
|
.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
|
|
|
|
|
2018-08-26 21:05:28 -04:00
|
|
|
advanced_filter(/^\#([\p{L}0-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(":")
|
2018-05-28 01:28:02 -04:00
|
|
|
next if slug.empty?
|
|
|
|
|
2016-05-11 05:53:54 -04:00
|
|
|
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
|
2018-10-05 05:23:52 -04:00
|
|
|
tag_id = Tag.where_name(slug[0]).pluck(:id).first
|
2017-08-01 18:15:04 -04:00
|
|
|
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
|
2018-02-19 22:41:00 -05:00
|
|
|
WHERE pd1.search_data @@ #{Search.ts_query(term: "##{match}")})")
|
2017-08-01 18:15:04 -04:00
|
|
|
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
|
|
|
|
|
2018-08-26 21:05:28 -04:00
|
|
|
advanced_filter(/^tags?:([\p{L}0-9,\-_+]+)/) do |posts, match|
|
2018-01-03 16:45:21 -05:00
|
|
|
search_tags(posts, match, positive: true)
|
|
|
|
end
|
2017-05-31 10:36:15 -04:00
|
|
|
|
2018-08-26 21:05:28 -04:00
|
|
|
advanced_filter(/\-tags?:([\p{L}0-9,\-_+]+)/) do |posts, match|
|
2018-01-03 16:45:21 -05:00
|
|
|
search_tags(posts, match, positive: false)
|
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 (
|
2018-08-03 19:56:26 -04:00
|
|
|
SELECT post_id
|
|
|
|
FROM topic_links
|
|
|
|
WHERE extension IN (:file_extensions)
|
|
|
|
|
2017-07-04 11:50:08 -04:00
|
|
|
UNION
|
2018-08-03 19:56:26 -04:00
|
|
|
|
|
|
|
SELECT post_uploads.post_id
|
|
|
|
FROM uploads
|
|
|
|
JOIN post_uploads ON post_uploads.upload_id = uploads.id
|
|
|
|
WHERE lower(uploads.extension) IN (:file_extensions)
|
|
|
|
)", 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
|
|
|
|
2018-01-03 16:45:21 -05:00
|
|
|
def search_tags(posts, match, positive:)
|
|
|
|
return if match.nil?
|
2018-10-05 05:23:52 -04:00
|
|
|
match.downcase!
|
2018-01-03 16:45:21 -05:00
|
|
|
modifier = positive ? "" : "NOT"
|
2014-10-17 23:54:11 -04:00
|
|
|
|
2018-01-03 16:45:21 -05:00
|
|
|
if match.include?('+')
|
|
|
|
tags = match.split('+')
|
|
|
|
|
|
|
|
posts.where("topics.id #{modifier} IN (
|
|
|
|
SELECT tt.topic_id
|
|
|
|
FROM topic_tags tt, tags
|
|
|
|
WHERE tt.tag_id = tags.id
|
|
|
|
GROUP BY tt.topic_id
|
2018-10-05 05:23:52 -04:00
|
|
|
HAVING to_tsvector(#{default_ts_config}, array_to_string(array_agg(lower(tags.name)), ' ')) @@ to_tsquery(#{default_ts_config}, ?)
|
2018-09-28 05:27:08 -04:00
|
|
|
)", tags.join('&'))
|
2018-01-03 16:45:21 -05:00
|
|
|
else
|
|
|
|
tags = match.split(",")
|
|
|
|
|
|
|
|
posts.where("topics.id #{modifier} IN (
|
|
|
|
SELECT DISTINCT(tt.topic_id)
|
|
|
|
FROM topic_tags tt, tags
|
2018-10-05 05:23:52 -04:00
|
|
|
WHERE tt.tag_id = tags.id AND lower(tags.name) IN (?)
|
2018-09-28 05:27:08 -04:00
|
|
|
)", tags)
|
2018-01-03 16:45:21 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_advanced_search!(term)
|
2018-06-07 01:28:18 -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
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
found = false
|
2015-06-22 22:14:06 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
Search.advanced_filters.each do |matcher, block|
|
|
|
|
cleaned = word.gsub(/["']/, "")
|
|
|
|
if cleaned =~ matcher
|
|
|
|
(@filters ||= []) << [block, $1]
|
|
|
|
found = true
|
2015-06-22 22:14:06 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2015-06-22 22:14:06 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
@in_title = false
|
|
|
|
|
|
|
|
if word == 'order:latest' || word == 'l'
|
|
|
|
@order = :latest
|
|
|
|
nil
|
|
|
|
elsif word == 'order:latest_topic'
|
|
|
|
@order = :latest_topic
|
|
|
|
nil
|
|
|
|
elsif word == 'in:title'
|
|
|
|
@in_title = true
|
|
|
|
nil
|
|
|
|
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
|
2015-06-26 03:36:28 -04:00
|
|
|
end
|
2014-09-03 07:54:10 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
nil
|
|
|
|
elsif word == 'order:views'
|
|
|
|
@order = :views
|
|
|
|
nil
|
|
|
|
elsif word == 'order:likes'
|
|
|
|
@order = :likes
|
|
|
|
nil
|
|
|
|
elsif word == 'in:private'
|
|
|
|
@search_pms = true
|
|
|
|
nil
|
|
|
|
elsif word =~ /^private_messages:(.+)$/
|
|
|
|
@search_pms = true
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
found ? nil : word
|
|
|
|
end
|
|
|
|
end.compact.join(' ')
|
|
|
|
end
|
2014-09-03 07:54:10 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def find_grouped_results
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2018-06-07 01:28:18 -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")
|
|
|
|
else
|
|
|
|
unless @search_context
|
|
|
|
user_search if @term.present?
|
|
|
|
category_search if @term.present?
|
|
|
|
tags_search if @term.present?
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
topic_search
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
add_more_topics_if_expected
|
|
|
|
@results
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.posts.length
|
|
|
|
if expected_topics > 0
|
|
|
|
extra_posts = posts_query(expected_topics * Search.burst_factor)
|
|
|
|
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-05-13 17:04:41 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
# If we're searching for a single topic
|
|
|
|
def single_topic(id)
|
|
|
|
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
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
@results.add(post)
|
|
|
|
@results
|
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def secure_category_ids
|
|
|
|
return @secure_category_ids unless @secure_category_ids.nil?
|
|
|
|
@secure_category_ids = @guardian.secure_category_ids
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def category_search
|
|
|
|
# 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-12-13 03:00:48 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
categories = Category.includes(:category_search_data)
|
|
|
|
.where("category_search_data.search_data @@ #{ts_query}")
|
|
|
|
.references(:category_search_data)
|
|
|
|
.order("topics_month DESC")
|
|
|
|
.secured(@guardian)
|
|
|
|
.limit(limit)
|
2013-05-13 17:04:41 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
categories.each do |category|
|
|
|
|
@results.add(category)
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def user_search
|
|
|
|
return if SiteSetting.hide_user_profiles_from_public && !@guardian.user
|
2015-10-28 14:56:08 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
users = User.includes(:user_search_data)
|
|
|
|
.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")
|
|
|
|
.limit(limit)
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
users.each do |user|
|
|
|
|
@results.add(user)
|
2013-05-23 14:26:51 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2013-05-22 14:36:14 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def tags_search
|
|
|
|
return unless SiteSetting.tagging_enabled
|
2017-08-25 11:52:18 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
tags = Tag.includes(:tag_search_data)
|
|
|
|
.where("tag_search_data.search_data @@ #{ts_query}")
|
|
|
|
.references(:tag_search_data)
|
|
|
|
.order("name asc")
|
|
|
|
.limit(limit)
|
2017-08-25 11:52:18 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
tags.each do |tag|
|
|
|
|
@results.add(tag)
|
2017-08-25 11:52:18 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2017-08-25 11:52:18 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def posts_query(limit, opts = nil)
|
|
|
|
opts ||= {}
|
|
|
|
posts = Post.where(post_type: Topic.visible_post_types(@guardian.user))
|
|
|
|
.joins(:post_search_data, :topic)
|
|
|
|
.joins("LEFT JOIN categories ON categories.id = topics.category_id")
|
|
|
|
.where("topics.deleted_at" => nil)
|
2015-02-18 20:56:49 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
is_topic_search = @search_context.present? && @search_context.is_a?(Topic)
|
2018-05-07 05:43:55 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
posts = posts.where("topics.visible") unless is_topic_search
|
2014-12-03 21:46:52 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if opts[:private_messages] || (is_topic_search && @search_context.private_message?)
|
|
|
|
posts = posts.where("topics.archetype = ?", Archetype.private_message)
|
2013-08-01 20:31:36 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
unless @guardian.is_admin?
|
|
|
|
posts = posts.private_posts_for_user(@guardian.user)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
posts = posts.where("topics.archetype <> ?", Archetype.private_message)
|
|
|
|
end
|
2016-07-25 01:06:25 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if @term.present?
|
|
|
|
if is_topic_search
|
2016-07-25 01:06:25 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
term_without_quote = @term
|
|
|
|
if @term =~ /"(.+)"/
|
|
|
|
term_without_quote = $1
|
|
|
|
end
|
2016-07-25 01:06:25 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if @term =~ /'(.+)'/
|
|
|
|
term_without_quote = $1
|
2014-10-18 00:19:08 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
posts = posts.joins('JOIN users u ON u.id = posts.user_id')
|
|
|
|
posts = posts.where("posts.raw || ' ' || u.username || ' ' || COALESCE(u.name, '') ilike ?", "%#{term_without_quote}%")
|
|
|
|
else
|
|
|
|
# A is for title
|
|
|
|
# B is for category
|
|
|
|
# C is for tags
|
|
|
|
# D is for cooked
|
|
|
|
weights = @in_title ? 'A' : (SiteSetting.tagging_enabled ? 'ABCD' : 'ABD')
|
|
|
|
posts = posts.where("post_search_data.search_data @@ #{ts_query(weight_filter: weights)}")
|
|
|
|
exact_terms = @term.scan(/"([^"]+)"/).flatten
|
|
|
|
exact_terms.each do |exact|
|
|
|
|
posts = posts.where("posts.raw ilike :exact OR topics.title ilike :exact", exact: "%#{exact}%")
|
2014-10-18 00:34:05 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
|
|
|
end
|
2014-10-18 00:19:08 -04:00
|
|
|
|
2018-06-07 01:28:18 -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
|
|
|
|
end
|
|
|
|
end if @filters
|
2013-05-24 14:03:45 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
# If we have a search context, prioritize those posts first
|
|
|
|
if @search_context.present?
|
2014-12-03 21:46:52 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if @search_context.is_a?(User)
|
2014-12-03 21:46:52 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if opts[:private_messages]
|
|
|
|
posts = posts.private_posts_for_user(@search_context)
|
|
|
|
else
|
|
|
|
posts = posts.where("posts.user_id = #{@search_context.id}")
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
elsif @search_context.is_a?(Category)
|
|
|
|
category_ids = [@search_context.id] + Category.where(parent_category_id: @search_context.id).pluck(:id)
|
|
|
|
posts = posts.where("topics.category_id in (?)", category_ids)
|
|
|
|
elsif @search_context.is_a?(Topic)
|
|
|
|
posts = posts.where("topics.id = #{@search_context.id}")
|
|
|
|
.order("posts.post_number #{@order == :latest ? "DESC" : ""}")
|
2013-05-24 14:03:45 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if @order == :latest || (@term.blank? && !@order)
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(posts.created_at) DESC")
|
2014-08-22 16:55:19 -04:00
|
|
|
else
|
2018-06-07 01:28:18 -04:00
|
|
|
posts = posts.reorder("posts.created_at DESC")
|
2014-08-22 16:55:19 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
elsif @order == :latest_topic
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(topics.created_at) DESC")
|
2013-05-22 14:36:14 -04:00
|
|
|
else
|
2018-06-07 01:28:18 -04:00
|
|
|
posts = posts.order("topics.created_at DESC")
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
elsif @order == :views
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(topics.views) DESC")
|
|
|
|
else
|
|
|
|
posts = posts.order("topics.views DESC")
|
|
|
|
end
|
|
|
|
elsif @order == :likes
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(posts.like_count) DESC")
|
|
|
|
else
|
|
|
|
posts = posts.order("posts.like_count DESC")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
data_ranking = "TS_RANK_CD(post_search_data.search_data, #{ts_query})"
|
|
|
|
if opts[:aggregate_search]
|
|
|
|
posts = posts.order("MAX(#{data_ranking}) DESC")
|
|
|
|
else
|
|
|
|
posts = posts.order("#{data_ranking} DESC")
|
|
|
|
end
|
|
|
|
posts = posts.order("topics.bumped_at DESC")
|
2013-05-22 14:36:14 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if secure_category_ids.present?
|
|
|
|
posts = posts.where("(categories.id IS NULL) OR (NOT categories.read_restricted) OR (categories.id IN (?))", secure_category_ids).references(:categories)
|
|
|
|
else
|
|
|
|
posts = posts.where("(categories.id IS NULL) OR (NOT categories.read_restricted)").references(:categories)
|
2013-05-23 11:13:23 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
posts = posts.offset(offset)
|
|
|
|
posts.limit(limit)
|
|
|
|
end
|
2014-05-15 10:31:45 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def self.default_ts_config
|
|
|
|
"'#{Search.ts_config}'"
|
|
|
|
end
|
2015-08-10 03:41:14 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def default_ts_config
|
|
|
|
self.class.default_ts_config
|
|
|
|
end
|
2015-08-10 03:41:14 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def self.ts_query(term: , ts_config: nil, joiner: "&", weight_filter: nil)
|
2015-08-13 03:55:10 -04:00
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
data = DB.query_single(
|
|
|
|
"SELECT TO_TSVECTOR(:config, :term)",
|
|
|
|
config: 'simple',
|
|
|
|
term: term
|
|
|
|
).first
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
ts_config = ActiveRecord::Base.connection.quote(ts_config) if ts_config
|
|
|
|
all_terms = data.scan(/'([^']+)'\:\d+/).flatten
|
|
|
|
all_terms.map! do |t|
|
|
|
|
t.split(/[\)\(&']/)[0]
|
|
|
|
end.compact!
|
2014-05-15 10:31:45 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
query = ActiveRecord::Base.connection.quote(
|
|
|
|
all_terms
|
|
|
|
.map { |t| "'#{PG::Connection.escape_string(t)}':*#{weight_filter}" }
|
|
|
|
.join(" #{joiner} ")
|
|
|
|
)
|
2013-05-23 11:13:23 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
"TO_TSQUERY(#{ts_config || default_ts_config}, #{query})"
|
|
|
|
end
|
2014-08-28 03:15:53 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def ts_query(ts_config = nil, weight_filter: nil)
|
|
|
|
@ts_query_cache ||= {}
|
|
|
|
@ts_query_cache["#{ts_config || default_ts_config} #{@term} #{weight_filter}"] ||=
|
2018-08-30 21:46:55 -04:00
|
|
|
Search.ts_query(term: @term, ts_config: ts_config, weight_filter: weight_filter)
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2015-09-18 03:16:37 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def wrap_rows(query)
|
|
|
|
"SELECT *, row_number() over() row_number FROM (#{query.to_sql}) xxx"
|
|
|
|
end
|
2014-09-01 03:04:40 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def aggregate_post_sql(opts)
|
|
|
|
min_or_max = @order == :latest ? "max" : "min"
|
2016-08-12 13:04:46 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
query =
|
|
|
|
if @order == :likes
|
|
|
|
# likes are a pain to aggregate so skip
|
|
|
|
posts_query(limit, private_messages: opts[:private_messages])
|
|
|
|
.select('topics.id', "posts.post_number")
|
|
|
|
else
|
|
|
|
posts_query(limit, aggregate_search: true, private_messages: opts[:private_messages])
|
|
|
|
.select('topics.id', "#{min_or_max}(posts.post_number) post_number")
|
|
|
|
.group('topics.id')
|
2016-08-12 13:04:46 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -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) }
|
2016-08-12 13:04:46 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
# double wrapping so we get correct row numbers
|
|
|
|
{ default: wrap_rows(query) }
|
|
|
|
end
|
2014-09-02 22:13:13 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def aggregate_posts(post_sql)
|
|
|
|
return [] unless post_sql
|
2016-08-12 13:04:46 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
posts_eager_loads(Post)
|
|
|
|
.joins("JOIN (#{post_sql}) x ON x.id = posts.topic_id AND x.post_number = posts.post_number")
|
|
|
|
.order('row_number')
|
|
|
|
end
|
2014-09-01 03:04:40 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def aggregate_search(opts = {})
|
|
|
|
post_sql = aggregate_post_sql(opts)
|
2017-03-08 09:46:23 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
added = 0
|
2016-08-12 17:18:12 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
aggregate_posts(post_sql[:default]).each do |p|
|
|
|
|
@results.add(p)
|
|
|
|
added += 1
|
2014-08-22 16:55:19 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if added < limit
|
|
|
|
aggregate_posts(post_sql[:remaining]).each { |p| @results.add(p) }
|
2014-12-03 21:46:52 -05:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2014-12-03 21:46:52 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def private_messages_search
|
|
|
|
raise Discourse::InvalidAccess.new("anonymous can not search PMs") unless @guardian.user
|
2017-05-30 19:14:09 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
aggregate_search(private_messages: true)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def topic_search
|
|
|
|
if @search_context.is_a?(Topic)
|
|
|
|
posts = posts_eager_loads(posts_query(limit))
|
|
|
|
.where('posts.topic_id = ?', @search_context.id)
|
2017-05-30 19:14:09 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
posts.each do |post|
|
|
|
|
@results.add(post)
|
2017-05-30 19:14:09 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
else
|
|
|
|
aggregate_search
|
|
|
|
end
|
|
|
|
end
|
2017-05-30 19:14:09 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def posts_eager_loads(query)
|
|
|
|
query = query.includes(:user)
|
|
|
|
topic_eager_loads = [:category]
|
|
|
|
|
|
|
|
if SiteSetting.tagging_enabled
|
|
|
|
topic_eager_loads << :tags
|
2017-05-30 19:14:09 -04:00
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
query.includes(topic: topic_eager_loads)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|