Class: Search
- Inherits:
-
Object
show all
- Defined in:
- lib/search.rb,
lib/search/grouped_search_results.rb
Defined Under Namespace
Classes: GroupedSearchResults
Constant Summary
collapse
- DIACRITICS =
/([\u0300-\u036f]|[\u1AB0-\u1AFF]|[\u1DC0-\u1DFF]|[\u20D0-\u20FF])/
- 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.
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/search.rb', line 239
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 !@order &&
SiteSetting.search_default_sort_order !=
SearchSortOrderSiteSetting.value_from_id(:relevance)
@order = SearchSortOrderSiteSetting.id_from_value(SiteSetting.search_default_sort_order)
end
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,
can_lazy_load_categories: @guardian.can_lazy_load_categories?,
)
end
|
Instance Attribute Details
#clean_term ⇒ Object
Returns the value of attribute clean_term.
237
238
239
|
# File 'lib/search.rb', line 237
def clean_term
@clean_term
end
|
#guardian ⇒ Object
Returns the value of attribute guardian.
237
238
239
|
# File 'lib/search.rb', line 237
def guardian
@guardian
end
|
#term ⇒ Object
Returns the value of attribute term.
236
237
238
|
# File 'lib/search.rb', line 236
def term
@term
end
|
Class Method Details
.advanced_filter(trigger, &block) ⇒ Object
375
376
377
|
# File 'lib/search.rb', line 375
def self.advanced_filter(trigger, &block)
advanced_filters[trigger] = block
end
|
.advanced_filters ⇒ Object
379
380
381
|
# File 'lib/search.rb', line 379
def self.advanced_filters
@advanced_filters ||= {}
end
|
.advanced_order(trigger, &block) ⇒ Object
367
368
369
|
# File 'lib/search.rb', line 367
def self.advanced_order(trigger, &block)
advanced_orders[trigger] = block
end
|
.advanced_orders ⇒ Object
371
372
373
|
# File 'lib/search.rb', line 371
def self.advanced_orders
@advanced_orders ||= {}
end
|
.clean_term(term) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/search.rb', line 100
def self.clean_term(term)
term = term.to_s.dup
term.gsub!(/[\u200B-\u200D\uFEFF]/, "")
term.gsub!(/[\u201c\u201d]/, '"')
term.gsub!(/[\u02b9\u02bb\u02bc\u02bd\u02c8\u2018\u2019\u201b\u2032\uff07]/, "'")
term
end
|
.custom_topic_eager_load(tables = nil, &block) ⇒ Object
383
384
385
|
# File 'lib/search.rb', line 383
def self.custom_topic_eager_load(tables = nil, &block)
(@custom_topic_eager_loads ||= []) << (tables || block)
end
|
.custom_topic_eager_loads ⇒ Object
387
388
389
|
# File 'lib/search.rb', line 387
def self.custom_topic_eager_loads
Array.wrap(@custom_topic_eager_loads)
end
|
.execute(term, opts = nil) ⇒ Object
312
313
314
|
# File 'lib/search.rb', line 312
def self.execute(term, opts = nil)
self.new(term, opts).execute
end
|
.facets ⇒ Object
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_regexp ⇒ Object
94
95
96
97
98
|
# File 'lib/search.rb', line 94
def self.japanese_punctuation_regexp
@japanese_punctuation_regexp ||=
Regexp.compile("[-–—―.。・()()[]{}{}【】⟨⟩、、,,،…‥〽「」『』〜~!!::??\"'|__“”‘’;/⁄/«»]")
end
|
.min_post_id(opts = nil) ⇒ Object
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/search.rb', line 218
def self.min_post_id(opts = nil)
return 0 unless SiteSetting.search_prefer_recent_posts?
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_cache ⇒ Object
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/search.rb', line 204
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
|
.need_segmenting?(data) ⇒ Boolean
229
230
231
232
233
234
|
# File 'lib/search.rb', line 229
def self.need_segmenting?(data)
return false if data.match?(/\A\d+\z/)
!URI.parse(data).path.to_s.start_with?("/")
rescue URI::InvalidURIError
true
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_facet ⇒ Object
18
19
20
|
# File 'lib/search.rb', line 18
def self.per_facet
5
end
|
.per_filter ⇒ Object
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
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
162
163
164
165
166
167
168
169
|
# File 'lib/search.rb', line 115
def self.prepare_data(search_data, purpose = nil)
data = search_data.dup
data.force_encoding("UTF-8")
data = clean_term(data)
if purpose != :topic && need_segmenting?(data)
if segment_chinese?
require "cppjieba_rb" unless defined?(CppjiebaRb)
segmented_data = []
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
end
end
str
end
data
end
|
.segment_chinese? ⇒ Boolean
.segment_japanese? ⇒ Boolean
.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)
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" end
end
|
.unaccent(str) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/search.rb', line 74
def self.unaccent(str)
if SiteSetting.search_ignore_accents
DB.query("SELECT unaccent(:str)", str: str)[0].unaccent
else
str
end
end
|
.word_to_date(str) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/search.rb', line 171
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(expr) ⇒ Object
82
83
84
|
# File 'lib/search.rb', line 82
def self.wrap_unaccent(expr)
SiteSetting.search_ignore_accents ? "unaccent(#{expr})" : expr
end
|
Instance Method Details
#apply_filters(posts) ⇒ Object
796
797
798
799
800
801
802
803
804
805
|
# File 'lib/search.rb', line 796
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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
|
# File 'lib/search.rb', line 807
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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
# File 'lib/search.rb', line 317
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_agent: @opts[:user_agent],
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 @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
|
#offset ⇒ Object
296
297
298
299
300
301
302
|
# File 'lib/search.rb', line 296
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
477
478
479
480
481
482
483
484
485
486
487
488
|
# File 'lib/search.rb', line 477
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_limit ⇒ Object
308
309
310
|
# File 'lib/search.rb', line 308
def use_full_page_limit
@opts[:search_type] == :full_page || Topic === @search_context
end
|
#valid? ⇒ Boolean
304
305
306
|
# File 'lib/search.rb', line 304
def valid?
@valid
end
|