chewy 0.8.4 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +34 -0
- data/.rubocop_todo.yml +44 -0
- data/.travis.yml +20 -60
- data/Appraisals +15 -40
- data/CHANGELOG.md +42 -0
- data/Gemfile +1 -0
- data/Guardfile +5 -5
- data/README.md +155 -6
- data/Rakefile +11 -1
- data/chewy.gemspec +5 -7
- data/gemfiles/rails.3.2.activerecord.gemfile +1 -0
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +1 -0
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +1 -0
- data/gemfiles/rails.4.2.activerecord.gemfile +1 -0
- data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +1 -0
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +1 -0
- data/gemfiles/{rails.4.2.mongoid.4.0.0.gemfile → rails.4.2.mongoid.4.0.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.4.0.0.kaminari.gemfile → rails.4.2.mongoid.4.0.kaminari.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.4.0.0.will_paginate.gemfile → rails.4.2.mongoid.4.0.will_paginate.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.5.1.0.gemfile → rails.4.2.mongoid.5.1.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.5.1.0.kaminari.gemfile → rails.4.2.mongoid.5.1.kaminari.gemfile} +2 -1
- data/gemfiles/{rails.4.2.mongoid.5.1.0.will_paginate.gemfile → rails.4.2.mongoid.5.1.will_paginate.gemfile} +2 -1
- data/gemfiles/{rails.4.0.activerecord.gemfile → rails.5.0.activerecord.gemfile} +4 -2
- data/gemfiles/{rails.4.0.mongoid.4.0.0.kaminari.gemfile → rails.5.0.activerecord.kaminari.gemfile} +4 -2
- data/gemfiles/{rails.4.0.mongoid.4.0.0.will_paginate.gemfile → rails.5.0.activerecord.will_paginate.gemfile} +4 -2
- data/gemfiles/{sequel.4.31.gemfile → sequel.4.38.gemfile} +3 -2
- data/lib/chewy.rb +24 -16
- data/lib/chewy/backports/deep_dup.rb +1 -1
- data/lib/chewy/backports/duplicable.rb +1 -0
- data/lib/chewy/config.rb +13 -7
- data/lib/chewy/errors.rb +4 -4
- data/lib/chewy/fields/base.rb +19 -14
- data/lib/chewy/fields/root.rb +11 -9
- data/lib/chewy/index.rb +38 -25
- data/lib/chewy/index/actions.rb +17 -17
- data/lib/chewy/index/settings.rb +3 -4
- data/lib/chewy/journal.rb +107 -0
- data/lib/chewy/journal/apply.rb +31 -0
- data/lib/chewy/journal/clean.rb +24 -0
- data/lib/chewy/journal/entry.rb +83 -0
- data/lib/chewy/journal/query.rb +87 -0
- data/lib/chewy/log_subscriber.rb +8 -8
- data/lib/chewy/minitest.rb +1 -0
- data/lib/chewy/minitest/helpers.rb +77 -0
- data/lib/chewy/minitest/search_index_receiver.rb +80 -0
- data/lib/chewy/query.rb +116 -60
- data/lib/chewy/query/compose.rb +5 -6
- data/lib/chewy/query/criteria.rb +26 -16
- data/lib/chewy/query/filters.rb +9 -9
- data/lib/chewy/query/loading.rb +2 -2
- data/lib/chewy/query/nodes/and.rb +3 -3
- data/lib/chewy/query/nodes/base.rb +1 -1
- data/lib/chewy/query/nodes/bool.rb +6 -4
- data/lib/chewy/query/nodes/equal.rb +6 -6
- data/lib/chewy/query/nodes/exists.rb +2 -2
- data/lib/chewy/query/nodes/expr.rb +2 -2
- data/lib/chewy/query/nodes/field.rb +35 -31
- data/lib/chewy/query/nodes/has_child.rb +1 -0
- data/lib/chewy/query/nodes/has_parent.rb +1 -0
- data/lib/chewy/query/nodes/has_relation.rb +11 -13
- data/lib/chewy/query/nodes/match_all.rb +1 -1
- data/lib/chewy/query/nodes/missing.rb +2 -2
- data/lib/chewy/query/nodes/not.rb +3 -3
- data/lib/chewy/query/nodes/or.rb +3 -3
- data/lib/chewy/query/nodes/prefix.rb +4 -3
- data/lib/chewy/query/nodes/query.rb +3 -3
- data/lib/chewy/query/nodes/range.rb +11 -11
- data/lib/chewy/query/nodes/raw.rb +1 -1
- data/lib/chewy/query/nodes/regexp.rb +15 -11
- data/lib/chewy/query/nodes/script.rb +6 -6
- data/lib/chewy/query/pagination/will_paginate.rb +2 -2
- data/lib/chewy/railtie.rb +3 -3
- data/lib/chewy/rake_helper.rb +51 -30
- data/lib/chewy/repository.rb +2 -2
- data/lib/chewy/rspec.rb +1 -1
- data/lib/chewy/rspec/update_index.rb +46 -47
- data/lib/chewy/runtime/version.rb +4 -4
- data/lib/chewy/search.rb +7 -5
- data/lib/chewy/strategy.rb +10 -8
- data/lib/chewy/strategy/atomic.rb +2 -2
- data/lib/chewy/strategy/base.rb +4 -4
- data/lib/chewy/strategy/bypass.rb +1 -2
- data/lib/chewy/strategy/sidekiq.rb +2 -0
- data/lib/chewy/strategy/urgent.rb +1 -1
- data/lib/chewy/type.rb +51 -45
- data/lib/chewy/type/adapter/active_record.rb +23 -12
- data/lib/chewy/type/adapter/base.rb +4 -4
- data/lib/chewy/type/adapter/mongoid.rb +6 -6
- data/lib/chewy/type/adapter/object.rb +15 -12
- data/lib/chewy/type/adapter/orm.rb +24 -15
- data/lib/chewy/type/adapter/sequel.rb +11 -7
- data/lib/chewy/type/crutch.rb +4 -3
- data/lib/chewy/type/import.rb +51 -32
- data/lib/chewy/type/mapping.rb +17 -17
- data/lib/chewy/type/observe.rb +9 -7
- data/lib/chewy/type/witchcraft.rb +62 -23
- data/lib/chewy/type/wrapper.rb +20 -14
- data/lib/chewy/version.rb +1 -1
- data/lib/generators/chewy/install_generator.rb +3 -3
- data/lib/tasks/chewy.rake +28 -23
- data/spec/chewy/config_spec.rb +33 -12
- data/spec/chewy/fields/base_spec.rb +143 -154
- data/spec/chewy/fields/root_spec.rb +22 -20
- data/spec/chewy/fields/time_fields_spec.rb +11 -9
- data/spec/chewy/index/actions_spec.rb +27 -4
- data/spec/chewy/index/aliases_spec.rb +2 -2
- data/spec/chewy/index/settings_spec.rb +72 -50
- data/spec/chewy/index_spec.rb +95 -43
- data/spec/chewy/journal/apply_spec.rb +120 -0
- data/spec/chewy/journal/entry_spec.rb +237 -0
- data/spec/chewy/journal_spec.rb +173 -0
- data/spec/chewy/minitest/helpers_spec.rb +90 -0
- data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
- data/spec/chewy/query/criteria_spec.rb +504 -237
- data/spec/chewy/query/filters_spec.rb +94 -66
- data/spec/chewy/query/loading_spec.rb +76 -40
- data/spec/chewy/query/nodes/and_spec.rb +3 -7
- data/spec/chewy/query/nodes/bool_spec.rb +5 -13
- data/spec/chewy/query/nodes/equal_spec.rb +20 -20
- data/spec/chewy/query/nodes/exists_spec.rb +7 -7
- data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
- data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
- data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
- data/spec/chewy/query/nodes/missing_spec.rb +6 -5
- data/spec/chewy/query/nodes/not_spec.rb +3 -7
- data/spec/chewy/query/nodes/or_spec.rb +3 -7
- data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
- data/spec/chewy/query/nodes/query_spec.rb +3 -3
- data/spec/chewy/query/nodes/range_spec.rb +19 -19
- data/spec/chewy/query/nodes/raw_spec.rb +2 -2
- data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
- data/spec/chewy/query/nodes/script_spec.rb +5 -5
- data/spec/chewy/query/pagination/kaminari_spec.rb +2 -2
- data/spec/chewy/query/pagination/will_paginage_spec.rb +6 -7
- data/spec/chewy/query/pagination_spec.rb +2 -3
- data/spec/chewy/query_spec.rb +208 -145
- data/spec/chewy/repository_spec.rb +8 -8
- data/spec/chewy/rspec/update_index_spec.rb +180 -111
- data/spec/chewy/search_spec.rb +8 -8
- data/spec/chewy/strategy/active_job_spec.rb +2 -2
- data/spec/chewy/strategy/atomic_spec.rb +4 -1
- data/spec/chewy/strategy/resque_spec.rb +2 -2
- data/spec/chewy/strategy/sidekiq_spec.rb +2 -2
- data/spec/chewy/type/actions_spec.rb +1 -1
- data/spec/chewy/type/adapter/active_record_spec.rb +255 -149
- data/spec/chewy/type/adapter/mongoid_spec.rb +169 -108
- data/spec/chewy/type/adapter/object_spec.rb +56 -40
- data/spec/chewy/type/adapter/sequel_spec.rb +248 -163
- data/spec/chewy/type/import_spec.rb +78 -47
- data/spec/chewy/type/mapping_spec.rb +6 -6
- data/spec/chewy/type/observe_spec.rb +20 -14
- data/spec/chewy/type/witchcraft_spec.rb +89 -43
- data/spec/chewy/type_spec.rb +4 -3
- data/spec/chewy_spec.rb +10 -8
- data/spec/spec_helper.rb +3 -0
- data/spec/support/active_record.rb +1 -1
- data/spec/support/class_helpers.rb +10 -11
- data/spec/support/mongoid.rb +2 -2
- data/spec/support/sequel.rb +1 -1
- metadata +65 -35
- data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
- data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
data/lib/chewy/repository.rb
CHANGED
@@ -5,10 +5,10 @@ module Chewy
|
|
5
5
|
attr_reader :analyzers, :tokenizers, :filters, :char_filters
|
6
6
|
|
7
7
|
def self.delegated
|
8
|
-
public_instance_methods -
|
8
|
+
public_instance_methods - superclass.public_instance_methods - Singleton.public_instance_methods
|
9
9
|
end
|
10
10
|
|
11
|
-
def self.repository
|
11
|
+
def self.repository(name)
|
12
12
|
plural_name = name.to_s.pluralize
|
13
13
|
|
14
14
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
data/lib/chewy/rspec.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'chewy/rspec/update_index'
|
1
|
+
require 'chewy/rspec/update_index'
|
@@ -20,11 +20,10 @@ require 'i18n/core_ext/hash'
|
|
20
20
|
# specify { expect { user1.destroy!; user2.save! } }
|
21
21
|
# .to update_index(UsersIndex::User).and_reindex(user2).and_delete(user1) }
|
22
22
|
#
|
23
|
-
RSpec::Matchers.define :update_index do |type_name, options = {}|
|
24
|
-
|
23
|
+
RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disable BlockLength
|
25
24
|
if !respond_to?(:failure_message) && respond_to?(:failure_message_for_should)
|
26
|
-
|
27
|
-
|
25
|
+
alias_method :failure_message, :failure_message_for_should
|
26
|
+
alias_method :failure_message_when_negated, :failure_message_for_should_not
|
28
27
|
end
|
29
28
|
|
30
29
|
# Specify indexed records by passing record itself or id.
|
@@ -83,7 +82,11 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
83
82
|
# specify { expect { [user1, user2].map(&:save!) }
|
84
83
|
# .to update_index(UsersIndex.user).and_reindex(user1).only }
|
85
84
|
#
|
86
|
-
chain(:only) do |*
|
85
|
+
chain(:only) do |*_args|
|
86
|
+
if @reindex.blank? && @delete.blank?
|
87
|
+
raise 'Use `only` in conjunction with `and_reindex` or `and_delete`'
|
88
|
+
end
|
89
|
+
|
87
90
|
@only = true
|
88
91
|
end
|
89
92
|
|
@@ -91,17 +94,16 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
91
94
|
true
|
92
95
|
end
|
93
96
|
|
94
|
-
match do |block|
|
97
|
+
match do |block| # rubocop:disable BlockLength
|
95
98
|
@reindex ||= {}
|
96
99
|
@delete ||= {}
|
97
100
|
@missed_reindex = []
|
98
101
|
@missed_delete = []
|
99
102
|
@updated = []
|
100
103
|
|
101
|
-
type = Chewy.derive_type(type_name)
|
102
|
-
|
103
104
|
instance_eval <<-RUBY, __FILE__, __LINE__ + 1
|
104
|
-
|
105
|
+
type = Chewy.derive_type(type_name)
|
106
|
+
#{agnostic_stub} do |bulk_options|
|
105
107
|
@updated += bulk_options[:body].map do |updated_document|
|
106
108
|
updated_document.deep_symbolize_keys
|
107
109
|
end
|
@@ -113,18 +115,18 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
113
115
|
Chewy.strategy(options[:strategy] || :atomic) { block.call }
|
114
116
|
|
115
117
|
@updated.each do |updated_document|
|
116
|
-
if body = updated_document[:index]
|
117
|
-
if document = @reindex[body[:_id].to_s]
|
118
|
+
if (body = updated_document[:index])
|
119
|
+
if (document = @reindex[body[:_id].to_s])
|
118
120
|
document[:real_count] += 1
|
119
121
|
document[:real_attributes].merge!(body[:data])
|
120
|
-
|
121
|
-
@missed_reindex.push(body[:_id].to_s)
|
122
|
+
elsif @only
|
123
|
+
@missed_reindex.push(body[:_id].to_s)
|
122
124
|
end
|
123
|
-
elsif body = updated_document[:delete]
|
124
|
-
if document = @delete[body[:_id].to_s]
|
125
|
+
elsif (body = updated_document[:delete])
|
126
|
+
if (document = @delete[body[:_id].to_s])
|
125
127
|
document[:real_count] += 1
|
126
|
-
|
127
|
-
@missed_delete.push(body[:_id].to_s)
|
128
|
+
elsif @only
|
129
|
+
@missed_delete.push(body[:_id].to_s)
|
128
130
|
end
|
129
131
|
end
|
130
132
|
end
|
@@ -141,11 +143,11 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
141
143
|
end
|
142
144
|
|
143
145
|
@updated.present? && @missed_reindex.none? && @missed_delete.none? &&
|
144
|
-
|
145
|
-
|
146
|
+
@reindex.all? { |_, document| document[:match_count] && document[:match_attributes] } &&
|
147
|
+
@delete.all? { |_, document| document[:match_count] }
|
146
148
|
end
|
147
149
|
|
148
|
-
failure_message do
|
150
|
+
failure_message do # rubocop:disable BlockLength
|
149
151
|
output = ''
|
150
152
|
|
151
153
|
if @updated.none?
|
@@ -166,28 +168,28 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
166
168
|
output << message
|
167
169
|
end
|
168
170
|
|
169
|
-
output << @reindex.each.with_object('') do |(id, document),
|
171
|
+
output << @reindex.each.with_object('') do |(id, document), result|
|
170
172
|
unless document[:match_count] && document[:match_attributes]
|
171
|
-
|
173
|
+
result << "Expected document with id `#{id}` to be reindexed"
|
172
174
|
if document[:real_count] > 0
|
173
|
-
|
174
|
-
|
175
|
+
result << "\n #{document[:expected_count]} times, but was reindexed #{document[:real_count]} times" if document[:expected_count] && !document[:match_count]
|
176
|
+
result << "\n with #{document[:expected_attributes]}, but it was reindexed with #{document[:real_attributes]}" if document[:expected_attributes].present? && !document[:match_attributes]
|
175
177
|
else
|
176
|
-
|
178
|
+
result << ', but it was not'
|
177
179
|
end
|
178
|
-
|
180
|
+
result << "\n"
|
179
181
|
end
|
180
182
|
end
|
181
183
|
|
182
|
-
output << @delete.each.with_object('') do |(id, document),
|
184
|
+
output << @delete.each.with_object('') do |(id, document), result|
|
183
185
|
unless document[:match_count]
|
184
|
-
|
185
|
-
if document[:real_count] > 0 && document[:expected_count]
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
186
|
+
result << "Expected document with id `#{id}` to be deleted"
|
187
|
+
result << if document[:real_count] > 0 && document[:expected_count]
|
188
|
+
"\n #{document[:expected_count]} times, but was deleted #{document[:real_count]} times"
|
189
|
+
else
|
190
|
+
', but it was not'
|
191
|
+
end
|
192
|
+
result << "\n"
|
191
193
|
end
|
192
194
|
end
|
193
195
|
|
@@ -196,23 +198,21 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
196
198
|
|
197
199
|
failure_message_when_negated do
|
198
200
|
if @updated.present?
|
199
|
-
"Expected index `#{type_name}` not to be updated, but it was with #{
|
200
|
-
|
201
|
-
|
202
|
-
end.join
|
203
|
-
}\n"
|
201
|
+
"Expected index `#{type_name}` not to be updated, but it was with #{@updated.map(&:values).flatten.group_by { |documents| documents[:_id] }.map do |id, documents|
|
202
|
+
"\n document id `#{id}` (#{documents.count} times)"
|
203
|
+
end.join}\n"
|
204
204
|
end
|
205
205
|
end
|
206
206
|
|
207
207
|
def agnostic_stub
|
208
208
|
if defined?(Mocha) && RSpec.configuration.mock_framework.to_s == 'RSpec::Core::MockingAdapters::Mocha'
|
209
|
-
|
209
|
+
'type.stubs(:bulk).with'
|
210
210
|
else
|
211
|
-
|
211
|
+
'allow(type).to receive(:bulk)'
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
215
|
-
def extract_documents
|
215
|
+
def extract_documents(*args)
|
216
216
|
options = args.extract_options!
|
217
217
|
|
218
218
|
expected_count = options[:times] || options[:count]
|
@@ -230,7 +230,7 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
230
230
|
end]
|
231
231
|
end
|
232
232
|
|
233
|
-
def compare_attributes
|
233
|
+
def compare_attributes(expected, real)
|
234
234
|
expected.inject(true) do |result, (key, value)|
|
235
235
|
equal = if value.is_a?(Array) && real[key].is_a?(Array)
|
236
236
|
array_difference(value, real[key]) && array_difference(real[key], value)
|
@@ -243,12 +243,11 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
243
243
|
end
|
244
244
|
end
|
245
245
|
|
246
|
-
def array_difference
|
246
|
+
def array_difference(first, second)
|
247
247
|
difference = first.to_ary.dup
|
248
248
|
second.to_ary.each do |element|
|
249
|
-
|
250
|
-
|
251
|
-
end
|
249
|
+
index = difference.index(element)
|
250
|
+
difference.delete_at(index) if index
|
252
251
|
end
|
253
252
|
difference.none?
|
254
253
|
end
|
@@ -4,21 +4,21 @@ module Chewy
|
|
4
4
|
include Comparable
|
5
5
|
attr_reader :major, :minor, :patch
|
6
6
|
|
7
|
-
def initialize
|
8
|
-
@major, @minor, @patch = *(version.to_s.split('.', 3) + [0]*3).first(3).map(&:to_i)
|
7
|
+
def initialize(version)
|
8
|
+
@major, @minor, @patch = *(version.to_s.split('.', 3) + [0] * 3).first(3).map(&:to_i)
|
9
9
|
end
|
10
10
|
|
11
11
|
def to_s
|
12
12
|
[major, minor, patch].join('.')
|
13
13
|
end
|
14
14
|
|
15
|
-
def <=>
|
15
|
+
def <=>(other)
|
16
16
|
other = self.class.new(other) unless other.is_a?(self.class)
|
17
17
|
[
|
18
18
|
major <=> other.major,
|
19
19
|
minor <=> other.minor,
|
20
20
|
patch <=> other.patch
|
21
|
-
].detect
|
21
|
+
].detect(&:nonzero?) || 0
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/chewy/search.rb
CHANGED
@@ -7,10 +7,11 @@ module Chewy
|
|
7
7
|
included do
|
8
8
|
singleton_class.delegate :explain, :query_mode, :filter_mode, :post_filter_mode,
|
9
9
|
:timeout, :limit, :offset, :highlight, :min_score, :rescore, :facets, :script_score,
|
10
|
-
:boost_factor, :random_score, :field_value_factor, :decay, :aggregations,
|
10
|
+
:boost_factor, :weight, :random_score, :field_value_factor, :decay, :aggregations,
|
11
11
|
:suggest, :none, :strategy, :query, :filter, :post_filter, :boost_mode,
|
12
12
|
:score_mode, :order, :reorder, :only, :types, :delete_all, :find, :total,
|
13
|
-
:total_count, :total_entries,
|
13
|
+
:total_count, :total_entries, :unlimited, :script_fields, :track_scores, :preference,
|
14
|
+
to: :all
|
14
15
|
end
|
15
16
|
|
16
17
|
module ClassMethods
|
@@ -18,11 +19,12 @@ module Chewy
|
|
18
19
|
query_class.scopes.last || query_class.new(self)
|
19
20
|
end
|
20
21
|
|
21
|
-
def search_string
|
22
|
+
def search_string(query, options = {})
|
22
23
|
options = options.merge(
|
23
24
|
index: all._indexes.map(&:index_name),
|
24
25
|
type: all._types.map(&:type_name),
|
25
|
-
q: query
|
26
|
+
q: query
|
27
|
+
)
|
26
28
|
Chewy.client.search(options)
|
27
29
|
end
|
28
30
|
|
@@ -42,7 +44,7 @@ module Chewy
|
|
42
44
|
end
|
43
45
|
end
|
44
46
|
|
45
|
-
def delegate_scoped
|
47
|
+
def delegate_scoped(source, destination, methods)
|
46
48
|
methods.each do |method|
|
47
49
|
destination.class_eval do
|
48
50
|
define_method method do |*args, &block|
|
data/lib/chewy/strategy.rb
CHANGED
@@ -7,18 +7,21 @@ begin
|
|
7
7
|
require 'resque'
|
8
8
|
require 'chewy/strategy/resque'
|
9
9
|
rescue LoadError
|
10
|
+
nil
|
10
11
|
end
|
11
12
|
|
12
13
|
begin
|
13
14
|
require 'sidekiq'
|
14
15
|
require 'chewy/strategy/sidekiq'
|
15
16
|
rescue LoadError
|
17
|
+
nil
|
16
18
|
end
|
17
19
|
|
18
20
|
begin
|
19
21
|
require 'active_job'
|
20
22
|
require 'chewy/strategy/active_job'
|
21
23
|
rescue LoadError
|
24
|
+
nil
|
22
25
|
end
|
23
26
|
|
24
27
|
module Chewy
|
@@ -42,7 +45,7 @@ module Chewy
|
|
42
45
|
@stack.last
|
43
46
|
end
|
44
47
|
|
45
|
-
def push
|
48
|
+
def push(name)
|
46
49
|
result = @stack.push resolve(name).new
|
47
50
|
debug "[#{@stack.size}] <- #{current.name}"
|
48
51
|
result
|
@@ -55,7 +58,7 @@ module Chewy
|
|
55
58
|
result
|
56
59
|
end
|
57
60
|
|
58
|
-
def wrap
|
61
|
+
def wrap(name)
|
59
62
|
stack = push(name)
|
60
63
|
yield
|
61
64
|
ensure
|
@@ -64,14 +67,13 @@ module Chewy
|
|
64
67
|
|
65
68
|
private
|
66
69
|
|
67
|
-
def debug
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
70
|
+
def debug(string)
|
71
|
+
return unless Chewy.logger && Chewy.logger.debug?
|
72
|
+
line = caller.detect { |l| l !~ %r{lib/chewy/strategy.rb:|lib/chewy.rb:} }
|
73
|
+
Chewy.logger.debug(["DEBUG: Chewy strategies stack: #{string}", line.sub(/:in\s.+$/, '')].join(' @ '))
|
72
74
|
end
|
73
75
|
|
74
|
-
def resolve
|
76
|
+
def resolve(name)
|
75
77
|
"Chewy::Strategy::#{name.to_s.camelize}".safe_constantize or raise "Can't find update strategy `#{name}`"
|
76
78
|
rescue NameError => ex
|
77
79
|
# WORKAROUND: Strange behavior of `safe_constantize` with mongoid gem
|
@@ -16,8 +16,8 @@ module Chewy
|
|
16
16
|
@stash = {}
|
17
17
|
end
|
18
18
|
|
19
|
-
def update
|
20
|
-
ActiveSupport::Deprecation.warn(
|
19
|
+
def update(type, objects, options = {})
|
20
|
+
ActiveSupport::Deprecation.warn('`urgent: true` option is deprecated and is not effective inside `:atomic` strategy, use `Chewy.strategy(:urgent)` strategy instead') if options.key?(:urgent)
|
21
21
|
|
22
22
|
@stash[type] ||= []
|
23
23
|
@stash[type] |= type.send(:build_root).id ? Array.wrap(objects) : type.adapter.identify(objects)
|
data/lib/chewy/strategy/base.rb
CHANGED
@@ -11,17 +11,17 @@ module Chewy
|
|
11
11
|
def name
|
12
12
|
self.class.name.demodulize.underscore.to_sym
|
13
13
|
end
|
14
|
+
|
14
15
|
# This method called when some model tries to update index
|
15
16
|
#
|
16
|
-
def update
|
17
|
-
raise UndefinedUpdateStrategy
|
17
|
+
def update(type, _objects, _options = {})
|
18
|
+
raise UndefinedUpdateStrategy, type
|
18
19
|
end
|
19
20
|
|
20
21
|
# This method called when strategy pops from the
|
21
22
|
# strategies stack
|
22
23
|
#
|
23
|
-
def leave
|
24
|
-
end
|
24
|
+
def leave; end
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
data/lib/chewy/type.rb
CHANGED
@@ -13,7 +13,7 @@ require 'chewy/type/adapter/sequel'
|
|
13
13
|
|
14
14
|
module Chewy
|
15
15
|
class Type
|
16
|
-
IMPORT_OPTIONS_KEYS = [:batch_size, :bulk_size, :refresh, :consistency, :replication]
|
16
|
+
IMPORT_OPTIONS_KEYS = [:batch_size, :bulk_size, :refresh, :consistency, :replication, :raw_import, :journal].freeze
|
17
17
|
|
18
18
|
include Search
|
19
19
|
include Mapping
|
@@ -24,66 +24,72 @@ module Chewy
|
|
24
24
|
include Witchcraft
|
25
25
|
include Import
|
26
26
|
|
27
|
-
singleton_class.delegate :index_name, :client, to: :index
|
27
|
+
singleton_class.delegate :index_name, :_index_name, :derivable_index_name, :client, to: :index
|
28
28
|
|
29
29
|
class_attribute :_default_import_options
|
30
30
|
self._default_import_options = {}
|
31
31
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
32
|
+
class << self
|
33
|
+
# Chewy index current type belongs to. Defined inside `Chewy.create_type`
|
34
|
+
#
|
35
|
+
def index
|
36
|
+
raise NotImplementedError
|
37
|
+
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
# Current type adapter. Defined inside `Chewy.create_type`, derived from
|
40
|
+
# `Chewy::Index.define_type` arguments.
|
41
|
+
#
|
42
|
+
def adapter
|
43
|
+
raise NotImplementedError
|
44
|
+
end
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
# Returns type name string
|
47
|
+
#
|
48
|
+
def type_name
|
49
|
+
adapter.type_name
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
52
|
+
# Returns list of public class methods defined in current type
|
53
|
+
#
|
54
|
+
def scopes
|
55
|
+
public_methods - Chewy::Type.public_methods
|
56
|
+
end
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
def default_import_options(params)
|
59
|
+
params.assert_valid_keys(IMPORT_OPTIONS_KEYS)
|
60
|
+
self._default_import_options = _default_import_options.merge(params)
|
61
|
+
end
|
61
62
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
def method_missing(method, *args, &block)
|
64
|
+
if index.scopes.include?(method)
|
65
|
+
define_singleton_method method do |*method_args, &method_block|
|
66
|
+
all.scoping { index.public_send(method, *method_args, &method_block) }
|
67
|
+
end
|
68
|
+
send(method, *args, &block)
|
69
|
+
else
|
70
|
+
super
|
66
71
|
end
|
67
|
-
send(method, *args, &block)
|
68
|
-
else
|
69
|
-
super
|
70
72
|
end
|
71
|
-
end
|
72
73
|
|
73
|
-
|
74
|
-
|
75
|
-
|
74
|
+
def respond_to_missing?(method, _)
|
75
|
+
index.scopes.include?(method) || super
|
76
|
+
end
|
77
|
+
|
78
|
+
def const_missing(name)
|
79
|
+
to_resolve = "#{self}::#{name}"
|
80
|
+
to_resolve[index.to_s] = ''
|
76
81
|
|
77
|
-
|
82
|
+
@__resolved_constants ||= {}
|
78
83
|
|
79
|
-
|
84
|
+
if to_resolve.empty? || @__resolved_constants[to_resolve]
|
85
|
+
super
|
86
|
+
else
|
87
|
+
@__resolved_constants[to_resolve] = true
|
88
|
+
to_resolve.constantize
|
89
|
+
end
|
90
|
+
rescue NotImplementedError
|
80
91
|
super
|
81
|
-
else
|
82
|
-
@__resolved_constants[to_resolve] = true
|
83
|
-
to_resolve.constantize
|
84
92
|
end
|
85
|
-
rescue NotImplementedError
|
86
|
-
super
|
87
93
|
end
|
88
94
|
end
|
89
95
|
end
|