Class: Search

Inherits:
Object
  • Object
show all
Defined in:
lib/search.rb,
lib/search/grouped_search_results.rb

Defined Under Namespace

Classes: GroupedSearchResults

Constant Summary collapse

HIGHLIGHT_CSS_CLASS =
"search-highlight"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, opts = nil) ⇒ Search

Returns a new instance of Search.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/search.rb', line 224

def initialize(term, opts = nil)
  @opts = opts || {}
  @guardian = @opts[:guardian] || Guardian.new
  @search_context = @opts[:search_context]
  @blurb_length = @opts[:blurb_length]
  @valid = true
  @page = @opts[:page]
  @search_all_pms = false

  term = Search.clean_term(term)

  @clean_term = term
  @in_title = false

  term = process_advanced_search!(term)

  if term.present?
    @term = Search.prepare_data(term, Topic === @search_context ? :topic : nil)
    @original_term = Search.escape_string(@term)
  end

  if @search_pms || @search_all_pms || @opts[:type_filter] == "private_messages"
    @opts[:type_filter] = "private_messages"
    @search_context ||= @guardian.user

    unless @search_context.present? && @guardian.can_see_private_messages?(@search_context.id)
      raise Discourse::InvalidAccess.new
    end
  end

  @opts[:type_filter] = "all_topics" if @search_all_topics && @guardian.user

  @results =
    GroupedSearchResults.new(
      type_filter: @opts[:type_filter],
      term: clean_term,
      blurb_term: term,
      search_context: @search_context,
      blurb_length: @blurb_length,
      is_header_search: !use_full_page_limit,
    )
end

Instance Attribute Details

#clean_termObject (readonly)

Returns the value of attribute clean_term.



222
223
224
# File 'lib/search.rb', line 222

def clean_term
  @clean_term
end

#guardianObject (readonly)

Returns the value of attribute guardian.



222
223
224
# File 'lib/search.rb', line 222

def guardian
  @guardian
end

#termObject

Returns the value of attribute term.



221
222
223
# File 'lib/search.rb', line 221

def term
  @term
end

Class Method Details

.advanced_filter(trigger, &block) ⇒ Object



353
354
355
# File 'lib/search.rb', line 353

def self.advanced_filter(trigger, &block)
  (@advanced_filters ||= {})[trigger] = block
end

.advanced_filtersObject



357
358
359
# File 'lib/search.rb', line 357

def self.advanced_filters
  @advanced_filters
end

.advanced_order(trigger, &block) ⇒ Object



345
346
347
# File 'lib/search.rb', line 345

def self.advanced_order(trigger, &block)
  (@advanced_orders ||= {})[trigger] = block
end

.advanced_ordersObject



349
350
351
# File 'lib/search.rb', line 349

def self.advanced_orders
  @advanced_orders
end

.clean_term(term) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/search.rb', line 92

def self.clean_term(term)
  term = term.to_s.dup

  # Removes any zero-width characters from search terms
  term.gsub!(/[\u200B-\u200D\uFEFF]/, "")

  # Replace curly quotes to regular quotes
  term.gsub!(/[\u201c\u201d]/, '"')

  # Replace fancy apostophes to regular apostophes
  term.gsub!(/[\u02b9\u02bb\u02bc\u02bd\u02c8\u2018\u2019\u201b\u2032\uff07]/, "'")

  term
end

.custom_topic_eager_load(tables = nil, &block) ⇒ Object



361
362
363
# File 'lib/search.rb', line 361

def self.custom_topic_eager_load(tables = nil, &block)
  (@custom_topic_eager_loads ||= []) << (tables || block)
end

.custom_topic_eager_loadsObject



365
366
367
# File 'lib/search.rb', line 365

def self.custom_topic_eager_loads
  Array.wrap(@custom_topic_eager_loads)
end

.execute(term, opts = nil) ⇒ Object



291
292
293
# File 'lib/search.rb', line 291

def self.execute(term, opts = nil)
  self.new(term, opts).execute
end

.facetsObject



26
27
28
# File 'lib/search.rb', line 26

def self.facets
  %w[topic category user private_messages tags all_topics exclude_topics]
end

.japanese_punctuation_regexpObject



86
87
88
89
90
# File 'lib/search.rb', line 86

def self.japanese_punctuation_regexp
  # Regexp adapted from https://github.com/6/tiny_segmenter/blob/15a5b825993dfd2c662df3766f232051716bef5b/lib/tiny_segmenter.rb#L7
  @japanese_punctuation_regexp ||=
    Regexp.compile("[-–—―.。・()()[]{}{}【】⟨⟩、、,,،…‥〽「」『』〜~!!::??\"'|__“”‘’;/⁄/«»]")
end

