mongoid_fulltext 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.github/workflows/danger-comment.yml +19 -0
- data/.github/workflows/danger.yml +21 -0
- data/.github/workflows/lint.yml +29 -0
- data/.github/workflows/release.yml +30 -0
- data/.github/workflows/test.yml +81 -0
- data/.gitignore +2 -0
- data/.rubocop.yml +149 -3
- data/.rubocop_todo.yml +131 -51
- data/.spellcheck.txt +181 -0
- data/CHANGELOG.md +57 -31
- data/Dangerfile +5 -1
- data/Gemfile +22 -18
- data/Gemfile.danger +5 -0
- data/README.md +171 -179
- data/RELEASING.md +60 -0
- data/Rakefile +21 -3
- data/UPGRADING.md +43 -0
- data/lib/mongoid/full_text_search/field_scores.rb +48 -0
- data/lib/mongoid/full_text_search/index_definition.rb +31 -0
- data/lib/mongoid/full_text_search/indexable.rb +1 -1
- data/lib/mongoid/full_text_search/indexes.rb +1 -1
- data/lib/mongoid/full_text_search/ngrams.rb +93 -0
- data/lib/mongoid/full_text_search/version.rb +1 -1
- data/lib/mongoid/full_text_search.rb +112 -144
- data/mongoid_fulltext.gemspec +4 -4
- data/script/release.rb +13 -0
- data/spec/models/chinese_artwork.rb +7 -0
- data/spec/models/filtered_artist.rb +1 -2
- data/spec/models/filtered_artwork.rb +1 -2
- data/spec/models/filtered_other.rb +1 -2
- data/spec/models/localized_artwork.rb +29 -0
- data/spec/models/multilingual_artwork.rb +7 -0
- data/spec/models/partitioned_artist.rb +1 -1
- data/spec/models/russian_artwork.rb +1 -2
- data/spec/models/scoped_artwork.rb +14 -0
- data/spec/models/tagged_artwork.rb +9 -0
- data/spec/models/weighted_artwork.rb +34 -0
- data/spec/mongoid/array_fields_spec.rb +43 -0
- data/spec/mongoid/bulk_indexing_spec.rb +56 -0
- data/spec/mongoid/candidate_limits_spec.rb +19 -0
- data/spec/mongoid/criteria_search_spec.rb +73 -0
- data/spec/mongoid/full_text_search_spec.rb +41 -12
- data/spec/mongoid/localized_fields_spec.rb +197 -0
- data/spec/mongoid/ngram_sampling_spec.rb +55 -0
- data/spec/mongoid/non_latin_spec.rb +50 -0
- data/spec/mongoid/timeout_option_spec.rb +18 -0
- data/spec/mongoid/weighted_fields_spec.rb +102 -0
- data/spec/release/release_spec.rb +60 -0
- data/spec/spec_helper.rb +22 -7
- data/spec/support/mongoid.rb +6 -4
- data/spec/unit/field_scores_spec.rb +54 -0
- data/spec/unit/index_definition_spec.rb +48 -0
- data/spec/unit/ngrams_spec.rb +73 -0
- data/spec/version_spec.rb +7 -0
- metadata +62 -11
- data/.travis.yml +0 -30
|
@@ -7,8 +7,10 @@ else
|
|
|
7
7
|
require 'mongoid/full_text_search/indexable'
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
require 'unicode_utils'
|
|
11
10
|
require 'cgi'
|
|
11
|
+
require 'mongoid/full_text_search/ngrams'
|
|
12
|
+
require 'mongoid/full_text_search/index_definition'
|
|
13
|
+
require 'mongoid/full_text_search/field_scores'
|
|
12
14
|
|
|
13
15
|
module Mongoid::FullTextSearch
|
|
14
16
|
extend ActiveSupport::Concern
|
|
@@ -24,11 +26,11 @@ module Mongoid::FullTextSearch
|
|
|
24
26
|
def fulltext_search_in(*args)
|
|
25
27
|
self.mongoid_fulltext_config = {} if mongoid_fulltext_config.nil?
|
|
26
28
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
|
27
|
-
if options.key?(:index_name)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
index_name = if options.key?(:index_name)
|
|
30
|
+
options[:index_name]
|
|
31
|
+
else
|
|
32
|
+
'mongoid_fulltext.index_%s_%s' % [name.downcase, mongoid_fulltext_config.count]
|
|
33
|
+
end
|
|
32
34
|
|
|
33
35
|
config = {
|
|
34
36
|
alphabet: 'abcdefghijklmnopqrstuvwxyz0123456789 ',
|
|
@@ -41,7 +43,7 @@ module Mongoid::FullTextSearch
|
|
|
41
43
|
max_candidate_set_size: 1000,
|
|
42
44
|
remove_accents: true,
|
|
43
45
|
reindex_immediately: true,
|
|
44
|
-
stop_words: Hash[%w
|
|
46
|
+
stop_words: Hash[%w[i a s t me my we he it am is be do an if
|
|
45
47
|
or as of at by to up in on no so our you him
|
|
46
48
|
his she her its who are was has had did the and
|
|
47
49
|
but for out off why how all any few nor not own
|
|
@@ -52,14 +54,18 @@ module Mongoid::FullTextSearch
|
|
|
52
54
|
being doing until while about after above below under
|
|
53
55
|
again there where other myself itself theirs having during
|
|
54
56
|
before should himself herself because against between through
|
|
55
|
-
further yourself ourselves yourselves themselves
|
|
57
|
+
further yourself ourselves yourselves themselves].map { |x| [x, true] }]
|
|
56
58
|
}
|
|
57
59
|
|
|
58
60
|
config.update(options)
|
|
59
61
|
|
|
62
|
+
raise ArgumentError, ':alphabet must be a String or Regexp' unless config[:alphabet].is_a?(String) || config[:alphabet].is_a?(Regexp)
|
|
63
|
+
raise ArgumentError, ':max_ngrams_to_search must be a positive integer' unless config[:max_ngrams_to_search].is_a?(Integer) && config[:max_ngrams_to_search] > 0
|
|
64
|
+
|
|
60
65
|
args = [:to_s] if args.empty?
|
|
66
|
+
FieldScores.configure(config, args)
|
|
61
67
|
config[:ngram_fields] = args
|
|
62
|
-
config[:alphabet] = Hash[config[:alphabet].split('').map { |ch| [ch, ch] }]
|
|
68
|
+
config[:alphabet] = Hash[config[:alphabet].split('').map { |ch| [ch, ch] }] if config[:alphabet].is_a?(String)
|
|
63
69
|
config[:word_separators] = Hash[config[:word_separators].split('').map { |ch| [ch, ch] }]
|
|
64
70
|
mongoid_fulltext_config[index_name] = config
|
|
65
71
|
|
|
@@ -69,57 +75,62 @@ module Mongoid::FullTextSearch
|
|
|
69
75
|
|
|
70
76
|
def create_fulltext_indexes
|
|
71
77
|
return unless mongoid_fulltext_config
|
|
78
|
+
|
|
72
79
|
mongoid_fulltext_config.each_pair do |index_name, fulltext_config|
|
|
73
80
|
fulltext_search_ensure_indexes(index_name, fulltext_config)
|
|
74
81
|
end
|
|
75
82
|
end
|
|
76
83
|
|
|
84
|
+
def localized_fulltext_index?(config)
|
|
85
|
+
config[:ngram_fields].any? do |field_name|
|
|
86
|
+
field = fields[field_name.to_s]
|
|
87
|
+
field && field.localized?
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def each_fulltext_index
|
|
92
|
+
mongoid_fulltext_config.each_pair do |index_name, config|
|
|
93
|
+
if localized_fulltext_index?(config)
|
|
94
|
+
I18n.available_locales.each do |locale|
|
|
95
|
+
I18n.with_locale(locale) { yield index_name, config, locale.to_s }
|
|
96
|
+
end
|
|
97
|
+
else
|
|
98
|
+
yield index_name, config, nil
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
77
103
|
def fulltext_search_ensure_indexes(index_name, config)
|
|
78
104
|
db = collection.database
|
|
79
105
|
coll = db[index_name]
|
|
80
106
|
|
|
81
|
-
#
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
all_filter_keys |= keys.find_all { |key| key.starts_with?('filter_values.') }
|
|
96
|
-
next unless keys & correct_keys != correct_keys
|
|
97
|
-
Mongoid.logger.info "Dropping #{idef['name']} [#{keys & correct_keys} <=> #{correct_keys}]" if Mongoid.logger
|
|
98
|
-
if Mongoid::Compatibility::Version.mongoid5? || Mongoid::Compatibility::Version.mongoid6?
|
|
107
|
+
# Creating an index also creates the collection before older drivers list its indexes.
|
|
108
|
+
Mongoid.logger.info "Ensuring document_id index on #{coll.name}" if Mongoid.logger
|
|
109
|
+
if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
110
|
+
coll.indexes.create_one('document_id' => 1)
|
|
111
|
+
else
|
|
112
|
+
coll.indexes.create('document_id' => 1)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
existing_indexes = []
|
|
116
|
+
coll.indexes.each { |index| existing_indexes << index }
|
|
117
|
+
definition = IndexDefinition.new(config[:filters], localized_fulltext_index?(config), existing_indexes)
|
|
118
|
+
definition.obsolete_indexes.each do |idef|
|
|
119
|
+
Mongoid.logger.info "Dropping #{idef['name']} [#{idef['key'].keys} <=> #{definition.keys.map(&:first)}]" if Mongoid.logger
|
|
120
|
+
if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
99
121
|
coll.indexes.drop_one(idef['key'])
|
|
100
122
|
else
|
|
101
123
|
coll.indexes.drop(idef['key'])
|
|
102
124
|
end
|
|
103
125
|
end
|
|
104
126
|
|
|
105
|
-
|
|
106
|
-
filter_indexes = all_filter_keys.map { |key| [key, 1] }.sort_by { |filter_index| filter_index[0] }
|
|
107
|
-
index_definition = [['ngram', 1], ['score', -1]].concat(filter_indexes)
|
|
108
|
-
end
|
|
109
|
-
|
|
127
|
+
index_definition = definition.keys
|
|
110
128
|
Mongoid.logger.info "Ensuring fts_index on #{coll.name}: #{index_definition}" if Mongoid.logger
|
|
111
|
-
if Mongoid::Compatibility::Version.
|
|
129
|
+
if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
112
130
|
coll.indexes.create_one(Hash[index_definition], name: 'fts_index')
|
|
113
131
|
else
|
|
114
132
|
coll.indexes.create(Hash[index_definition], name: 'fts_index')
|
|
115
133
|
end
|
|
116
|
-
|
|
117
|
-
Mongoid.logger.info "Ensuring document_id index on #{coll.name}" if Mongoid.logger
|
|
118
|
-
if Mongoid::Compatibility::Version.mongoid5? || Mongoid::Compatibility::Version.mongoid6?
|
|
119
|
-
coll.indexes.create_one('document_id' => 1) # to make removes fast
|
|
120
|
-
else
|
|
121
|
-
coll.indexes.create('document_id' => 1) # to make removes fast
|
|
122
|
-
end
|
|
123
134
|
end
|
|
124
135
|
|
|
125
136
|
def fulltext_search(query_string, options = {})
|
|
@@ -127,7 +138,7 @@ module Mongoid::FullTextSearch
|
|
|
127
138
|
return_scores = options.key?(:return_scores) ? options.delete(:return_scores) : false
|
|
128
139
|
if mongoid_fulltext_config.count > 1 && !options.key?(:index)
|
|
129
140
|
error_message = '%s is indexed by multiple full-text indexes. You must specify one by passing an :index_name parameter'
|
|
130
|
-
|
|
141
|
+
raise UnspecifiedIndexError, error_message % name, caller
|
|
131
142
|
end
|
|
132
143
|
index_name = options.key?(:index) ? options.delete(:index) : mongoid_fulltext_config.keys.first
|
|
133
144
|
|
|
@@ -140,10 +151,13 @@ module Mongoid::FullTextSearch
|
|
|
140
151
|
# get a count of the number of index documents containing that n-gram
|
|
141
152
|
ordering = { 'score' => -1 }
|
|
142
153
|
limit = mongoid_fulltext_config[index_name][:max_candidate_set_size]
|
|
154
|
+
config = mongoid_fulltext_config[index_name]
|
|
143
155
|
coll = collection.database[index_name]
|
|
144
156
|
cursors = ngrams.map do |ngram|
|
|
145
157
|
query = { 'ngram' => ngram[0] }
|
|
146
|
-
query.
|
|
158
|
+
query['locale'] = I18n.locale.to_s if localized_fulltext_index?(config)
|
|
159
|
+
query.update(document_type_filters)
|
|
160
|
+
query.update(map_query_filters(options))
|
|
147
161
|
count = coll.find(query).count
|
|
148
162
|
{ ngram: ngram, count: count, query: query }
|
|
149
163
|
end.sort! { |record1, record2| record1[:count] <=> record2[:count] }
|
|
@@ -156,6 +170,7 @@ module Mongoid::FullTextSearch
|
|
|
156
170
|
results_so_far = 0
|
|
157
171
|
candidates_list = cursors.map do |doc|
|
|
158
172
|
next if doc[:count] == 0
|
|
173
|
+
|
|
159
174
|
query_result = coll.find(doc[:query])
|
|
160
175
|
if results_so_far >= limit
|
|
161
176
|
query_result = query_result.sort(ordering).limit(max_results)
|
|
@@ -182,107 +197,43 @@ module Mongoid::FullTextSearch
|
|
|
182
197
|
scores = candidates.map do |candidate_id, data|
|
|
183
198
|
{ id: candidate_id,
|
|
184
199
|
clazz: data[:clazz],
|
|
185
|
-
score: data[:score] + candidates_list.map { |others| (others.delete(candidate_id) || { score: 0 })[:score] }.sum
|
|
186
|
-
}
|
|
200
|
+
score: data[:score] + candidates_list.map { |others| (others.delete(candidate_id) || { score: 0 })[:score] }.sum }
|
|
187
201
|
end
|
|
188
202
|
all_scores.concat(scores)
|
|
189
203
|
end
|
|
190
204
|
all_scores.sort! { |document1, document2| -document1[:score] <=> -document2[:score] }
|
|
191
|
-
|
|
205
|
+
if max_results > 0
|
|
206
|
+
instantiate_mapreduce_results(all_scores, return_scores: return_scores, max_results: max_results)
|
|
207
|
+
else
|
|
208
|
+
instantiate_mapreduce_results(all_scores[0..max_results - 1], return_scores: return_scores)
|
|
209
|
+
end
|
|
192
210
|
end
|
|
193
211
|
|
|
194
212
|
def instantiate_mapreduce_result(result)
|
|
195
|
-
|
|
213
|
+
scope = criteria
|
|
214
|
+
scope = result[:clazz].constantize.criteria if scope.selector.empty?
|
|
215
|
+
# Mongoid 7.0 and older replace an existing _id condition with where; and keeps both.
|
|
216
|
+
scope.and(_id: result[:id]).first
|
|
196
217
|
end
|
|
197
218
|
|
|
198
219
|
def instantiate_mapreduce_results(results, options)
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
220
|
+
results.each_with_object([]) do |result, documents|
|
|
221
|
+
document = instantiate_mapreduce_result(result)
|
|
222
|
+
next unless document
|
|
223
|
+
|
|
224
|
+
documents << (options[:return_scores] ? [document, result[:score]] : document)
|
|
225
|
+
break documents if options[:max_results] && documents.length >= options[:max_results]
|
|
203
226
|
end
|
|
204
227
|
end
|
|
205
228
|
|
|
206
229
|
def all_ngrams(str, config, bound_number_returned = true)
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
if config[:remove_accents]
|
|
210
|
-
if defined?(UnicodeUtils)
|
|
211
|
-
str = UnicodeUtils.nfkd(str)
|
|
212
|
-
elsif defined?(DiacriticsFu)
|
|
213
|
-
str = DiacriticsFu.escape(str)
|
|
214
|
-
end
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
# Remove any characters that aren't in the alphabet and aren't word separators
|
|
218
|
-
filtered_str = str.mb_chars.downcase.to_s.split('').find_all { |ch| config[:alphabet][ch] || config[:word_separators][ch] }.join('')
|
|
219
|
-
|
|
220
|
-
# Figure out how many ngrams to extract from the string. If we can't afford to extract all ngrams,
|
|
221
|
-
# step over the string in evenly spaced strides to extract ngrams. For example, to extract 3 3-letter
|
|
222
|
-
# ngrams from 'abcdefghijk', we'd want to extract 'abc', 'efg', and 'ijk'.
|
|
223
|
-
if bound_number_returned
|
|
224
|
-
step_size = [((filtered_str.length - config[:ngram_width]).to_f / config[:max_ngrams_to_search]).ceil, 1].max
|
|
225
|
-
else
|
|
226
|
-
step_size = 1
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
# Create an array of records of the form {:ngram => x, :score => y} for all ngrams that occur in the
|
|
230
|
-
# input string using the step size that we just computed. Let score(x,y) be the score of string x
|
|
231
|
-
# compared with string y - assigning scores to ngrams with the square root-based scoring function
|
|
232
|
-
# below and multiplying scores of matching ngrams together yields a score function that has the
|
|
233
|
-
# property that score(x,y) > score(x,z) for any string z containing y and score(x,y) > score(x,z)
|
|
234
|
-
# for any string z contained in y.
|
|
235
|
-
ngram_array = (0..filtered_str.length - config[:ngram_width]).step(step_size).map do |i|
|
|
236
|
-
if i == 0 || (config[:apply_prefix_scoring_to_all_words] && \
|
|
237
|
-
config[:word_separators].key?(filtered_str[i - 1].chr))
|
|
238
|
-
score = Math.sqrt(1 + 1.0 / filtered_str.length)
|
|
239
|
-
else
|
|
240
|
-
score = Math.sqrt(2.0 / filtered_str.length)
|
|
241
|
-
end
|
|
242
|
-
{ ngram: filtered_str[i..i + config[:ngram_width] - 1], score: score }
|
|
243
|
-
end
|
|
244
|
-
|
|
245
|
-
# If an ngram appears multiple times in the query string, keep the max score
|
|
246
|
-
ngram_array = ngram_array.group_by { |h| h[:ngram] }.map { |key, values| { ngram: key, score: values.map { |v| v[:score] }.max } }
|
|
247
|
-
|
|
248
|
-
if config[:index_short_prefixes] || config[:index_full_words]
|
|
249
|
-
split_regex_def = config[:word_separators].keys.map { |k| Regexp.escape(k) }.join
|
|
250
|
-
split_regex = Regexp.compile("[#{split_regex_def}]")
|
|
251
|
-
all_words = filtered_str.split(split_regex)
|
|
252
|
-
end
|
|
253
|
-
|
|
254
|
-
# Add 'short prefix' records to the array: prefixes of the string that are length (ngram_width - 1)
|
|
255
|
-
if config[:index_short_prefixes]
|
|
256
|
-
prefixes_seen = {}
|
|
257
|
-
all_words.each do |word|
|
|
258
|
-
next if word.length < config[:ngram_width] - 1
|
|
259
|
-
prefix = word[0...config[:ngram_width] - 1]
|
|
260
|
-
if prefixes_seen[prefix].nil? && (config[:stop_words][word].nil? || word == filtered_str)
|
|
261
|
-
ngram_array << { ngram: prefix, score: 1 + 1.0 / filtered_str.length }
|
|
262
|
-
prefixes_seen[prefix] = true
|
|
263
|
-
end
|
|
264
|
-
end
|
|
265
|
-
end
|
|
266
|
-
|
|
267
|
-
# Add records to the array of ngrams for each full word in the string that isn't a stop word
|
|
268
|
-
if config[:index_full_words]
|
|
269
|
-
full_words_seen = {}
|
|
270
|
-
all_words.each do |word|
|
|
271
|
-
if word.length > 1 && full_words_seen[word].nil? && (config[:stop_words][word].nil? || word == filtered_str)
|
|
272
|
-
ngram_array << { ngram: word, score: 1 + 1.0 / filtered_str.length }
|
|
273
|
-
full_words_seen[word] = true
|
|
274
|
-
end
|
|
275
|
-
end
|
|
276
|
-
end
|
|
277
|
-
|
|
278
|
-
# If an ngram appears as any combination of full word, short prefix, and ngram, keep the sum of the two scores
|
|
279
|
-
Hash[ngram_array.group_by { |h| h[:ngram] }.map { |key, values| [key, values.map { |v| v[:score] }.sum] }]
|
|
230
|
+
Ngrams.calculate(str, config, bound_number_returned)
|
|
280
231
|
end
|
|
281
232
|
|
|
282
233
|
def remove_from_ngram_index
|
|
283
234
|
mongoid_fulltext_config.each_pair do |index_name, _fulltext_config|
|
|
284
235
|
coll = collection.database[index_name]
|
|
285
|
-
if Mongoid::Compatibility::Version.
|
|
236
|
+
if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
286
237
|
coll.find('class' => name).delete_many
|
|
287
238
|
else
|
|
288
239
|
coll.find('class' => name).remove_all
|
|
@@ -290,21 +241,29 @@ module Mongoid::FullTextSearch
|
|
|
290
241
|
end
|
|
291
242
|
end
|
|
292
243
|
|
|
293
|
-
def update_ngram_index
|
|
294
|
-
all
|
|
244
|
+
def update_ngram_index(options = { timeout: true })
|
|
245
|
+
items = all
|
|
246
|
+
items = items.no_timeout if options.key?(:timeout) && !options[:timeout]
|
|
247
|
+
items.each(&:update_ngram_index)
|
|
295
248
|
end
|
|
296
249
|
|
|
297
250
|
private
|
|
298
251
|
|
|
252
|
+
def document_type_filters
|
|
253
|
+
return {} if criteria.selector.empty? && descendants.empty? && !superclass.respond_to?(:mongoid_fulltext_config)
|
|
254
|
+
|
|
255
|
+
{ 'class' => { '$in' => ([self] + descendants).map(&:name) } }
|
|
256
|
+
end
|
|
257
|
+
|
|
299
258
|
# Take a list of filters to be mapped so they can update the query
|
|
300
259
|
# used upon the fulltext search of the ngrams
|
|
301
260
|
def map_query_filters(filters)
|
|
302
|
-
Hash[filters.map do|key, value|
|
|
261
|
+
Hash[filters.map do |key, value|
|
|
303
262
|
case value
|
|
304
|
-
when Hash
|
|
263
|
+
when Hash
|
|
305
264
|
if value.key? :any then format_query_filter('$in', key, value[:any])
|
|
306
265
|
elsif value.key? :all then format_query_filter('$all', key, value[:all])
|
|
307
|
-
else
|
|
266
|
+
else raise UnknownFilterQueryOperator, value.keys.join(','), caller end
|
|
308
267
|
else format_query_filter('$all', key, value)
|
|
309
268
|
end
|
|
310
269
|
end]
|
|
@@ -316,7 +275,7 @@ module Mongoid::FullTextSearch
|
|
|
316
275
|
end
|
|
317
276
|
|
|
318
277
|
def update_ngram_index
|
|
319
|
-
|
|
278
|
+
self.class.each_fulltext_index do |index_name, fulltext_config, locale|
|
|
320
279
|
if condition = fulltext_config[:update_if]
|
|
321
280
|
case condition
|
|
322
281
|
when Symbol then next unless send condition
|
|
@@ -328,35 +287,44 @@ module Mongoid::FullTextSearch
|
|
|
328
287
|
|
|
329
288
|
# remove existing ngrams from external index
|
|
330
289
|
coll = collection.database[index_name.to_sym]
|
|
331
|
-
|
|
332
|
-
|
|
290
|
+
removal_query = { 'document_id' => _id }
|
|
291
|
+
removal_query['locale'] = locale if locale
|
|
292
|
+
if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
293
|
+
coll.find(removal_query).delete_many
|
|
333
294
|
else
|
|
334
|
-
coll.find(
|
|
295
|
+
coll.find(removal_query).remove_all
|
|
335
296
|
end
|
|
336
297
|
# extract ngrams from fields
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
298
|
+
ngrams = if fulltext_config.key?(:priorities)
|
|
299
|
+
FieldScores.calculate(fulltext_config) { |field| send(field) }
|
|
300
|
+
else
|
|
301
|
+
field_values = fulltext_config[:ngram_fields].flat_map { |field| send(field) }
|
|
302
|
+
field_values.inject({}) { |accum, item| accum.update(self.class.all_ngrams(item, fulltext_config, false)) }
|
|
303
|
+
end
|
|
304
|
+
next if ngrams.empty?
|
|
305
|
+
|
|
340
306
|
# apply filters, if necessary
|
|
341
307
|
filter_values = nil
|
|
342
308
|
if fulltext_config.key?(:filters)
|
|
343
309
|
filter_values = Hash[fulltext_config[:filters].map do |key, value|
|
|
344
310
|
begin
|
|
345
311
|
[key, value.call(self)]
|
|
346
|
-
rescue
|
|
312
|
+
rescue StandardError
|
|
347
313
|
# Suppress any exceptions caused by filters
|
|
348
314
|
end
|
|
349
315
|
end.compact]
|
|
350
316
|
end
|
|
351
317
|
# insert new ngrams in external index
|
|
352
|
-
ngrams.
|
|
318
|
+
index_documents = ngrams.map do |ngram, score|
|
|
353
319
|
index_document = { 'ngram' => ngram, 'document_id' => _id, 'score' => score, 'class' => self.class.name }
|
|
320
|
+
index_document['locale'] = locale if locale
|
|
354
321
|
index_document['filter_values'] = filter_values if fulltext_config.key?(:filters)
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
322
|
+
index_document
|
|
323
|
+
end
|
|
324
|
+
if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
325
|
+
coll.insert_many(index_documents)
|
|
326
|
+
else
|
|
327
|
+
coll.insert(index_documents)
|
|
360
328
|
end
|
|
361
329
|
end
|
|
362
330
|
end
|
|
@@ -364,7 +332,7 @@ module Mongoid::FullTextSearch
|
|
|
364
332
|
def remove_from_ngram_index
|
|
365
333
|
mongoid_fulltext_config.each_pair do |index_name, _fulltext_config|
|
|
366
334
|
coll = collection.database[index_name]
|
|
367
|
-
if Mongoid::Compatibility::Version.
|
|
335
|
+
if Mongoid::Compatibility::Version.mongoid5_or_newer?
|
|
368
336
|
coll.find('document_id' => _id).delete_many
|
|
369
337
|
else
|
|
370
338
|
coll.find('document_id' => _id).remove_all
|
data/mongoid_fulltext.gemspec
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
require 'mongoid/full_text_search/version'
|
|
1
|
+
require_relative 'lib/mongoid/full_text_search/version'
|
|
3
2
|
|
|
4
3
|
Gem::Specification.new do |s|
|
|
5
4
|
s.name = 'mongoid_fulltext'
|
|
@@ -13,7 +12,8 @@ Gem::Specification.new do |s|
|
|
|
13
12
|
s.homepage = 'https://github.com/mongoid/mongoid_fulltext'
|
|
14
13
|
s.licenses = ['MIT']
|
|
15
14
|
s.summary = 'Full-text search for the Mongoid ORM, using n-grams extracted from text.'
|
|
16
|
-
s.add_dependency 'mongoid', '>= 3.0'
|
|
17
|
-
s.add_dependency 'mongoid-compatibility'
|
|
15
|
+
s.add_dependency 'mongoid', '>= 3.0', '< 10'
|
|
16
|
+
s.add_dependency 'mongoid-compatibility', '>= 0.5.1'
|
|
18
17
|
s.add_dependency 'unicode_utils'
|
|
18
|
+
s.add_development_dependency 'database_cleaner'
|
|
19
19
|
end
|
data/script/release.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'ruby-gem-release/changelog'
|
|
2
|
+
require 'ruby-gem-release/workflow/ruby_gem'
|
|
3
|
+
|
|
4
|
+
# Builds the ruby-gem-release workflow used by the Rakefile, the Release GitHub Actions workflow, and spec/release.
|
|
5
|
+
module MongoidFulltextRelease
|
|
6
|
+
def self.workflow(**options)
|
|
7
|
+
RubyGemRelease::Workflow::RubyGem.new(
|
|
8
|
+
version: RubyGemRelease::Version::RubyConstant.new(path: 'lib/mongoid/full_text_search/version.rb', constant: 'VERSION'),
|
|
9
|
+
changelog: RubyGemRelease::Changelog::Intridea.new(placeholder: '* Your contribution here.'),
|
|
10
|
+
**options
|
|
11
|
+
)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -5,6 +5,5 @@ class FilteredArtist
|
|
|
5
5
|
fulltext_search_in :full_name, index_name: 'mongoid_fulltext.artworks_and_artists',
|
|
6
6
|
filters: { is_foobar: ->(x) { x.full_name == 'foobar' },
|
|
7
7
|
is_artist: ->(_x) { true },
|
|
8
|
-
is_artwork: ->(_x) { false }
|
|
9
|
-
}
|
|
8
|
+
is_artwork: ->(_x) { false } }
|
|
10
9
|
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
class LocalizedArtwork
|
|
2
|
+
include Mongoid::Document
|
|
3
|
+
include Mongoid::FullTextSearch
|
|
4
|
+
|
|
5
|
+
field :title, type: String, localize: true
|
|
6
|
+
field :artist, type: String
|
|
7
|
+
fulltext_search_in :title, :artist, :description, filters: { language: ->(_document) { I18n.locale.to_s } }
|
|
8
|
+
|
|
9
|
+
def description
|
|
10
|
+
I18n.locale == :cs ? 'Cesky' : 'English'
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class PartlyLocalizedArtwork
|
|
15
|
+
include Mongoid::Document
|
|
16
|
+
include Mongoid::FullTextSearch
|
|
17
|
+
|
|
18
|
+
field :title, type: String, localize: true
|
|
19
|
+
field :artist, type: String
|
|
20
|
+
fulltext_search_in :artist, index_name: 'mongoid_fulltext.untranslated'
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
class OnlyLocalizedArtwork
|
|
24
|
+
include Mongoid::Document
|
|
25
|
+
include Mongoid::FullTextSearch
|
|
26
|
+
|
|
27
|
+
field :title, type: String, localize: true
|
|
28
|
+
fulltext_search_in :title
|
|
29
|
+
end
|
|
@@ -8,7 +8,7 @@ class PartitionedArtist
|
|
|
8
8
|
fulltext_search_in :full_name,
|
|
9
9
|
index_name: 'mongoid_fulltext.partitioned_artists',
|
|
10
10
|
filters: {
|
|
11
|
-
has_exhibitions: ->(x) { x.exhibitions.
|
|
11
|
+
has_exhibitions: ->(x) { !x.exhibitions.empty? },
|
|
12
12
|
exhibitions: ->(x) { [x.exhibitions].flatten }
|
|
13
13
|
}
|
|
14
14
|
end
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
1
|
class RussianArtwork
|
|
3
2
|
include Mongoid::Document
|
|
4
3
|
include Mongoid::FullTextSearch
|
|
5
4
|
|
|
6
|
-
Alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789абвгдежзиклмнопрстуфхцчшщъыьэюя'
|
|
5
|
+
Alphabet = 'abcdefghijklmnopqrstuvwxyz0123456789абвгдежзиклмнопрстуфхцчшщъыьэюя'.freeze
|
|
7
6
|
|
|
8
7
|
field :title
|
|
9
8
|
fulltext_search_in :title, alphabet: Alphabet
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class ScopedArtwork
|
|
2
|
+
include Mongoid::Document
|
|
3
|
+
include Mongoid::FullTextSearch
|
|
4
|
+
|
|
5
|
+
field :title, type: String
|
|
6
|
+
field :value, type: Integer
|
|
7
|
+
fulltext_search_in :title
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
class InheritedArtwork < ScopedArtwork
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class FurtherInheritedArtwork < InheritedArtwork
|
|
14
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
class WeightedArtwork
|
|
2
|
+
include Mongoid::Document
|
|
3
|
+
include Mongoid::FullTextSearch
|
|
4
|
+
|
|
5
|
+
field :title, type: String
|
|
6
|
+
field :description, type: String
|
|
7
|
+
field :tags, type: Array
|
|
8
|
+
fulltext_search_in :title, :description, :tags, index_name: 'weighted_max',
|
|
9
|
+
priorities: { title: 10, 'description' => 5 }
|
|
10
|
+
fulltext_search_in :tags, :description, :title, index_name: 'weighted_sum',
|
|
11
|
+
priorities: { title: 10, description: 5 }, score_aggregation: :sum
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class WeightedComputedArtwork
|
|
15
|
+
include Mongoid::Document
|
|
16
|
+
include Mongoid::FullTextSearch
|
|
17
|
+
|
|
18
|
+
field :title, type: String
|
|
19
|
+
fulltext_search_in :title, :search_text, priorities: { search_text: 2.5 }, score_aggregation: :sum
|
|
20
|
+
|
|
21
|
+
def search_text
|
|
22
|
+
title
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class WeightedLocalizedArtwork
|
|
27
|
+
include Mongoid::Document
|
|
28
|
+
include Mongoid::FullTextSearch
|
|
29
|
+
|
|
30
|
+
field :title, type: String, localize: true
|
|
31
|
+
field :description, type: String
|
|
32
|
+
fulltext_search_in :title, :description, priorities: { title: 10 },
|
|
33
|
+
filters: { language: ->(_document) { I18n.locale.to_s } }
|
|
34
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Array fields' do
|
|
4
|
+
it 'searches each tag as well as scalar fields' do
|
|
5
|
+
artwork = TaggedArtwork.create!(title: 'Painting', tags: %w[ghost accounting])
|
|
6
|
+
%w[painting ghost accounting].each do |query|
|
|
7
|
+
expect(TaggedArtwork.fulltext_search(query)).to eq([artwork])
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'does not create ngrams across array element boundaries' do
|
|
12
|
+
artwork = TaggedArtwork.create!(tags: %w[ab cd])
|
|
13
|
+
config = TaggedArtwork.mongoid_fulltext_config.values.first
|
|
14
|
+
coll = artwork.collection.database[TaggedArtwork.mongoid_fulltext_config.keys.first]
|
|
15
|
+
expected = %w[ab cd].inject({}) do |ngrams, tag|
|
|
16
|
+
ngrams.update(TaggedArtwork.all_ngrams(tag, config, false))
|
|
17
|
+
end
|
|
18
|
+
actual = Hash[coll.find.map { |document| [document['ngram'], document['score']] }]
|
|
19
|
+
expect(actual).to eq(expected)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'handles nil, empty, and repeated elements without duplicate records' do
|
|
23
|
+
artwork = TaggedArtwork.create!(tags: [nil, '', 'ghost', 'ghost'])
|
|
24
|
+
expect(TaggedArtwork.fulltext_search('ghost')).to eq([artwork])
|
|
25
|
+
coll = artwork.collection.database[TaggedArtwork.mongoid_fulltext_config.keys.first]
|
|
26
|
+
ngrams = coll.find.map { |document| document['ngram'] }
|
|
27
|
+
expect(ngrams).to eq(ngrams.uniq)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'removes old tags when the array changes or becomes empty' do
|
|
31
|
+
artwork = TaggedArtwork.create!(tags: ['ghost'])
|
|
32
|
+
artwork.update_attributes!(tags: ['accounting'])
|
|
33
|
+
expect(TaggedArtwork.fulltext_search('ghost')).to eq([])
|
|
34
|
+
expect(TaggedArtwork.fulltext_search('accounting')).to eq([artwork])
|
|
35
|
+
artwork.update_attributes!(tags: [])
|
|
36
|
+
expect(TaggedArtwork.fulltext_search('accounting')).to eq([])
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'still indexes scalar fields when the array is nil' do
|
|
40
|
+
artwork = TaggedArtwork.create!(title: 'Painting', tags: nil)
|
|
41
|
+
expect(TaggedArtwork.fulltext_search('painting')).to eq([artwork])
|
|
42
|
+
end
|
|
43
|
+
end
|