chewy 0.8.4 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.rubocop.yml +56 -0
- data/.rubocop_todo.yml +44 -0
- data/.travis.yml +36 -67
- data/.yardopts +5 -0
- data/Appraisals +63 -58
- data/CHANGELOG.md +168 -11
- data/Gemfile +16 -9
- data/Guardfile +5 -5
- data/LEGACY_DSL.md +497 -0
- data/README.md +403 -470
- data/Rakefile +11 -1
- data/chewy.gemspec +12 -15
- data/gemfiles/rails.4.0.activerecord.gemfile +9 -9
- data/gemfiles/rails.4.1.activerecord.gemfile +9 -9
- data/gemfiles/rails.4.2.activerecord.gemfile +8 -8
- data/gemfiles/rails.4.2.mongoid.5.2.gemfile +16 -0
- data/gemfiles/rails.5.0.activerecord.gemfile +16 -0
- data/gemfiles/rails.5.0.mongoid.6.1.gemfile +16 -0
- data/gemfiles/rails.5.1.activerecord.gemfile +16 -0
- data/gemfiles/rails.5.1.mongoid.6.3.gemfile +16 -0
- data/gemfiles/rails.5.2.activerecord.gemfile +16 -0
- data/gemfiles/sequel.4.45.gemfile +11 -0
- data/lib/chewy/backports/deep_dup.rb +1 -1
- data/lib/chewy/backports/duplicable.rb +1 -0
- data/lib/chewy/config.rb +53 -21
- data/lib/chewy/errors.rb +6 -6
- data/lib/chewy/fields/base.rb +59 -29
- data/lib/chewy/fields/root.rb +49 -14
- data/lib/chewy/index/actions.rb +95 -36
- data/lib/chewy/index/aliases.rb +2 -1
- data/lib/chewy/index/settings.rb +10 -5
- data/lib/chewy/index/specification.rb +60 -0
- data/lib/chewy/index.rb +239 -138
- data/lib/chewy/journal.rb +55 -0
- data/lib/chewy/log_subscriber.rb +8 -8
- data/lib/chewy/minitest/helpers.rb +77 -0
- data/lib/chewy/minitest/search_index_receiver.rb +80 -0
- data/lib/chewy/minitest.rb +1 -0
- data/lib/chewy/query/compose.rb +18 -19
- data/lib/chewy/query/criteria.rb +34 -24
- data/lib/chewy/query/filters.rb +28 -11
- data/lib/chewy/query/loading.rb +3 -4
- data/lib/chewy/query/nodes/and.rb +1 -1
- 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 +4 -4
- data/lib/chewy/query/nodes/exists.rb +1 -1
- 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 +10 -12
- data/lib/chewy/query/nodes/missing.rb +1 -1
- data/lib/chewy/query/nodes/not.rb +1 -1
- data/lib/chewy/query/nodes/or.rb +1 -1
- data/lib/chewy/query/nodes/prefix.rb +3 -2
- data/lib/chewy/query/nodes/query.rb +1 -1
- data/lib/chewy/query/nodes/range.rb +9 -9
- data/lib/chewy/query/nodes/raw.rb +1 -1
- data/lib/chewy/query/nodes/regexp.rb +13 -9
- data/lib/chewy/query/nodes/script.rb +4 -4
- data/lib/chewy/query/pagination.rb +10 -1
- data/lib/chewy/query.rb +286 -170
- data/lib/chewy/railtie.rb +7 -6
- data/lib/chewy/rake_helper.rb +275 -37
- data/lib/chewy/repository.rb +2 -2
- data/lib/chewy/rspec/update_index.rb +70 -65
- data/lib/chewy/rspec.rb +1 -1
- data/lib/chewy/runtime/version.rb +4 -4
- data/lib/chewy/search/loader.rb +83 -0
- data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
- data/lib/chewy/search/pagination/will_paginate.rb +43 -0
- data/lib/chewy/search/parameters/aggs.rb +16 -0
- data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
- data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
- data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
- data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
- data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
- data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
- data/lib/chewy/search/parameters/explain.rb +16 -0
- data/lib/chewy/search/parameters/filter.rb +47 -0
- data/lib/chewy/search/parameters/highlight.rb +16 -0
- data/lib/chewy/search/parameters/indices.rb +123 -0
- data/lib/chewy/search/parameters/indices_boost.rb +52 -0
- data/lib/chewy/search/parameters/limit.rb +17 -0
- data/lib/chewy/search/parameters/load.rb +32 -0
- data/lib/chewy/search/parameters/min_score.rb +16 -0
- data/lib/chewy/search/parameters/none.rb +27 -0
- data/lib/chewy/search/parameters/offset.rb +17 -0
- data/lib/chewy/search/parameters/order.rb +64 -0
- data/lib/chewy/search/parameters/post_filter.rb +19 -0
- data/lib/chewy/search/parameters/preference.rb +16 -0
- data/lib/chewy/search/parameters/profile.rb +16 -0
- data/lib/chewy/search/parameters/query.rb +19 -0
- data/lib/chewy/search/parameters/request_cache.rb +27 -0
- data/lib/chewy/search/parameters/rescore.rb +29 -0
- data/lib/chewy/search/parameters/script_fields.rb +16 -0
- data/lib/chewy/search/parameters/search_after.rb +20 -0
- data/lib/chewy/search/parameters/search_type.rb +16 -0
- data/lib/chewy/search/parameters/source.rb +73 -0
- data/lib/chewy/search/parameters/storage.rb +95 -0
- data/lib/chewy/search/parameters/stored_fields.rb +63 -0
- data/lib/chewy/search/parameters/suggest.rb +16 -0
- data/lib/chewy/search/parameters/terminate_after.rb +16 -0
- data/lib/chewy/search/parameters/timeout.rb +16 -0
- data/lib/chewy/search/parameters/track_scores.rb +16 -0
- data/lib/chewy/search/parameters/types.rb +20 -0
- data/lib/chewy/search/parameters/version.rb +16 -0
- data/lib/chewy/search/parameters.rb +167 -0
- data/lib/chewy/search/query_proxy.rb +257 -0
- data/lib/chewy/search/request.rb +1045 -0
- data/lib/chewy/search/response.rb +119 -0
- data/lib/chewy/search/scoping.rb +50 -0
- data/lib/chewy/search/scrolling.rb +134 -0
- data/lib/chewy/search.rb +81 -26
- data/lib/chewy/stash.rb +79 -0
- data/lib/chewy/strategy/active_job.rb +1 -0
- data/lib/chewy/strategy/atomic.rb +2 -4
- data/lib/chewy/strategy/base.rb +4 -4
- data/lib/chewy/strategy/bypass.rb +1 -2
- data/lib/chewy/strategy/resque.rb +1 -0
- data/lib/chewy/strategy/shoryuken.rb +40 -0
- data/lib/chewy/strategy/sidekiq.rb +13 -1
- data/lib/chewy/strategy/urgent.rb +1 -1
- data/lib/chewy/strategy.rb +19 -10
- data/lib/chewy/type/actions.rb +26 -2
- data/lib/chewy/type/adapter/active_record.rb +50 -24
- data/lib/chewy/type/adapter/base.rb +29 -9
- data/lib/chewy/type/adapter/mongoid.rb +19 -10
- data/lib/chewy/type/adapter/object.rb +195 -31
- data/lib/chewy/type/adapter/orm.rb +69 -33
- data/lib/chewy/type/adapter/sequel.rb +37 -19
- data/lib/chewy/type/crutch.rb +5 -4
- data/lib/chewy/type/import/bulk_builder.rb +122 -0
- data/lib/chewy/type/import/bulk_request.rb +78 -0
- data/lib/chewy/type/import/journal_builder.rb +45 -0
- data/lib/chewy/type/import/routine.rb +138 -0
- data/lib/chewy/type/import.rb +150 -176
- data/lib/chewy/type/mapping.rb +58 -42
- data/lib/chewy/type/observe.rb +21 -15
- data/lib/chewy/type/syncer.rb +222 -0
- data/lib/chewy/type/witchcraft.rb +89 -34
- data/lib/chewy/type/wrapper.rb +48 -16
- data/lib/chewy/type.rb +77 -49
- data/lib/chewy/version.rb +1 -1
- data/lib/chewy.rb +95 -52
- data/lib/generators/chewy/install_generator.rb +3 -3
- data/lib/sequel/plugins/chewy_observe.rb +4 -19
- data/lib/tasks/chewy.rake +91 -28
- data/spec/chewy/config_spec.rb +130 -12
- data/spec/chewy/fields/base_spec.rb +194 -172
- data/spec/chewy/fields/root_spec.rb +123 -17
- data/spec/chewy/fields/time_fields_spec.rb +10 -9
- data/spec/chewy/index/actions_spec.rb +228 -43
- data/spec/chewy/index/aliases_spec.rb +2 -2
- data/spec/chewy/index/settings_spec.rb +100 -49
- data/spec/chewy/index/specification_spec.rb +169 -0
- data/spec/chewy/index_spec.rb +159 -63
- data/spec/chewy/journal_spec.rb +268 -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 +503 -236
- data/spec/chewy/query/filters_spec.rb +96 -68
- data/spec/chewy/query/loading_spec.rb +80 -42
- 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 +5 -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 +3 -55
- data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
- data/spec/chewy/query/pagination_spec.rb +25 -22
- data/spec/chewy/query_spec.rb +510 -505
- data/spec/chewy/rake_helper_spec.rb +381 -0
- data/spec/chewy/repository_spec.rb +8 -8
- data/spec/chewy/rspec/update_index_spec.rb +215 -113
- data/spec/chewy/runtime_spec.rb +2 -2
- data/spec/chewy/search/loader_spec.rb +117 -0
- data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
- data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
- data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
- data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
- data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
- data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
- data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/explain_spec.rb +5 -0
- data/spec/chewy/search/parameters/filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
- data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
- data/spec/chewy/search/parameters/indices_spec.rb +191 -0
- data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/limit_spec.rb +5 -0
- data/spec/chewy/search/parameters/load_spec.rb +60 -0
- data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
- data/spec/chewy/search/parameters/none_spec.rb +5 -0
- data/spec/chewy/search/parameters/offset_spec.rb +5 -0
- data/spec/chewy/search/parameters/order_spec.rb +65 -0
- data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/preference_spec.rb +5 -0
- data/spec/chewy/search/parameters/profile_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
- data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
- data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
- data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
- data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
- data/spec/chewy/search/parameters/source_spec.rb +156 -0
- data/spec/chewy/search/parameters/storage_spec.rb +60 -0
- data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
- data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
- data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
- data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
- data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
- data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
- data/spec/chewy/search/parameters/types_spec.rb +5 -0
- data/spec/chewy/search/parameters/version_spec.rb +5 -0
- data/spec/chewy/search/parameters_spec.rb +145 -0
- data/spec/chewy/search/query_proxy_spec.rb +68 -0
- data/spec/chewy/search/request_spec.rb +685 -0
- data/spec/chewy/search/response_spec.rb +192 -0
- data/spec/chewy/search/scrolling_spec.rb +169 -0
- data/spec/chewy/search_spec.rb +37 -20
- data/spec/chewy/stash_spec.rb +95 -0
- data/spec/chewy/strategy/active_job_spec.rb +8 -2
- data/spec/chewy/strategy/atomic_spec.rb +4 -1
- data/spec/chewy/strategy/resque_spec.rb +8 -2
- data/spec/chewy/strategy/shoryuken_spec.rb +66 -0
- data/spec/chewy/strategy/sidekiq_spec.rb +10 -2
- data/spec/chewy/strategy_spec.rb +6 -6
- data/spec/chewy/type/actions_spec.rb +29 -10
- data/spec/chewy/type/adapter/active_record_spec.rb +357 -139
- data/spec/chewy/type/adapter/mongoid_spec.rb +220 -101
- data/spec/chewy/type/adapter/object_spec.rb +129 -40
- data/spec/chewy/type/adapter/sequel_spec.rb +304 -152
- data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
- data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
- data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
- data/spec/chewy/type/import/routine_spec.rb +110 -0
- data/spec/chewy/type/import_spec.rb +360 -244
- data/spec/chewy/type/mapping_spec.rb +96 -29
- data/spec/chewy/type/observe_spec.rb +25 -15
- data/spec/chewy/type/syncer_spec.rb +123 -0
- data/spec/chewy/type/witchcraft_spec.rb +122 -44
- data/spec/chewy/type/wrapper_spec.rb +63 -23
- data/spec/chewy/type_spec.rb +32 -10
- data/spec/chewy_spec.rb +82 -12
- data/spec/spec_helper.rb +16 -2
- data/spec/support/active_record.rb +6 -2
- data/spec/support/class_helpers.rb +4 -19
- data/spec/support/mongoid.rb +17 -5
- data/spec/support/sequel.rb +6 -1
- metadata +250 -57
- data/gemfiles/rails.3.2.activerecord.gemfile +0 -15
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -14
- 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.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.will_paginate.gemfile +0 -14
- 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.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.4.2.activerecord.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.2.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/gemfiles/sequel.4.31.gemfile +0 -13
- data/lib/chewy/query/pagination/will_paginate.rb +0 -27
- data/lib/chewy/query/scoping.rb +0 -20
- data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -60
data/lib/chewy/railtie.rb
CHANGED
@@ -14,6 +14,8 @@ module Chewy
|
|
14
14
|
if Rails.application.config.respond_to?(:assets) && env['PATH_INFO'].start_with?(Rails.application.config.assets.prefix)
|
15
15
|
@app.call(env)
|
16
16
|
else
|
17
|
+
Chewy.logger.info("Chewy request strategy is `#{Chewy.request_strategy}`") if Chewy.logger && @request_strategy != Chewy.request_strategy
|
18
|
+
@request_strategy = Chewy.request_strategy
|
17
19
|
Chewy.strategy(Chewy.request_strategy) { @app.call(env) }
|
18
20
|
end
|
19
21
|
end
|
@@ -41,17 +43,16 @@ module Chewy
|
|
41
43
|
end
|
42
44
|
|
43
45
|
console do |app|
|
44
|
-
Chewy.logger = ActiveRecord::Base.logger if defined?(ActiveRecord)
|
45
|
-
|
46
46
|
if app.sandbox?
|
47
47
|
Chewy.strategy(:bypass)
|
48
48
|
else
|
49
49
|
Chewy.strategy(:urgent)
|
50
50
|
end
|
51
|
+
puts "Chewy console strategy is `#{Chewy.strategy.current.name}`"
|
51
52
|
end
|
52
53
|
|
53
54
|
initializer 'chewy.logger', after: 'active_record.logger' do
|
54
|
-
ActiveSupport.on_load(:active_record)
|
55
|
+
ActiveSupport.on_load(:active_record) { Chewy.logger ||= ActiveRecord::Base.logger }
|
55
56
|
end
|
56
57
|
|
57
58
|
initializer 'chewy.migration_strategy' do
|
@@ -67,12 +68,12 @@ module Chewy
|
|
67
68
|
end
|
68
69
|
|
69
70
|
initializer 'chewy.request_strategy' do |app|
|
70
|
-
app.config.middleware.
|
71
|
+
app.config.middleware.insert_before(ActionDispatch::ShowExceptions, RequestStrategy)
|
71
72
|
end
|
72
73
|
|
73
|
-
initializer 'chewy.
|
74
|
+
initializer 'chewy.add_indices_path' do |_app|
|
74
75
|
Chewy::Railtie.all_engines.each do |engine|
|
75
|
-
engine.paths.add
|
76
|
+
engine.paths.add Chewy.configuration[:indices_path]
|
76
77
|
end
|
77
78
|
end
|
78
79
|
end
|
data/lib/chewy/rake_helper.rb
CHANGED
@@ -1,64 +1,302 @@
|
|
1
1
|
module Chewy
|
2
2
|
module RakeHelper
|
3
|
+
IMPORT_CALLBACK = lambda do |output, _name, start, finish, _id, payload|
|
4
|
+
duration = (finish - start).ceil
|
5
|
+
stats = payload.fetch(:import, {}).map { |key, count| "#{key} #{count}" }.join(', ')
|
6
|
+
output.puts " Imported #{payload[:type]} in #{human_duration(duration)}, stats: #{stats}"
|
7
|
+
if payload[:errors]
|
8
|
+
payload[:errors].each do |action, errors|
|
9
|
+
output.puts " #{action.to_s.humanize} errors:"
|
10
|
+
errors.each do |error, documents|
|
11
|
+
output.puts " `#{error}`"
|
12
|
+
output.puts " on #{documents.count} documents: #{documents}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
JOURNAL_CALLBACK = lambda do |output, _, _, _, _, payload|
|
19
|
+
count = payload[:groups].values.map(&:size).sum
|
20
|
+
targets = payload[:groups].keys.sort_by(&:derivable_name)
|
21
|
+
output.puts " Applying journal to #{targets}, #{count} entries, stage #{payload[:stage]}"
|
22
|
+
end
|
23
|
+
|
3
24
|
class << self
|
25
|
+
# Performs zero-downtime reindexing of all documents for the specified indexes
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# Chewy::RakeHelper.reset # resets everything
|
29
|
+
# Chewy::RakeHelper.reset(only: 'cities') # resets only CitiesIndex
|
30
|
+
# Chewy::RakeHelper.reset(only: ['cities', CountriesIndex]) # resets CitiesIndex and CountriesIndex
|
31
|
+
# Chewy::RakeHelper.reset(except: CitiesIndex) # resets everything, but CitiesIndex
|
32
|
+
# Chewy::RakeHelper.reset(only: ['cities', 'countries'], except: CitiesIndex) # resets only CountriesIndex
|
33
|
+
#
|
34
|
+
# @param only [Array<Chewy::Index, String>, Chewy::Index, String] index or indexes to reset; if nothing is passed - uses all the indexes defined in the app
|
35
|
+
# @param except [Array<Chewy::Index, String>, Chewy::Index, String] index or indexes to exclude from processing
|
36
|
+
# @param parallel [true, Integer, Hash] any acceptable parallel options for import
|
37
|
+
# @param output [IO] output io for logging
|
38
|
+
# @return [Array<Chewy::Index>] indexes that were reset
|
39
|
+
def reset(only: nil, except: nil, parallel: nil, output: STDOUT)
|
40
|
+
subscribed_task_stats(output) do
|
41
|
+
indexes_from(only: only, except: except).each do |index|
|
42
|
+
reset_one(index, output, parallel: parallel)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
4
46
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
47
|
+
# Performs zero-downtime reindexing of all documents for the specified
|
48
|
+
# indexes only if a particular index specification was changed.
|
49
|
+
#
|
50
|
+
# @example
|
51
|
+
# Chewy::RakeHelper.upgrade # resets everything
|
52
|
+
# Chewy::RakeHelper.upgrade(only: 'cities') # resets only CitiesIndex
|
53
|
+
# Chewy::RakeHelper.upgrade(only: ['cities', CountriesIndex]) # resets CitiesIndex and CountriesIndex
|
54
|
+
# Chewy::RakeHelper.upgrade(except: CitiesIndex) # resets everything, but CitiesIndex
|
55
|
+
# Chewy::RakeHelper.upgrade(only: ['cities', 'countries'], except: CitiesIndex) # resets only CountriesIndex
|
56
|
+
#
|
57
|
+
# @param only [Array<Chewy::Index, String>, Chewy::Index, String] index or indexes to reset; if nothing is passed - uses all the indexes defined in the app
|
58
|
+
# @param except [Array<Chewy::Index, String>, Chewy::Index, String] index or indexes to exclude from processing
|
59
|
+
# @param parallel [true, Integer, Hash] any acceptable parallel options for import
|
60
|
+
# @param output [IO] output io for logging
|
61
|
+
# @return [Array<Chewy::Index>] indexes that were actually reset
|
62
|
+
def upgrade(only: nil, except: nil, parallel: nil, output: STDOUT)
|
63
|
+
subscribed_task_stats(output) do
|
64
|
+
indexes = indexes_from(only: only, except: except)
|
65
|
+
|
66
|
+
changed_indexes = indexes.select do |index|
|
67
|
+
index.specification.changed?
|
68
|
+
end
|
69
|
+
|
70
|
+
if changed_indexes.present?
|
71
|
+
indexes.each do |index|
|
72
|
+
if changed_indexes.include?(index)
|
73
|
+
reset_one(index, output, parallel: parallel)
|
74
|
+
else
|
75
|
+
output.puts "Skipping #{index}, the specification didn't change"
|
76
|
+
end
|
14
77
|
end
|
15
|
-
|
78
|
+
else
|
79
|
+
output.puts 'No index specification was changed'
|
80
|
+
end
|
81
|
+
|
82
|
+
changed_indexes
|
16
83
|
end
|
17
|
-
|
18
|
-
|
84
|
+
end
|
85
|
+
|
86
|
+
# Performs full update for each passed type if the corresponding index exists.
|
87
|
+
#
|
88
|
+
# @example
|
89
|
+
# Chewy::RakeHelper.update # updates everything
|
90
|
+
# Chewy::RakeHelper.update(only: 'places') # updates only PlacesIndex::City and PlacesIndex::Country
|
91
|
+
# Chewy::RakeHelper.update(only: 'places#city') # updates PlacesIndex::City only
|
92
|
+
# Chewy::RakeHelper.update(except: PlacesIndex::Country) # updates everything, but PlacesIndex::Country
|
93
|
+
# Chewy::RakeHelper.update(only: 'places', except: 'places#country') # updates PlacesIndex::City only
|
94
|
+
#
|
95
|
+
# @param only [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to update; if nothing is passed - uses all the types defined in the app
|
96
|
+
# @param except [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to exclude from processing
|
97
|
+
# @param parallel [true, Integer, Hash] any acceptable parallel options for import
|
98
|
+
# @param output [IO] output io for logging
|
99
|
+
# @return [Array<Chewy::Type>] types that were actually updated
|
100
|
+
def update(only: nil, except: nil, parallel: nil, output: STDOUT)
|
101
|
+
subscribed_task_stats(output) do
|
102
|
+
types_from(only: only, except: except).group_by(&:index).each_with_object([]) do |(index, types), update_types|
|
103
|
+
if index.exists?
|
104
|
+
output.puts "Updating #{index}"
|
105
|
+
types.each { |type| type.import(parallel: parallel) }
|
106
|
+
update_types.concat(types)
|
107
|
+
else
|
108
|
+
output.puts "Skipping #{index}, it does not exists (use rake chewy:reset[#{index.derivable_name}] to create and update it)"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
# Performs synchronization for each passed index if it exists.
|
115
|
+
#
|
116
|
+
# @example
|
117
|
+
# Chewy::RakeHelper.sync # synchronizes everything
|
118
|
+
# Chewy::RakeHelper.sync(only: 'places') # synchronizes only PlacesIndex::City and PlacesIndex::Country
|
119
|
+
# Chewy::RakeHelper.sync(only: 'places#city') # synchronizes PlacesIndex::City only
|
120
|
+
# Chewy::RakeHelper.sync(except: PlacesIndex::Country) # synchronizes everything, but PlacesIndex::Country
|
121
|
+
# Chewy::RakeHelper.sync(only: 'places', except: 'places#country') # synchronizes PlacesIndex::City only
|
122
|
+
#
|
123
|
+
# @param only [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to synchronize; if nothing is passed - uses all the types defined in the app
|
124
|
+
# @param except [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to exclude from processing
|
125
|
+
# @param parallel [true, Integer, Hash] any acceptable parallel options for sync
|
126
|
+
# @param output [IO] output io for logging
|
127
|
+
# @return [Array<Chewy::Type>] types that were actually updated
|
128
|
+
def sync(only: nil, except: nil, parallel: nil, output: STDOUT)
|
129
|
+
subscribed_task_stats(output) do
|
130
|
+
types_from(only: only, except: except).each_with_object([]) do |type, synced_types|
|
131
|
+
output.puts "Synchronizing #{type}"
|
132
|
+
output.puts " #{type} doesn't support outdated synchronization" unless type.supports_outdated_sync?
|
133
|
+
time = Time.now
|
134
|
+
sync_result = type.sync(parallel: parallel)
|
135
|
+
if !sync_result
|
136
|
+
output.puts " Something went wrong with the #{type} synchronization"
|
137
|
+
elsif sync_result[:count] > 0
|
138
|
+
output.puts " Missing documents: #{sync_result[:missing]}" if sync_result[:missing].present?
|
139
|
+
output.puts " Outdated documents: #{sync_result[:outdated]}" if sync_result[:outdated].present?
|
140
|
+
synced_types.push(type)
|
141
|
+
else
|
142
|
+
output.puts " Skipping #{type}, up to date"
|
143
|
+
end
|
144
|
+
output.puts " Took #{human_duration(Time.now - time)}"
|
145
|
+
end
|
19
146
|
end
|
20
147
|
end
|
21
148
|
|
22
|
-
|
23
|
-
|
149
|
+
# Applies changes that were done after the specified time for the
|
150
|
+
# specified indexes/types or all of them.
|
151
|
+
#
|
152
|
+
# @example
|
153
|
+
# Chewy::RakeHelper.journal_apply(time: 1.minute.ago) # applies entries created for the last minute
|
154
|
+
# Chewy::RakeHelper.journal_apply(time: 1.minute.ago, only: 'places') # applies only PlacesIndex::City and PlacesIndex::Country entries reated for the last minute
|
155
|
+
# Chewy::RakeHelper.journal_apply(time: 1.minute.ago, only: 'places#city') # applies PlacesIndex::City entries reated for the last minute only
|
156
|
+
# Chewy::RakeHelper.journal_apply(time: 1.minute.ago, except: PlacesIndex::Country) # applies everything, but PlacesIndex::Country entries reated for the last minute
|
157
|
+
# Chewy::RakeHelper.journal_apply(time: 1.minute.ago, only: 'places', except: 'places#country') # applies PlacesIndex::City entries reated for the last minute only
|
158
|
+
#
|
159
|
+
# @param time [Time, DateTime] use only journal entries created after this time
|
160
|
+
# @param only [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to synchronize; if nothing is passed - uses all the types defined in the app
|
161
|
+
# @param except [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to exclude from processing
|
162
|
+
# @param output [IO] output io for logging
|
163
|
+
# @return [Array<Chewy::Type>] types that were actually updated
|
164
|
+
def journal_apply(time: nil, only: nil, except: nil, output: STDOUT)
|
165
|
+
raise ArgumentError, 'Please specify the time to start with' unless time
|
166
|
+
subscribed_task_stats(output) do
|
167
|
+
output.puts "Applying journal entries created after #{time}"
|
168
|
+
count = Chewy::Journal.new(types_from(only: only, except: except)).apply(time)
|
169
|
+
output.puts 'No journal entries were created after the specified time' if count.zero?
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# Removes journal records created before the specified timestamp for
|
174
|
+
# the specified indexes/types or all of them.
|
175
|
+
#
|
176
|
+
# @example
|
177
|
+
# Chewy::RakeHelper.journal_clean # cleans everything
|
178
|
+
# Chewy::RakeHelper.journal_clean(time: 1.minute.ago) # leaves only entries created for the last minute
|
179
|
+
# Chewy::RakeHelper.journal_clean(only: 'places') # cleans only PlacesIndex::City and PlacesIndex::Country entries
|
180
|
+
# Chewy::RakeHelper.journal_clean(only: 'places#city') # cleans PlacesIndex::City entries only
|
181
|
+
# Chewy::RakeHelper.journal_clean(except: PlacesIndex::Country) # cleans everything, but PlacesIndex::Country entries
|
182
|
+
# Chewy::RakeHelper.journal_clean(only: 'places', except: 'places#country') # cleans PlacesIndex::City entries only
|
183
|
+
#
|
184
|
+
# @param time [Time, DateTime] clean all the journal entries created before this time
|
185
|
+
# @param only [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to synchronize; if nothing is passed - uses all the types defined in the app
|
186
|
+
# @param except [Array<Chewy::Index, Chewy::Type, String>, Chewy::Index, Chewy::Type, String] indexes or types to exclude from processing
|
187
|
+
# @param output [IO] output io for logging
|
188
|
+
# @return [Array<Chewy::Type>] types that were actually updated
|
189
|
+
def journal_clean(time: nil, only: nil, except: nil, output: STDOUT)
|
190
|
+
subscribed_task_stats(output) do
|
191
|
+
output.puts "Cleaning journal entries created before #{time}" if time
|
192
|
+
response = Chewy::Journal.new(types_from(only: only, except: except)).clean(time)
|
193
|
+
count = response['deleted'] || response['_indices']['_all']['deleted']
|
194
|
+
output.puts "Cleaned up #{count} journal entries"
|
195
|
+
end
|
196
|
+
end
|
24
197
|
|
25
|
-
|
26
|
-
|
27
|
-
|
198
|
+
# Eager loads and returns all the indexes defined in the application
|
199
|
+
# except Chewy::Stash::Specification and Chewy::Stash::Journal.
|
200
|
+
#
|
201
|
+
# @return [Array<Chewy::Index>] indexes found
|
202
|
+
def all_indexes
|
203
|
+
Chewy.eager_load!
|
204
|
+
Chewy::Index.descendants - [Chewy::Stash::Journal, Chewy::Stash::Specification]
|
205
|
+
end
|
206
|
+
|
207
|
+
def normalize_indexes(*identifiers)
|
208
|
+
identifiers.flatten(1).map { |identifier| normalize_index(identifier) }
|
209
|
+
end
|
210
|
+
|
211
|
+
def normalize_index(identifier)
|
212
|
+
return identifier if identifier.is_a?(Class) && identifier < Chewy::Index
|
213
|
+
"#{identifier.to_s.gsub(/identifier\z/i, '').camelize}Index".constantize
|
214
|
+
end
|
215
|
+
|
216
|
+
def subscribed_task_stats(output = STDOUT)
|
217
|
+
start = Time.now
|
218
|
+
ActiveSupport::Notifications.subscribed(JOURNAL_CALLBACK.curry[output], 'apply_journal.chewy') do
|
219
|
+
ActiveSupport::Notifications.subscribed(IMPORT_CALLBACK.curry[output], 'import_objects.chewy') do
|
220
|
+
yield
|
28
221
|
end
|
29
222
|
end
|
223
|
+
output.puts "Total: #{human_duration(Time.now - start)}"
|
224
|
+
end
|
225
|
+
|
226
|
+
def reset_index(*indexes)
|
227
|
+
ActiveSupport::Deprecation.warn '`Chewy::RakeHelper.reset_index` is deprecated and will be removed soon, use `Chewy::RakeHelper.reset` instead'
|
228
|
+
reset(only: indexes)
|
229
|
+
end
|
230
|
+
|
231
|
+
def reset_all(*except)
|
232
|
+
ActiveSupport::Deprecation.warn '`Chewy::RakeHelper.reset_all` is deprecated and will be removed soon, use `Chewy::RakeHelper.reset` instead'
|
233
|
+
reset(except: except)
|
30
234
|
end
|
31
235
|
|
32
|
-
def
|
33
|
-
|
236
|
+
def update_index(*indexes)
|
237
|
+
ActiveSupport::Deprecation.warn '`Chewy::RakeHelper.update_index` is deprecated and will be removed soon, use `Chewy::RakeHelper.update` instead'
|
238
|
+
update(only: indexes)
|
34
239
|
end
|
35
240
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
puts "Resetting #{index}"
|
40
|
-
index.reset! (Time.now.to_f * 1000).round
|
241
|
+
def update_all(*except)
|
242
|
+
ActiveSupport::Deprecation.warn '`Chewy::RakeHelper.update_all` is deprecated and will be removed soon, use `Chewy::RakeHelper.update` instead'
|
243
|
+
update(except: except)
|
41
244
|
end
|
42
245
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
246
|
+
private
|
247
|
+
|
248
|
+
def indexes_from(only: nil, except: nil)
|
249
|
+
indexes = if only.present?
|
250
|
+
normalize_indexes(Array.wrap(only))
|
251
|
+
else
|
252
|
+
all_indexes
|
253
|
+
end
|
254
|
+
|
255
|
+
indexes = if except.present?
|
256
|
+
indexes - normalize_indexes(Array.wrap(except))
|
257
|
+
else
|
258
|
+
indexes
|
259
|
+
end
|
260
|
+
|
261
|
+
indexes.sort_by(&:derivable_name)
|
47
262
|
end
|
48
263
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
52
|
-
if index.exists?
|
53
|
-
index.import
|
264
|
+
def types_from(only: nil, except: nil)
|
265
|
+
types = if only.present?
|
266
|
+
normalize_types(Array.wrap(only))
|
54
267
|
else
|
55
|
-
|
268
|
+
all_indexes.flat_map(&:types)
|
56
269
|
end
|
270
|
+
|
271
|
+
types = if except.present?
|
272
|
+
types - normalize_types(Array.wrap(except))
|
273
|
+
else
|
274
|
+
types
|
275
|
+
end
|
276
|
+
|
277
|
+
types.sort_by(&:derivable_name)
|
278
|
+
end
|
279
|
+
|
280
|
+
def normalize_types(*identifiers)
|
281
|
+
identifiers.flatten(1).flat_map { |identifier| normalize_type(identifier) }
|
282
|
+
end
|
283
|
+
|
284
|
+
def normalize_type(identifier)
|
285
|
+
Chewy.derive_types(identifier)
|
286
|
+
end
|
287
|
+
|
288
|
+
def human_duration(seconds)
|
289
|
+
[[60, :s], [60, :m], [24, :h]].map do |amount, unit|
|
290
|
+
if seconds > 0
|
291
|
+
seconds, n = seconds.divmod(amount)
|
292
|
+
"#{n.to_i}#{unit}"
|
293
|
+
end
|
294
|
+
end.compact.reverse.join(' ')
|
57
295
|
end
|
58
296
|
|
59
|
-
def
|
60
|
-
|
61
|
-
|
297
|
+
def reset_one(index, output, parallel: false)
|
298
|
+
output.puts "Resetting #{index}"
|
299
|
+
index.reset!((Time.now.to_f * 1000).round, parallel: parallel)
|
62
300
|
end
|
63
301
|
end
|
64
302
|
end
|
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
|
@@ -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,9 @@ 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
|
+
raise 'Use `only` in conjunction with `and_reindex` or `and_delete`' if @reindex.blank? && @delete.blank?
|
87
|
+
|
87
88
|
@only = true
|
88
89
|
end
|
89
90
|
|
@@ -91,64 +92,60 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
91
92
|
true
|
92
93
|
end
|
93
94
|
|
94
|
-
match do |block|
|
95
|
+
match do |block| # rubocop:disable BlockLength
|
95
96
|
@reindex ||= {}
|
96
97
|
@delete ||= {}
|
97
98
|
@missed_reindex = []
|
98
99
|
@missed_delete = []
|
99
|
-
@updated = []
|
100
100
|
|
101
101
|
type = Chewy.derive_type(type_name)
|
102
|
+
if defined?(Mocha) && RSpec.configuration.mock_framework.to_s == 'RSpec::Core::MockingAdapters::Mocha'
|
103
|
+
Chewy::Type::Import::BulkRequest.stubs(:new).with(type, any_parameters).returns(mock_bulk_request)
|
104
|
+
else
|
105
|
+
mocked_already = ::RSpec::Mocks.space.proxy_for(Chewy::Type::Import::BulkRequest).method_double_if_exists_for_message(:new)
|
106
|
+
allow(Chewy::Type::Import::BulkRequest).to receive(:new).and_call_original unless mocked_already
|
107
|
+
allow(Chewy::Type::Import::BulkRequest).to receive(:new).with(type, any_args).and_return(mock_bulk_request)
|
108
|
+
end
|
102
109
|
|
103
|
-
instance_eval <<-RUBY, __FILE__, __LINE__ + 1
|
104
|
-
#{agnostic_stub} do |bulk_options|
|
105
|
-
@updated += bulk_options[:body].map do |updated_document|
|
106
|
-
updated_document.deep_symbolize_keys
|
107
|
-
end
|
108
|
-
{}
|
109
|
-
end
|
110
|
-
RUBY
|
111
|
-
|
112
|
-
ActiveSupport::Deprecation.warn('`atomic: false` option is removed and not effective anymore, use `strategy: :atomic` option instead') if options.key?(:atomic)
|
113
110
|
Chewy.strategy(options[:strategy] || :atomic) { block.call }
|
114
111
|
|
115
|
-
|
116
|
-
if body = updated_document[:index]
|
117
|
-
if document = @reindex[body[:_id].to_s]
|
112
|
+
mock_bulk_request.updates.each do |updated_document|
|
113
|
+
if (body = updated_document[:index])
|
114
|
+
if (document = @reindex[body[:_id].to_s])
|
118
115
|
document[:real_count] += 1
|
119
116
|
document[:real_attributes].merge!(body[:data])
|
120
|
-
|
121
|
-
@missed_reindex.push(body[:_id].to_s)
|
117
|
+
elsif @only
|
118
|
+
@missed_reindex.push(body[:_id].to_s)
|
122
119
|
end
|
123
|
-
elsif body = updated_document[:delete]
|
124
|
-
if document = @delete[body[:_id].to_s]
|
120
|
+
elsif (body = updated_document[:delete])
|
121
|
+
if (document = @delete[body[:_id].to_s])
|
125
122
|
document[:real_count] += 1
|
126
|
-
|
127
|
-
@missed_delete.push(body[:_id].to_s)
|
123
|
+
elsif @only
|
124
|
+
@missed_delete.push(body[:_id].to_s)
|
128
125
|
end
|
129
126
|
end
|
130
127
|
end
|
131
128
|
|
132
|
-
@reindex.
|
129
|
+
@reindex.each_value do |document|
|
133
130
|
document[:match_count] = (!document[:expected_count] && document[:real_count] > 0) ||
|
134
131
|
(document[:expected_count] && document[:expected_count] == document[:real_count])
|
135
132
|
document[:match_attributes] = document[:expected_attributes].blank? ||
|
136
133
|
compare_attributes(document[:expected_attributes], document[:real_attributes])
|
137
134
|
end
|
138
|
-
@delete.
|
135
|
+
@delete.each_value do |document|
|
139
136
|
document[:match_count] = (!document[:expected_count] && document[:real_count] > 0) ||
|
140
137
|
(document[:expected_count] && document[:expected_count] == document[:real_count])
|
141
138
|
end
|
142
139
|
|
143
|
-
|
144
|
-
|
145
|
-
|
140
|
+
mock_bulk_request.updates.present? && @missed_reindex.none? && @missed_delete.none? &&
|
141
|
+
@reindex.all? { |_, document| document[:match_count] && document[:match_attributes] } &&
|
142
|
+
@delete.all? { |_, document| document[:match_count] }
|
146
143
|
end
|
147
144
|
|
148
|
-
failure_message do
|
145
|
+
failure_message do # rubocop:disable BlockLength
|
149
146
|
output = ''
|
150
147
|
|
151
|
-
if
|
148
|
+
if mock_bulk_request.updates.none?
|
152
149
|
output << "Expected index `#{type_name}` to be updated, but it was not\n"
|
153
150
|
elsif @missed_reindex.present? || @missed_delete.present?
|
154
151
|
message = "Expected index `#{type_name}` "
|
@@ -166,28 +163,28 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
166
163
|
output << message
|
167
164
|
end
|
168
165
|
|
169
|
-
output << @reindex.each.with_object('') do |(id, document),
|
166
|
+
output << @reindex.each.with_object('') do |(id, document), result|
|
170
167
|
unless document[:match_count] && document[:match_attributes]
|
171
|
-
|
168
|
+
result << "Expected document with id `#{id}` to be reindexed"
|
172
169
|
if document[:real_count] > 0
|
173
|
-
|
174
|
-
|
170
|
+
result << "\n #{document[:expected_count]} times, but was reindexed #{document[:real_count]} times" if document[:expected_count] && !document[:match_count]
|
171
|
+
result << "\n with #{document[:expected_attributes]}, but it was reindexed with #{document[:real_attributes]}" if document[:expected_attributes].present? && !document[:match_attributes]
|
175
172
|
else
|
176
|
-
|
173
|
+
result << ', but it was not'
|
177
174
|
end
|
178
|
-
|
175
|
+
result << "\n"
|
179
176
|
end
|
180
177
|
end
|
181
178
|
|
182
|
-
output << @delete.each.with_object('') do |(id, document),
|
179
|
+
output << @delete.each.with_object('') do |(id, document), result|
|
183
180
|
unless document[:match_count]
|
184
|
-
|
185
|
-
if document[:real_count] > 0 && document[:expected_count]
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
181
|
+
result << "Expected document with id `#{id}` to be deleted"
|
182
|
+
result << if document[:real_count] > 0 && document[:expected_count]
|
183
|
+
"\n #{document[:expected_count]} times, but was deleted #{document[:real_count]} times"
|
184
|
+
else
|
185
|
+
', but it was not'
|
186
|
+
end
|
187
|
+
result << "\n"
|
191
188
|
end
|
192
189
|
end
|
193
190
|
|
@@ -195,24 +192,18 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
195
192
|
end
|
196
193
|
|
197
194
|
failure_message_when_negated do
|
198
|
-
if
|
199
|
-
"Expected index `#{type_name}` not to be updated, but it was with #{
|
200
|
-
|
201
|
-
|
202
|
-
end.join
|
203
|
-
}\n"
|
195
|
+
if mock_bulk_request.updates.present?
|
196
|
+
"Expected index `#{type_name}` not to be updated, but it was with #{mock_bulk_request.updates.map(&:values).flatten.group_by { |documents| documents[:_id] }.map do |id, documents|
|
197
|
+
"\n document id `#{id}` (#{documents.count} times)"
|
198
|
+
end.join}\n"
|
204
199
|
end
|
205
200
|
end
|
206
201
|
|
207
|
-
def
|
208
|
-
|
209
|
-
"type.stubs(:bulk).with"
|
210
|
-
else
|
211
|
-
"allow(type).to receive(:bulk)"
|
212
|
-
end
|
202
|
+
def mock_bulk_request
|
203
|
+
@mock_bulk_request ||= MockBulkRequest.new
|
213
204
|
end
|
214
205
|
|
215
|
-
def extract_documents
|
206
|
+
def extract_documents(*args)
|
216
207
|
options = args.extract_options!
|
217
208
|
|
218
209
|
expected_count = options[:times] || options[:count]
|
@@ -230,7 +221,7 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
230
221
|
end]
|
231
222
|
end
|
232
223
|
|
233
|
-
def compare_attributes
|
224
|
+
def compare_attributes(expected, real)
|
234
225
|
expected.inject(true) do |result, (key, value)|
|
235
226
|
equal = if value.is_a?(Array) && real[key].is_a?(Array)
|
236
227
|
array_difference(value, real[key]) && array_difference(real[key], value)
|
@@ -243,13 +234,27 @@ RSpec::Matchers.define :update_index do |type_name, options = {}|
|
|
243
234
|
end
|
244
235
|
end
|
245
236
|
|
246
|
-
def array_difference
|
237
|
+
def array_difference(first, second)
|
247
238
|
difference = first.to_ary.dup
|
248
239
|
second.to_ary.each do |element|
|
249
|
-
|
250
|
-
|
251
|
-
end
|
240
|
+
index = difference.index(element)
|
241
|
+
difference.delete_at(index) if index
|
252
242
|
end
|
253
243
|
difference.none?
|
254
244
|
end
|
245
|
+
|
246
|
+
# Collects request bodies coming through the perform method for
|
247
|
+
# the further analysis.
|
248
|
+
class MockBulkRequest
|
249
|
+
attr_reader :updates
|
250
|
+
|
251
|
+
def initialize
|
252
|
+
@updates = []
|
253
|
+
end
|
254
|
+
|
255
|
+
def perform(body)
|
256
|
+
@updates.concat(body.map(&:deep_symbolize_keys))
|
257
|
+
[]
|
258
|
+
end
|
259
|
+
end
|
255
260
|
end
|
data/lib/chewy/rspec.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require 'chewy/rspec/update_index'
|
1
|
+
require 'chewy/rspec/update_index'
|
@@ -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
|