.min_post_id(opts = nil) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'lib/search.rb', line 210

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
  Discourse
    .cache
    .fetch("search-min-post-id:#{SiteSetting.search_recent_posts_size}", expires_in: 1.week) do
      min_post_id_no_cache
    end
end

.min_post_id_no_cacheObject



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/search.rb', line 196

def self.min_post_id_no_cache
  return 0 unless SiteSetting.search_prefer_recent_posts?

  offset, has_more =
    Post
      .unscoped
      .order("id desc")
      .offset(SiteSetting.search_recent_posts_size - 1)
      .limit(2)
      .pluck(:id)

  has_more ? offset : 0
end

.on_preload(&blk) ⇒ Object



10
11
12
# File 'lib/search.rb', line 10

def self.on_preload(&blk)
  (@preload ||= Set.new) << blk
end

.per_facetObject



18
19
20
# File 'lib/search.rb', line 18

def self.per_facet
  5
end

.per_filterObject



22
23
24
# File 'lib/search.rb', line 22

def self.per_filter
  SiteSetting.search_page_size
end

.preload(results, object) ⇒ Object



14
15
16
# File 'lib/search.rb', line 14

def self.preload(results, object)
  @preload.each { |preload| preload.call(results, object) } if @preload
end

.prepare_data(search_data, purpose = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/search.rb', line 107

def self.prepare_data(search_data, purpose = nil)
  data = search_data.dup
  data.force_encoding("UTF-8")
  data = clean_term(data)

  if purpose != :topic
    if segment_chinese?
      require "cppjieba_rb" unless defined?(CppjiebaRb)

      segmented_data = []

      # We need to split up the string here because Cppjieba has a bug where text starting with numeric chars will
      # be split into two segments. For example, '123abc' becomes '123' and 'abc' after segmentation.
      data.scan(/(?<chinese>[\p{Han}。,、“”《》…\.:?!;()]+)|([^\p{Han}]+)/) do
        match_data = $LAST_MATCH_INFO

        if match_data[:chinese]
          segments = CppjiebaRb.segment(match_data.to_s, mode: :mix)

          segments = CppjiebaRb.filter_stop_word(segments) if ts_config != "english"

          segments = segments.filter { |s| s.present? }
          segmented_data << segments.join(" ")
        else
          segmented_data << match_data.to_s.squish
        end
      end

      data = segmented_data.join(" ")
    elsif segment_japanese?
      data.gsub!(japanese_punctuation_regexp, " ")
      data = TinyJapaneseSegmenter.segment(data)
      data = data.filter { |s| s.present? }
      data = data.join(" ")
    else
      data.squish!
    end
  end

  data.gsub!(/\S+/) do |str|
    if str =~ %r{\A["]?((https?://)[\S]+)["]?\z}
      begin
        uri = URI.parse(Regexp.last_match[1])
        uri.query = nil
        str = uri.to_s
      rescue URI::Error
        # don't fail if uri does not parse
      end
    end

    str
  end

  data
end

.segment_chinese?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/search.rb', line 78

def self.segment_chinese?
  %w[zh_TW zh_CN].include?(SiteSetting.default_locale) || SiteSetting.search_tokenize_chinese
end

.segment_japanese?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/search.rb', line 82

def self.segment_japanese?
  SiteSetting.default_locale == "ja" || SiteSetting.search_tokenize_japanese
end

.ts_config(locale = SiteSetting.default_locale) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/search.rb', line 30

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.
  # base docker config
  #
  case locale.split("_")[0].to_sym
  when :da
    "danish"
  when :nl
    "dutch"
  when :en
    "english"
  when :fi
    "finnish"
  when :fr
    "french"
  when :de
    "german"
  when :hu
    "hungarian"
  when :it
    "italian"
  when :nb
    "norwegian"
  when :pt
    "portuguese"
  when :ro
    "romanian"
  when :ru
    "russian"
  when :es
    "spanish"
  when :sv
    "swedish"
  when :tr
    "turkish"
  else
    "simple" # use the 'simple' stemmer for other languages
  end
end

.word_to_date(str) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/search.rb', line 163

def self.word_to_date(str)
  return Time.zone.now.beginning_of_day.days_ago(str.to_i) if str =~ /\A[0-9]{1,3}\z/

  if str =~ /\A([12][0-9]{3})(-([0-1]?[0-9]))?(-([0-3]?[0-9]))?\z/
    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(
      begin
        Time.zone.parse("#{year}-#{month}-#{day}")
      rescue ArgumentError
      end
    )
  end

  return Time.zone.now.beginning_of_day.yesterday if str.downcase == "yesterday"

  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

.wrap_unaccent(str) ⇒ Object



74
75
76
# File 'lib/search.rb', line 74

def self.wrap_unaccent(str)
  SiteSetting.search_ignore_accents ? "unaccent(#{str})" : str
end

Instance Method Details

#apply_filters(posts) ⇒ Object



748
749
750
751
752
753
754
755
756
757
# File 'lib/search.rb', line 748

def apply_filters(posts)
  @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
  posts
end

#apply_order(posts, aggregate_search: false, allow_relevance_search: true, type_filter: "all_topics") ⇒ Object



759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'lib/search.rb', line 759

def apply_order(
  posts,
  aggregate_search: false,
  allow_relevance_search: true,
  type_filter: "all_topics"
)
  if @order == :latest
    if aggregate_search
      posts = posts.order("MAX(posts.created_at) DESC")
    else
      posts = posts.reorder("posts.created_at DESC")
    end
  elsif @order == :oldest
    if aggregate_search
      posts = posts.order("MAX(posts.created_at) ASC")
    else
      posts = posts.reorder("posts.created_at ASC")
    end
  elsif @order == :latest_topic
    if aggregate_search
      posts = posts.order("MAX(topics.created_at) DESC")
    else
      posts = posts.order("topics.created_at DESC")
    end
  elsif @order == :oldest_topic
    if aggregate_search
      posts = posts.order("MAX(topics.created_at) ASC")
    else
      posts = posts.order("topics.created_at ASC")
    end
  elsif @order == :views
    if aggregate_search
      posts = posts.order("MAX(topics.views) DESC")
    else
      posts = posts.order("topics.views DESC")
    end
  elsif @order == :likes
    if aggregate_search
      posts = posts.order("MAX(posts.like_count) DESC")
    else
      posts = posts.order("posts.like_count DESC")
    end
  elsif allow_relevance_search
    posts = sort_by_relevance(posts, type_filter: type_filter, aggregate_search: aggregate_search)
  end

  if @order
    advanced_order = Search.advanced_orders&.fetch(@order, nil)
    posts = advanced_order.call(posts) if advanced_order
  end

  posts
end

#execute(readonly_mode: Discourse.readonly_mode?) ⇒ Object

Query a term



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/search.rb', line 296

def execute(readonly_mode: Discourse.readonly_mode?)
  if log_query?(readonly_mode)
    status, search_log_id =
      SearchLog.log(
        term: @clean_term,
        search_type: @opts[:search_type],
        ip_address: @opts[:ip_address],
        user_id: @opts[:user_id],
      )
    @results.search_log_id = search_log_id unless status == :error
  end

  unless @filters.present? || @opts[:search_for_id]
    min_length = min_search_term_length
    terms = (@term || "").split(/\s(?=(?:[^"]|"[^"]*")*$)/).reject { |t| t.length < min_length }

    if terms.blank?
      @term = ""
      @valid = false
      return
    end
  end

  # If the term is a number or url to a topic, just include that topic
  if @opts[:search_for_id] && %w[topic private_messages all_topics].include?(@results.type_filter)
    if @term =~ /\A\d+\z/
      single_topic(@term.to_i)
    else
      if route = Discourse.route_for(@term)
        if route[:controller] == "topics" && route[:action] == "show"
          topic_id = (route[:id] || route[:topic_id]).to_i
          single_topic(topic_id) if topic_id > 0
        end
      end
    end
  end

  find_grouped_results if @results.posts.blank?

  if preloaded_topic_custom_fields.present? && @results.posts.present?
    topics = @results.posts.map(&:topic)
    Topic.preload_custom_fields(topics, preloaded_topic_custom_fields)
  end

  Search.preload(@results, self)

  @results
end

#limitObject



267
268
269
270
271
272
273
# File 'lib/search.rb', line 267

def limit
  if use_full_page_limit
    Search.per_filter + 1
  else
    Search.per_facet + 1
  end
end

#offsetObject



275
276
277
278
279
280
281
# File 'lib/search.rb', line 275

def offset
  if @page && @opts[:type_filter].present?
    (@page - 1) * Search.per_filter
  else
    0
  end
end

#post_action_type_filter(posts, post_action_type) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/search.rb', line 455

def post_action_type_filter(posts, post_action_type)
  posts.where(
    "posts.id IN (
    SELECT pa.post_id FROM post_actions pa
    WHERE pa.user_id = ? AND
          pa.post_action_type_id = ? AND
          deleted_at IS NULL
  )",
    @guardian.user.id,
    post_action_type,
  )
end

#use_full_page_limitObject



287
288
289
# File 'lib/search.rb', line 287

def use_full_page_limit
  @opts[:search_type] == :full_page || Topic === @search_context
end

#valid?Boolean

Returns:

  • (Boolean)


283
284
285
# File 'lib/search.rb', line 283

def valid?
  @valid
end