chewy 0.9.0 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +214 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +41 -19
- data/.rubocop_todo.yml +2 -2
- data/.yardopts +5 -0
- data/Appraisals +58 -28
- data/CHANGELOG.md +153 -12
- data/Gemfile +20 -12
- data/LEGACY_DSL.md +497 -0
- data/LICENSE.txt +1 -1
- data/README.md +338 -528
- data/chewy.gemspec +11 -12
- data/gemfiles/rails.5.2.activerecord.gemfile +17 -0
- data/gemfiles/rails.5.2.mongoid.6.4.gemfile +17 -0
- data/gemfiles/rails.6.0.activerecord.gemfile +17 -0
- data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
- data/gemfiles/ruby3.gemfile +10 -0
- data/gemfiles/sequel.4.45.gemfile +11 -0
- data/lib/chewy.rb +79 -44
- data/lib/chewy/backports/duplicable.rb +1 -1
- data/lib/chewy/config.rb +43 -17
- data/lib/chewy/errors.rb +2 -2
- data/lib/chewy/fields/base.rb +56 -31
- data/lib/chewy/fields/root.rb +44 -11
- data/lib/chewy/index.rb +237 -149
- data/lib/chewy/index/actions.rb +100 -35
- data/lib/chewy/index/aliases.rb +2 -1
- data/lib/chewy/index/settings.rb +11 -5
- data/lib/chewy/index/specification.rb +60 -0
- data/lib/chewy/journal.rb +40 -92
- data/lib/chewy/minitest/helpers.rb +6 -6
- data/lib/chewy/minitest/search_index_receiver.rb +17 -17
- data/lib/chewy/query.rb +182 -122
- data/lib/chewy/query/compose.rb +13 -13
- data/lib/chewy/query/criteria.rb +13 -13
- data/lib/chewy/query/filters.rb +21 -4
- data/lib/chewy/query/loading.rb +1 -2
- data/lib/chewy/query/nodes/and.rb +2 -2
- data/lib/chewy/query/nodes/bool.rb +1 -1
- data/lib/chewy/query/nodes/equal.rb +2 -2
- data/lib/chewy/query/nodes/exists.rb +1 -1
- data/lib/chewy/query/nodes/field.rb +1 -1
- data/lib/chewy/query/nodes/has_relation.rb +2 -2
- data/lib/chewy/query/nodes/match_all.rb +1 -1
- data/lib/chewy/query/nodes/missing.rb +1 -1
- data/lib/chewy/query/nodes/not.rb +2 -2
- data/lib/chewy/query/nodes/or.rb +2 -2
- data/lib/chewy/query/nodes/prefix.rb +1 -1
- data/lib/chewy/query/nodes/query.rb +2 -2
- data/lib/chewy/query/nodes/range.rb +4 -4
- data/lib/chewy/query/nodes/regexp.rb +4 -4
- data/lib/chewy/query/nodes/script.rb +3 -3
- data/lib/chewy/query/pagination.rb +10 -1
- data/lib/chewy/railtie.rb +4 -3
- data/lib/chewy/rake_helper.rb +265 -48
- data/lib/chewy/rspec/update_index.rb +33 -27
- data/lib/chewy/search.rb +79 -26
- 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.rb +168 -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/query_proxy.rb +257 -0
- data/lib/chewy/search/request.rb +1046 -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/stash.rb +79 -0
- data/lib/chewy/strategy.rb +10 -3
- data/lib/chewy/strategy/active_job.rb +2 -1
- data/lib/chewy/strategy/atomic.rb +2 -4
- data/lib/chewy/strategy/bypass.rb +1 -1
- data/lib/chewy/strategy/resque.rb +1 -0
- data/lib/chewy/strategy/shoryuken.rb +40 -0
- data/lib/chewy/strategy/sidekiq.rb +13 -3
- data/lib/chewy/type.rb +29 -7
- data/lib/chewy/type/actions.rb +26 -2
- data/lib/chewy/type/adapter/active_record.rb +44 -29
- data/lib/chewy/type/adapter/base.rb +27 -7
- data/lib/chewy/type/adapter/mongoid.rb +19 -10
- data/lib/chewy/type/adapter/object.rb +187 -26
- data/lib/chewy/type/adapter/orm.rb +59 -32
- data/lib/chewy/type/adapter/sequel.rb +33 -19
- data/lib/chewy/type/crutch.rb +1 -1
- data/lib/chewy/type/import.rb +146 -191
- 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/mapping.rb +51 -35
- data/lib/chewy/type/observe.rb +17 -13
- data/lib/chewy/type/syncer.rb +222 -0
- data/lib/chewy/type/witchcraft.rb +32 -16
- data/lib/chewy/type/wrapper.rb +30 -4
- data/lib/chewy/version.rb +1 -1
- data/lib/sequel/plugins/chewy_observe.rb +4 -19
- data/lib/tasks/chewy.rake +84 -26
- data/spec/chewy/config_spec.rb +98 -1
- data/spec/chewy/fields/base_spec.rb +170 -135
- data/spec/chewy/fields/root_spec.rb +124 -20
- data/spec/chewy/fields/time_fields_spec.rb +2 -3
- data/spec/chewy/index/actions_spec.rb +214 -52
- data/spec/chewy/index/aliases_spec.rb +2 -2
- data/spec/chewy/index/settings_spec.rb +67 -38
- data/spec/chewy/index/specification_spec.rb +169 -0
- data/spec/chewy/index_spec.rb +108 -64
- data/spec/chewy/journal_spec.rb +150 -55
- data/spec/chewy/minitest/helpers_spec.rb +4 -4
- data/spec/chewy/minitest/search_index_receiver_spec.rb +1 -1
- data/spec/chewy/query/criteria_spec.rb +179 -179
- data/spec/chewy/query/filters_spec.rb +16 -16
- data/spec/chewy/query/loading_spec.rb +22 -20
- data/spec/chewy/query/nodes/and_spec.rb +2 -2
- data/spec/chewy/query/nodes/bool_spec.rb +4 -4
- data/spec/chewy/query/nodes/equal_spec.rb +19 -19
- data/spec/chewy/query/nodes/exists_spec.rb +6 -6
- data/spec/chewy/query/nodes/has_child_spec.rb +19 -19
- data/spec/chewy/query/nodes/has_parent_spec.rb +19 -19
- data/spec/chewy/query/nodes/missing_spec.rb +5 -5
- data/spec/chewy/query/nodes/not_spec.rb +4 -2
- data/spec/chewy/query/nodes/or_spec.rb +2 -2
- data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
- data/spec/chewy/query/nodes/query_spec.rb +2 -2
- data/spec/chewy/query/nodes/range_spec.rb +18 -18
- data/spec/chewy/query/nodes/raw_spec.rb +1 -1
- data/spec/chewy/query/nodes/regexp_spec.rb +14 -14
- data/spec/chewy/query/nodes/script_spec.rb +4 -4
- 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 -21
- data/spec/chewy/query_spec.rb +503 -561
- data/spec/chewy/rake_helper_spec.rb +381 -0
- data/spec/chewy/repository_spec.rb +4 -4
- data/spec/chewy/rspec/update_index_spec.rb +89 -56
- 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 +147 -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 +198 -0
- data/spec/chewy/search/scrolling_spec.rb +169 -0
- data/spec/chewy/search_spec.rb +33 -16
- data/spec/chewy/stash_spec.rb +95 -0
- data/spec/chewy/strategy/active_job_spec.rb +21 -2
- data/spec/chewy/strategy/resque_spec.rb +6 -0
- data/spec/chewy/strategy/shoryuken_spec.rb +70 -0
- data/spec/chewy/strategy/sidekiq_spec.rb +13 -1
- 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 +203 -91
- data/spec/chewy/type/adapter/mongoid_spec.rb +112 -54
- data/spec/chewy/type/adapter/object_spec.rb +101 -28
- data/spec/chewy/type/adapter/sequel_spec.rb +149 -82
- 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 +356 -271
- data/spec/chewy/type/mapping_spec.rb +96 -29
- data/spec/chewy/type/observe_spec.rb +9 -5
- data/spec/chewy/type/syncer_spec.rb +123 -0
- data/spec/chewy/type/witchcraft_spec.rb +61 -29
- data/spec/chewy/type/wrapper_spec.rb +63 -23
- data/spec/chewy/type_spec.rb +28 -7
- data/spec/chewy_spec.rb +75 -7
- data/spec/spec_helper.rb +17 -3
- data/spec/support/active_record.rb +5 -1
- data/spec/support/class_helpers.rb +0 -14
- data/spec/support/mongoid.rb +15 -3
- data/spec/support/sequel.rb +6 -1
- metadata +219 -58
- data/.travis.yml +0 -36
- data/gemfiles/rails.3.2.activerecord.gemfile +0 -16
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -15
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.activerecord.gemfile +0 -17
- data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.4.0.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.4.0.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.5.1.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
- data/gemfiles/rails.5.0.activerecord.gemfile +0 -17
- data/gemfiles/rails.5.0.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.5.0.activerecord.will_paginate.gemfile +0 -16
- data/gemfiles/sequel.4.38.gemfile +0 -14
- data/lib/chewy/journal/apply.rb +0 -31
- data/lib/chewy/journal/clean.rb +0 -24
- data/lib/chewy/journal/entry.rb +0 -83
- data/lib/chewy/journal/query.rb +0 -87
- data/lib/chewy/query/pagination/will_paginate.rb +0 -27
- data/lib/chewy/query/scoping.rb +0 -20
- data/spec/chewy/journal/apply_spec.rb +0 -120
- data/spec/chewy/journal/entry_spec.rb +0 -237
- data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -59
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chewy::Type::Import::BulkRequest do
|
4
|
+
before { Chewy.massacre }
|
5
|
+
|
6
|
+
subject { described_class.new(type, suffix: suffix, bulk_size: bulk_size, **bulk_options) }
|
7
|
+
let(:suffix) {}
|
8
|
+
let(:bulk_size) {}
|
9
|
+
let(:bulk_options) { {} }
|
10
|
+
let(:type) { PlacesIndex::City }
|
11
|
+
|
12
|
+
describe '#initialize' do
|
13
|
+
specify { expect { described_class.new(nil, bulk_size: 100) }.to raise_error(ArgumentError) }
|
14
|
+
specify { expect { described_class.new(nil, bulk_size: 100.kilobytes) }.not_to raise_error }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#perform' do
|
18
|
+
before do
|
19
|
+
stub_model(:city)
|
20
|
+
stub_index(:places) do
|
21
|
+
define_type City do
|
22
|
+
field :name
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
specify do
|
28
|
+
expect(Chewy.client).not_to receive(:bulk)
|
29
|
+
subject.perform([])
|
30
|
+
end
|
31
|
+
|
32
|
+
specify do
|
33
|
+
expect(Chewy.client).to receive(:bulk).with(
|
34
|
+
index: 'places',
|
35
|
+
type: 'city',
|
36
|
+
body: [{index: {id: 42, data: {name: 'Name'}}}]
|
37
|
+
)
|
38
|
+
subject.perform([{index: {id: 42, data: {name: 'Name'}}}])
|
39
|
+
end
|
40
|
+
|
41
|
+
context ':suffix' do
|
42
|
+
let(:suffix) { 'suffix' }
|
43
|
+
|
44
|
+
specify do
|
45
|
+
expect(Chewy.client).to receive(:bulk).with(
|
46
|
+
index: 'places_suffix',
|
47
|
+
type: 'city',
|
48
|
+
body: [{index: {id: 42, data: {name: 'Name'}}}]
|
49
|
+
)
|
50
|
+
subject.perform([{index: {id: 42, data: {name: 'Name'}}}])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context ':bulk_size' do
|
55
|
+
let(:bulk_size) { 1.2.kilobyte }
|
56
|
+
|
57
|
+
specify do
|
58
|
+
expect(Chewy.client).to receive(:bulk).with(
|
59
|
+
index: 'places',
|
60
|
+
type: 'city',
|
61
|
+
body: "{\"index\":{\"id\":42}}\n{\"name\":\"#{'Name' * 10}\"}\n{\"index\":{\"id\":43}}\n{\"name\":\"#{'Shame' * 10}\"}\n"
|
62
|
+
)
|
63
|
+
subject.perform([
|
64
|
+
{index: {id: 42, data: {name: 'Name' * 10}}},
|
65
|
+
{index: {id: 43, data: {name: 'Shame' * 10}}}
|
66
|
+
])
|
67
|
+
end
|
68
|
+
|
69
|
+
specify do
|
70
|
+
expect(Chewy.client).to receive(:bulk).with(
|
71
|
+
index: 'places',
|
72
|
+
type: 'city',
|
73
|
+
body: "{\"index\":{\"id\":42}}\n{\"name\":\"#{'Name' * 30}\"}\n"
|
74
|
+
)
|
75
|
+
expect(Chewy.client).to receive(:bulk).with(
|
76
|
+
index: 'places',
|
77
|
+
type: 'city',
|
78
|
+
body: "{\"index\":{\"id\":43}}\n{\"name\":\"#{'Shame' * 100}\"}\n"
|
79
|
+
)
|
80
|
+
expect(Chewy.client).to receive(:bulk).with(
|
81
|
+
index: 'places',
|
82
|
+
type: 'city',
|
83
|
+
body: "{\"index\":{\"id\":44}}\n{\"name\":\"#{'Blame' * 30}\"}\n"
|
84
|
+
)
|
85
|
+
subject.perform([
|
86
|
+
{index: {id: 42, data: {name: 'Name' * 30}}},
|
87
|
+
{index: {id: 43, data: {name: 'Shame' * 100}}},
|
88
|
+
{index: {id: 44, data: {name: 'Blame' * 30}}}
|
89
|
+
])
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'bulk_options' do
|
94
|
+
let(:bulk_options) { {refresh: true} }
|
95
|
+
|
96
|
+
specify do
|
97
|
+
expect(Chewy.client).to receive(:bulk).with(hash_including(bulk_options))
|
98
|
+
subject.perform([{index: {id: 42, data: {name: 'Name'}}}])
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chewy::Type::Import::JournalBuilder, :orm do
|
4
|
+
before do
|
5
|
+
stub_model(:country)
|
6
|
+
stub_index 'namespace/places' do
|
7
|
+
define_type :city
|
8
|
+
define_type Country
|
9
|
+
end
|
10
|
+
Timecop.freeze(time)
|
11
|
+
end
|
12
|
+
after { Timecop.return }
|
13
|
+
|
14
|
+
let(:time) { Time.parse('2017-07-14 12:00Z') }
|
15
|
+
|
16
|
+
let(:type) { Namespace::PlacesIndex::City }
|
17
|
+
let(:index) { [] }
|
18
|
+
let(:delete) { [] }
|
19
|
+
subject { described_class.new(type, index: index, delete: delete) }
|
20
|
+
|
21
|
+
describe '#bulk_body' do
|
22
|
+
specify { expect(subject.bulk_body).to eq([]) }
|
23
|
+
|
24
|
+
context do
|
25
|
+
let(:index) { [{id: 1, name: 'City'}] }
|
26
|
+
specify do
|
27
|
+
expect(subject.bulk_body).to eq([{
|
28
|
+
index: {
|
29
|
+
_index: 'chewy_journal',
|
30
|
+
_type: 'journal',
|
31
|
+
data: {
|
32
|
+
'index_name' => 'namespace/places',
|
33
|
+
'type_name' => 'city',
|
34
|
+
'action' => 'index',
|
35
|
+
'references' => [Base64.encode64('{"id":1,"name":"City"}')],
|
36
|
+
'created_at' => time.as_json
|
37
|
+
}
|
38
|
+
}
|
39
|
+
}])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context do
|
44
|
+
let(:delete) { [{id: 1, name: 'City'}] }
|
45
|
+
specify do
|
46
|
+
expect(subject.bulk_body).to eq([{
|
47
|
+
index: {
|
48
|
+
_index: 'chewy_journal',
|
49
|
+
_type: 'journal',
|
50
|
+
data: {
|
51
|
+
'index_name' => 'namespace/places',
|
52
|
+
'type_name' => 'city',
|
53
|
+
'action' => 'delete',
|
54
|
+
'references' => [Base64.encode64('{"id":1,"name":"City"}')],
|
55
|
+
'created_at' => time.as_json
|
56
|
+
}
|
57
|
+
}
|
58
|
+
}])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context do
|
63
|
+
let(:type) { Namespace::PlacesIndex::Country }
|
64
|
+
let(:index) { [Country.new(id: 1, name: 'City')] }
|
65
|
+
let(:delete) { [Country.new(id: 2, name: 'City')] }
|
66
|
+
specify do
|
67
|
+
expect(subject.bulk_body).to eq([{
|
68
|
+
index: {
|
69
|
+
_index: 'chewy_journal',
|
70
|
+
_type: 'journal',
|
71
|
+
data: {
|
72
|
+
'index_name' => 'namespace/places',
|
73
|
+
'type_name' => 'country',
|
74
|
+
'action' => 'index',
|
75
|
+
'references' => [Base64.encode64('1')],
|
76
|
+
'created_at' => time.as_json
|
77
|
+
}
|
78
|
+
}
|
79
|
+
}, {
|
80
|
+
index: {
|
81
|
+
_index: 'chewy_journal',
|
82
|
+
_type: 'journal',
|
83
|
+
data: {
|
84
|
+
'index_name' => 'namespace/places',
|
85
|
+
'type_name' => 'country',
|
86
|
+
'action' => 'delete',
|
87
|
+
'references' => [Base64.encode64('2')],
|
88
|
+
'created_at' => time.as_json
|
89
|
+
}
|
90
|
+
}
|
91
|
+
}])
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# TODO: add more specs here later
|
4
|
+
describe Chewy::Type::Import::Routine do
|
5
|
+
before { Chewy.massacre }
|
6
|
+
before do
|
7
|
+
stub_index(:cities) do
|
8
|
+
define_type :city do
|
9
|
+
field :name
|
10
|
+
field :object, type: 'object'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
CitiesIndex.create!
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:index) { [double(id: 1, name: 'Name', object: {}), double(id: 2, name: 'Name', object: {})] }
|
17
|
+
let(:delete) { [double(id: 3, name: 'Name')] }
|
18
|
+
|
19
|
+
describe '#options' do
|
20
|
+
specify do
|
21
|
+
expect(described_class.new(CitiesIndex::City).options).to eq(
|
22
|
+
journal: nil,
|
23
|
+
refresh: true,
|
24
|
+
update_failover: true,
|
25
|
+
update_fields: [],
|
26
|
+
batch_size: 1000
|
27
|
+
)
|
28
|
+
end
|
29
|
+
|
30
|
+
specify do
|
31
|
+
expect(described_class.new(
|
32
|
+
CitiesIndex::City, batch_size: 100, bulk_size: 1.megabyte, refresh: false
|
33
|
+
).options).to eq(
|
34
|
+
journal: nil,
|
35
|
+
refresh: false,
|
36
|
+
update_failover: true,
|
37
|
+
update_fields: [],
|
38
|
+
bulk_size: 1_048_576,
|
39
|
+
batch_size: 100
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
context do
|
44
|
+
before { allow(Chewy).to receive_messages(configuration: Chewy.configuration.merge(journal: true)) }
|
45
|
+
specify do
|
46
|
+
expect(described_class.new(CitiesIndex::City).options).to eq(
|
47
|
+
journal: true,
|
48
|
+
refresh: true,
|
49
|
+
update_failover: true,
|
50
|
+
update_fields: [],
|
51
|
+
batch_size: 1000
|
52
|
+
)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
specify do
|
57
|
+
expect(CitiesIndex.client).to receive(:bulk).with(hash_including(refresh: true))
|
58
|
+
described_class.new(CitiesIndex::City).process(index: index)
|
59
|
+
end
|
60
|
+
|
61
|
+
specify do
|
62
|
+
expect(CitiesIndex.client).to receive(:bulk).with(hash_including(refresh: false))
|
63
|
+
described_class.new(CitiesIndex::City, refresh: false).process(index: index)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#parallel_options' do
|
68
|
+
specify { expect(described_class.new(CitiesIndex::City).parallel_options).to be_nil }
|
69
|
+
specify { expect(described_class.new(CitiesIndex::City, parallel: true).parallel_options).to eq({}) }
|
70
|
+
specify { expect(described_class.new(CitiesIndex::City, parallel: 3).parallel_options).to eq(in_processes: 3) }
|
71
|
+
specify { expect(described_class.new(CitiesIndex::City, parallel: {in_threads: 2}).parallel_options).to eq(in_threads: 2) }
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '#stats' do
|
75
|
+
subject { described_class.new(CitiesIndex::City) }
|
76
|
+
|
77
|
+
specify { expect(subject.stats).to eq({}) }
|
78
|
+
specify do
|
79
|
+
expect { subject.process(index: index) }
|
80
|
+
.to change { subject.stats }.to(index: 2)
|
81
|
+
end
|
82
|
+
specify do
|
83
|
+
expect { subject.process(index: index, delete: delete) }
|
84
|
+
.to change { subject.stats }.to(index: 2, delete: 1)
|
85
|
+
end
|
86
|
+
specify do
|
87
|
+
expect do
|
88
|
+
subject.process(index: index, delete: delete)
|
89
|
+
subject.process(index: index, delete: delete)
|
90
|
+
end.to change { subject.stats }.to(index: 4, delete: 2)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe '#errors' do
|
95
|
+
subject { described_class.new(CitiesIndex::City) }
|
96
|
+
let(:index) { [double(id: 1, name: 'Name', object: ''), double(id: 2, name: 'Name', object: {})] }
|
97
|
+
|
98
|
+
specify { expect(subject.errors).to eq([]) }
|
99
|
+
specify do
|
100
|
+
expect { subject.process(index: index) }
|
101
|
+
.to change { subject.errors }.to(have(1).item)
|
102
|
+
end
|
103
|
+
specify do
|
104
|
+
expect do
|
105
|
+
subject.process(index: index)
|
106
|
+
subject.process(index: index)
|
107
|
+
end.to change { subject.errors }.to(have(2).items)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -15,44 +15,89 @@ describe Chewy::Type::Import do
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
+
def imported_cities
|
19
|
+
CitiesIndex::City.all.map do |city|
|
20
|
+
city.attributes.except('_score', '_explanation')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def subscribe_notification
|
25
|
+
outer_payload = {}
|
26
|
+
ActiveSupport::Notifications.subscribe('import_objects.chewy') do |_name, _start, _finish, _id, payload|
|
27
|
+
outer_payload.merge!(payload)
|
28
|
+
end
|
29
|
+
outer_payload
|
30
|
+
end
|
31
|
+
|
18
32
|
let!(:dummy_cities) { Array.new(3) { |i| City.create(id: i + 1, name: "name#{i}") } }
|
19
|
-
let(:city) { CitiesIndex::City }
|
20
33
|
|
21
|
-
describe '
|
22
|
-
|
23
|
-
|
24
|
-
specify
|
25
|
-
|
34
|
+
describe 'index creation on import' do
|
35
|
+
let(:dummy_city) { City.create }
|
36
|
+
|
37
|
+
specify 'lazy (default)' do
|
38
|
+
expect(CitiesIndex).to receive(:exists?).and_call_original
|
39
|
+
expect(CitiesIndex).to receive(:create!).and_call_original
|
40
|
+
CitiesIndex::City.import(dummy_city)
|
41
|
+
end
|
42
|
+
|
43
|
+
specify 'lazy without objects' do
|
44
|
+
expect(CitiesIndex).not_to receive(:exists?)
|
45
|
+
expect(CitiesIndex).not_to receive(:create!)
|
46
|
+
CitiesIndex::City.import([])
|
47
|
+
end
|
26
48
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
49
|
+
context 'skip' do
|
50
|
+
before do
|
51
|
+
# To avoid flaky issues when previous specs were run
|
52
|
+
expect(Chewy::Index).to receive(:descendants).and_return([CitiesIndex])
|
53
|
+
Chewy.create_indices
|
54
|
+
Chewy.config.settings[:skip_index_creation_on_import] = true
|
55
|
+
end
|
56
|
+
after { Chewy.config.settings[:skip_index_creation_on_import] = nil }
|
57
|
+
|
58
|
+
specify do
|
59
|
+
expect(CitiesIndex).not_to receive(:exists?)
|
60
|
+
expect(CitiesIndex).not_to receive(:create!)
|
61
|
+
CitiesIndex::City.import(dummy_city)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
shared_examples 'importing' do
|
67
|
+
specify { expect(import).to eq(true) }
|
68
|
+
specify { expect(import([])).to eq(true) }
|
69
|
+
specify { expect(import(dummy_cities)).to eq(true) }
|
70
|
+
specify { expect(import(dummy_cities.map(&:id))).to eq(true) }
|
71
|
+
|
72
|
+
specify { expect { import([]) }.not_to update_index(CitiesIndex::City) }
|
73
|
+
specify { expect { import }.to update_index(CitiesIndex::City).and_reindex(dummy_cities) }
|
74
|
+
specify { expect { import dummy_cities }.to update_index(CitiesIndex::City).and_reindex(dummy_cities) }
|
75
|
+
specify { expect { import dummy_cities.map(&:id) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities) }
|
31
76
|
|
32
77
|
describe 'criteria-driven importing' do
|
33
|
-
let(:names) { %w
|
78
|
+
let(:names) { %w[name0 name1] }
|
34
79
|
|
35
80
|
context 'mongoid', :mongoid do
|
36
|
-
specify { expect {
|
37
|
-
specify { expect {
|
81
|
+
specify { expect { import(City.where(:name.in => names)) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
|
82
|
+
specify { expect { import(City.where(:name.in => names).map(&:id)) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
|
38
83
|
end
|
39
84
|
|
40
85
|
context 'active record', :active_record do
|
41
|
-
specify { expect {
|
42
|
-
specify { expect {
|
86
|
+
specify { expect { import(City.where(name: names)) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
|
87
|
+
specify { expect { import(City.where(name: names).map(&:id)) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
|
43
88
|
end
|
44
89
|
end
|
45
90
|
|
46
91
|
specify do
|
47
92
|
dummy_cities.first.destroy
|
48
|
-
expect {
|
49
|
-
.to update_index(
|
93
|
+
expect { import dummy_cities }
|
94
|
+
.to update_index(CitiesIndex::City).and_reindex(dummy_cities.from(1)).and_delete(dummy_cities.first)
|
50
95
|
end
|
51
96
|
|
52
97
|
specify do
|
53
98
|
dummy_cities.first.destroy
|
54
|
-
expect {
|
55
|
-
.to update_index(
|
99
|
+
expect { import dummy_cities.map(&:id) }
|
100
|
+
.to update_index(CitiesIndex::City).and_reindex(dummy_cities.from(1)).and_delete(dummy_cities.first)
|
56
101
|
end
|
57
102
|
|
58
103
|
specify do
|
@@ -64,80 +109,34 @@ describe Chewy::Type::Import do
|
|
64
109
|
nil
|
65
110
|
}
|
66
111
|
|
67
|
-
|
112
|
+
import dummy_cities.map(&:id), batch_size: 2
|
68
113
|
expect(imported.flatten).to match_array([
|
69
|
-
{
|
70
|
-
{
|
71
|
-
{
|
114
|
+
{index: {_id: 2, data: {'name' => 'name1'}}},
|
115
|
+
{index: {_id: 3, data: {'name' => 'name2'}}},
|
116
|
+
{delete: {_id: dummy_cities.first.id}}
|
72
117
|
])
|
73
118
|
end
|
74
119
|
|
75
120
|
context ':bulk_size' do
|
76
|
-
|
77
|
-
specify { expect(city.import(dummy_cities, bulk_size: 1.2.kilobyte)).to eq(true) }
|
78
|
-
specify { expect { city.import(dummy_cities, bulk_size: 1.2.kilobyte) }.to update_index(city).and_reindex(dummy_cities) }
|
79
|
-
|
80
|
-
specify do
|
81
|
-
dummy_cities.first.destroy
|
82
|
-
|
83
|
-
imported = []
|
84
|
-
allow(CitiesIndex.client).to receive(:bulk) { |params|
|
85
|
-
imported << params[:body]
|
86
|
-
nil
|
87
|
-
}
|
121
|
+
let!(:dummy_cities) { Array.new(3) { |i| City.create(id: i + 1, name: "name#{i}" * 20) } }
|
88
122
|
|
89
|
-
|
90
|
-
expect(imported.flatten).to match_array([
|
91
|
-
%({"delete":{"_id":1}}\n),
|
92
|
-
%({"index":{"_id":2}}\n{"name":"name1"}\n{"index":{"_id":3}}\n{"name":"name2"}\n)
|
93
|
-
])
|
94
|
-
end
|
123
|
+
specify { expect { import(dummy_cities, bulk_size: 1.2.kilobyte) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities) }
|
95
124
|
|
96
125
|
context do
|
97
|
-
|
98
|
-
|
99
|
-
specify do
|
100
|
-
dummy_cities.first.destroy
|
101
|
-
|
102
|
-
imported = []
|
103
|
-
allow(CitiesIndex.client).to receive(:bulk) { |params|
|
104
|
-
imported << params[:body]
|
105
|
-
nil
|
106
|
-
}
|
107
|
-
|
108
|
-
city.import dummy_cities.map(&:id), bulk_size: 1.2.kilobyte
|
109
|
-
expect(imported.flatten).to match_array([
|
110
|
-
%({"delete":{"_id":1}}\n),
|
111
|
-
%({"index":{"_id":2}}\n{"name":"#{'name1' * 20}"}\n),
|
112
|
-
%({"index":{"_id":3}}\n{"name":"#{'name2' * 20}"}\n)
|
113
|
-
])
|
114
|
-
end
|
115
|
-
|
116
|
-
specify do
|
117
|
-
expect { city.import dummy_cities.map(&:id), bulk_size: 1.1.kilobyte }.to raise_error ArgumentError
|
118
|
-
end
|
126
|
+
before { expect(Chewy.client).to receive(:bulk).exactly(3).times.and_call_original }
|
127
|
+
specify { expect(import(dummy_cities, bulk_size: 1.2.kilobyte)).to eq(true) }
|
119
128
|
end
|
120
129
|
end
|
121
130
|
|
122
|
-
specify do
|
123
|
-
expect(CitiesIndex.client).to receive(:bulk).with(hash_including(refresh: true))
|
124
|
-
city.import dummy_cities
|
125
|
-
end
|
126
|
-
|
127
|
-
specify do
|
128
|
-
expect(CitiesIndex.client).to receive(:bulk).with(hash_including(refresh: false))
|
129
|
-
city.import dummy_cities, refresh: false
|
130
|
-
end
|
131
|
-
|
132
131
|
context 'scoped' do
|
133
132
|
before do
|
134
|
-
names = %w
|
133
|
+
names = %w[name0 name1]
|
135
134
|
|
136
135
|
criteria = case adapter
|
137
136
|
when :mongoid
|
138
|
-
{
|
137
|
+
{:name.in => names}
|
139
138
|
else
|
140
|
-
{
|
139
|
+
{name: names}
|
141
140
|
end
|
142
141
|
|
143
142
|
stub_index(:cities) do
|
@@ -147,52 +146,40 @@ describe Chewy::Type::Import do
|
|
147
146
|
end
|
148
147
|
end
|
149
148
|
|
150
|
-
specify { expect {
|
149
|
+
specify { expect { import }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
|
151
150
|
|
152
151
|
context 'mongoid', :mongoid do
|
153
152
|
specify do
|
154
|
-
expect {
|
153
|
+
expect { import City.where(_id: dummy_cities.first.id) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first).only
|
155
154
|
end
|
156
155
|
end
|
157
156
|
|
158
157
|
context 'active record', :active_record do
|
159
158
|
specify do
|
160
|
-
expect {
|
159
|
+
expect { import City.where(id: dummy_cities.first.id) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first).only
|
161
160
|
end
|
162
161
|
end
|
163
162
|
end
|
164
163
|
|
165
164
|
context 'instrumentation payload' do
|
166
165
|
specify do
|
167
|
-
|
168
|
-
ActiveSupport::Notifications.subscribe('import_objects.chewy') do |_name, _start, _finish, _id, payload|
|
169
|
-
outer_payload = payload
|
170
|
-
end
|
171
|
-
|
166
|
+
payload = subscribe_notification
|
172
167
|
dummy_cities.first.destroy
|
173
|
-
|
174
|
-
expect(
|
168
|
+
import dummy_cities
|
169
|
+
expect(payload).to eq(type: CitiesIndex::City, import: {delete: 1, index: 2})
|
175
170
|
end
|
176
171
|
|
177
172
|
specify do
|
178
|
-
|
179
|
-
ActiveSupport::Notifications.subscribe('import_objects.chewy') do |_name, _start, _finish, _id, payload|
|
180
|
-
outer_payload = payload
|
181
|
-
end
|
182
|
-
|
173
|
+
payload = subscribe_notification
|
183
174
|
dummy_cities.first.destroy
|
184
|
-
|
185
|
-
expect(
|
175
|
+
import dummy_cities, batch_size: 2
|
176
|
+
expect(payload).to eq(type: CitiesIndex::City, import: {delete: 1, index: 2})
|
186
177
|
end
|
187
178
|
|
188
179
|
specify do
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
end
|
193
|
-
|
194
|
-
city.import dummy_cities, batch_size: 2
|
195
|
-
expect(outer_payload).to eq(type: CitiesIndex::City, import: { index: 3 })
|
180
|
+
payload = subscribe_notification
|
181
|
+
import dummy_cities, batch_size: 2
|
182
|
+
expect(payload).to eq(type: CitiesIndex::City, import: {index: 3})
|
196
183
|
end
|
197
184
|
|
198
185
|
context do
|
@@ -204,235 +191,296 @@ describe Chewy::Type::Import do
|
|
204
191
|
end
|
205
192
|
end
|
206
193
|
|
207
|
-
let(:write_failure_exception) do
|
208
|
-
'WriteFailureException; nested: MapperParsingException[object mapping for [city] ' \
|
209
|
-
'tried to parse field [name] as object, but got EOF, has a concrete value been provided to it?]; '
|
210
|
-
end
|
211
194
|
let(:mapper_parsing_exception) do
|
212
|
-
|
213
|
-
'
|
214
|
-
|
215
|
-
|
216
|
-
specify do
|
217
|
-
skip_on_version_gte('2.0', 'format of exception changed in 2.x')
|
218
|
-
outer_payload = nil
|
219
|
-
ActiveSupport::Notifications.subscribe('import_objects.chewy') do |_name, _start, _finish, _id, payload|
|
220
|
-
outer_payload = payload
|
221
|
-
end
|
222
|
-
|
223
|
-
city.import dummy_cities, batch_size: 2
|
224
|
-
expect(outer_payload).to eq(type: CitiesIndex::City,
|
225
|
-
errors: {
|
226
|
-
index: {
|
227
|
-
write_failure_exception => ['1'],
|
228
|
-
mapper_parsing_exception => %w(2 3)
|
229
|
-
}
|
230
|
-
},
|
231
|
-
import: { index: 3 })
|
195
|
+
{
|
196
|
+
'type' => 'mapper_parsing_exception',
|
197
|
+
'reason' => 'object mapping for [name] tried to parse field [name] as object, but found a concrete value'
|
198
|
+
}
|
232
199
|
end
|
233
200
|
|
234
201
|
specify do
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
city.import dummy_cities, batch_size: 2
|
242
|
-
expect(outer_payload).to eq(type: CitiesIndex::City,
|
243
|
-
errors: {
|
244
|
-
index: {
|
245
|
-
{
|
246
|
-
'type' => 'mapper_parsing_exception',
|
247
|
-
'reason' => 'object mapping for [name] tried to parse field [name] as object, but found a concrete value'
|
248
|
-
} => %w(1 2 3)
|
249
|
-
}
|
250
|
-
},
|
251
|
-
import: { index: 3 })
|
202
|
+
payload = subscribe_notification
|
203
|
+
import dummy_cities, batch_size: 2
|
204
|
+
expect(payload).to eq(type: CitiesIndex::City,
|
205
|
+
errors: {index: {mapper_parsing_exception => %w[1 2 3]}},
|
206
|
+
import: {index: 3})
|
252
207
|
end
|
253
208
|
end
|
254
209
|
end
|
255
210
|
|
256
|
-
context '
|
257
|
-
|
258
|
-
before do
|
259
|
-
stub_index(:cities) do
|
260
|
-
define_type City do
|
261
|
-
field :name, type: 'object'
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
211
|
+
context 'fields' do
|
212
|
+
before { CitiesIndex::City.import!(dummy_cities.first(2)) }
|
265
213
|
|
266
|
-
|
267
|
-
|
268
|
-
specify { expect(
|
214
|
+
context do
|
215
|
+
before { expect(Chewy.client).to receive(:bulk).twice.and_call_original }
|
216
|
+
specify { expect(import(dummy_cities, update_fields: [:name])).to eq(true) }
|
269
217
|
end
|
270
218
|
|
271
219
|
context do
|
272
|
-
before
|
273
|
-
|
274
|
-
|
275
|
-
field :name, type: 'object', value: -> { name == 'name1' ? name : { name: name } }
|
276
|
-
end
|
277
|
-
end
|
278
|
-
end
|
279
|
-
|
280
|
-
specify { expect(city.import(dummy_cities)).to eq(false) }
|
281
|
-
specify { expect(city.import(dummy_cities.map(&:id))).to eq(false) }
|
282
|
-
specify { expect(city.import(dummy_cities, batch_size: 2)).to eq(false) }
|
220
|
+
before { CitiesIndex::City.import!(dummy_cities.last) }
|
221
|
+
before { expect(Chewy.client).to receive(:bulk).once.and_call_original }
|
222
|
+
specify { expect(import(dummy_cities, update_fields: [:name])).to eq(true) }
|
283
223
|
end
|
284
224
|
end
|
285
225
|
|
286
|
-
context '
|
287
|
-
let(:country) { Country.create(id: 1, name: 'country') }
|
288
|
-
let(:another_country) { Country.create(id: 2, name: 'another country') }
|
289
|
-
|
226
|
+
context 'fields integrational' do
|
290
227
|
before do
|
291
|
-
|
292
|
-
|
293
|
-
adapter == :sequel ? City.many_to_one(:country) : City.belongs_to(:country)
|
294
|
-
end
|
295
|
-
|
296
|
-
before do
|
297
|
-
stub_index(:countries) do
|
298
|
-
define_type Country do
|
228
|
+
stub_index(:cities) do
|
229
|
+
define_type :city do
|
299
230
|
field :name
|
300
|
-
|
301
|
-
|
302
|
-
define_type City do
|
303
|
-
root parent: { type: 'country' }, parent_id: -> { country_id } do
|
304
|
-
field :name
|
305
|
-
end
|
231
|
+
field :object, type: 'object'
|
306
232
|
end
|
307
233
|
end
|
308
234
|
end
|
309
235
|
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
236
|
+
let(:objects) do
|
237
|
+
[
|
238
|
+
double('Name1', id: 1, name: 'Name11', object: {foo: 11}),
|
239
|
+
double('Name2', id: 2, name: 'Name12', object: 'foo'),
|
240
|
+
double('Name3', id: 3, name: 'Name13', object: {foo: 13}),
|
241
|
+
double('Name4', id: 4, name: 'Name14', object: 'foo'),
|
242
|
+
double('Name5', id: 5, name: 'Name15', object: {foo: 15}),
|
243
|
+
double('Name6', id: '', name: 'Name16', object: {foo: 16})
|
244
|
+
]
|
245
|
+
end
|
314
246
|
|
315
|
-
|
316
|
-
|
247
|
+
let(:old_objects) do
|
248
|
+
Array.new(6) do |i|
|
249
|
+
double("Name#{i + 1}", id: i + 1, name: "Name#{i + 1}", object: {foo: i + 1})
|
250
|
+
end
|
251
|
+
end
|
317
252
|
|
318
253
|
specify do
|
319
|
-
|
320
|
-
|
321
|
-
))
|
254
|
+
payload = subscribe_notification
|
255
|
+
|
256
|
+
expect(Chewy.client).to receive(:bulk).twice.and_call_original
|
257
|
+
import(objects, update_fields: %i[name])
|
258
|
+
|
259
|
+
expect(payload).to eq(
|
260
|
+
errors: {index: {{'type' => 'mapper_parsing_exception', 'reason' => 'object mapping for [object] tried to parse field [object] as object, but found a concrete value'} => %w[2 4]}},
|
261
|
+
import: {index: 6},
|
262
|
+
type: CitiesIndex::City
|
263
|
+
)
|
264
|
+
expect(imported_cities).to match_array([
|
265
|
+
{'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
|
266
|
+
{'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
|
267
|
+
{'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
|
268
|
+
])
|
269
|
+
end
|
322
270
|
|
323
|
-
|
271
|
+
specify do
|
272
|
+
payload = subscribe_notification
|
273
|
+
|
274
|
+
expect(Chewy.client).to receive(:bulk).at_least(4).at_most(6).times.and_call_original
|
275
|
+
import(objects, batch_size: 2, update_fields: %i[name])
|
276
|
+
|
277
|
+
expect(payload).to eq(
|
278
|
+
errors: {index: {{'type' => 'mapper_parsing_exception', 'reason' => 'object mapping for [object] tried to parse field [object] as object, but found a concrete value'} => %w[2 4]}},
|
279
|
+
import: {index: 6},
|
280
|
+
type: CitiesIndex::City
|
281
|
+
)
|
282
|
+
expect(imported_cities).to match_array([
|
283
|
+
{'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
|
284
|
+
{'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
|
285
|
+
{'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
|
286
|
+
])
|
324
287
|
end
|
325
288
|
|
326
|
-
context
|
327
|
-
before {
|
289
|
+
context do
|
290
|
+
before { CitiesIndex::City.import!(objects[4]) }
|
328
291
|
|
329
292
|
specify do
|
330
|
-
|
293
|
+
payload = subscribe_notification
|
294
|
+
|
295
|
+
expect(Chewy.client).to receive(:bulk).at_least(3).at_most(5).times.and_call_original
|
296
|
+
import(objects, batch_size: 2, update_fields: %i[name])
|
297
|
+
|
298
|
+
expect(payload).to eq(
|
299
|
+
errors: {index: {{'type' => 'mapper_parsing_exception', 'reason' => 'object mapping for [object] tried to parse field [object] as object, but found a concrete value'} => %w[2 4]}},
|
300
|
+
import: {index: 6},
|
301
|
+
type: CitiesIndex::City
|
302
|
+
)
|
303
|
+
expect(imported_cities).to match_array([
|
304
|
+
{'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
|
305
|
+
{'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
|
306
|
+
{'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
|
307
|
+
])
|
308
|
+
end
|
309
|
+
end
|
331
310
|
|
332
|
-
|
333
|
-
|
334
|
-
{ delete: { _id: child_city.id, parent: country.id.to_s } },
|
335
|
-
{ index: { _id: child_city.id, parent: another_country.id, data: { 'name' => 'city' } } }
|
336
|
-
]
|
337
|
-
))
|
311
|
+
context do
|
312
|
+
before { CitiesIndex::City.import!(old_objects[1], old_objects[3], objects[4]) }
|
338
313
|
|
339
|
-
|
314
|
+
specify do
|
315
|
+
payload = subscribe_notification
|
316
|
+
|
317
|
+
expect(Chewy.client).to receive(:bulk).twice.and_call_original
|
318
|
+
import(objects, update_fields: %i[name])
|
319
|
+
|
320
|
+
expect(payload).to eq(
|
321
|
+
import: {index: 6},
|
322
|
+
type: CitiesIndex::City
|
323
|
+
)
|
324
|
+
expect(imported_cities).to match_array([
|
325
|
+
{'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
|
326
|
+
{'id' => '2', 'name' => 'Name12', 'object' => {'foo' => 2}},
|
327
|
+
{'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
|
328
|
+
{'id' => '4', 'name' => 'Name14', 'object' => {'foo' => 4}},
|
329
|
+
{'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
|
330
|
+
])
|
340
331
|
end
|
341
332
|
|
342
333
|
specify do
|
343
|
-
|
344
|
-
|
345
|
-
expect(
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
334
|
+
payload = subscribe_notification
|
335
|
+
|
336
|
+
expect(Chewy.client).to receive(:bulk).at_least(3).at_most(5).times.and_call_original
|
337
|
+
import(objects, batch_size: 2, update_fields: %i[name])
|
338
|
+
|
339
|
+
expect(payload).to eq(
|
340
|
+
import: {index: 6},
|
341
|
+
type: CitiesIndex::City
|
342
|
+
)
|
343
|
+
expect(imported_cities).to match_array([
|
344
|
+
{'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
|
345
|
+
{'id' => '2', 'name' => 'Name12', 'object' => {'foo' => 2}},
|
346
|
+
{'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
|
347
|
+
{'id' => '4', 'name' => 'Name14', 'object' => {'foo' => 4}},
|
348
|
+
{'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
|
349
|
+
])
|
350
350
|
end
|
351
351
|
|
352
352
|
specify do
|
353
|
-
|
353
|
+
payload = subscribe_notification
|
354
354
|
|
355
|
-
expect(
|
356
|
-
|
357
|
-
))
|
355
|
+
expect(Chewy.client).to receive(:bulk).once.and_call_original
|
356
|
+
import(objects, update_fields: %i[name], update_failover: false)
|
358
357
|
|
359
|
-
|
358
|
+
# Full match doesn't work here.
|
359
|
+
expect(payload[:errors][:update].keys).to match([
|
360
|
+
hash_including('type' => 'document_missing_exception', 'reason' => '[city][1]: document missing'),
|
361
|
+
hash_including('type' => 'document_missing_exception', 'reason' => '[city][3]: document missing')
|
362
|
+
])
|
363
|
+
expect(payload[:errors][:update].values).to eq([['1'], ['3']])
|
364
|
+
expect(imported_cities).to match_array([
|
365
|
+
{'id' => '2', 'name' => 'Name12', 'object' => {'foo' => 2}},
|
366
|
+
{'id' => '4', 'name' => 'Name14', 'object' => {'foo' => 4}},
|
367
|
+
{'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
|
368
|
+
])
|
360
369
|
end
|
370
|
+
end
|
361
371
|
|
362
|
-
|
363
|
-
|
372
|
+
context do
|
373
|
+
before { CitiesIndex::City.import!(old_objects) }
|
364
374
|
|
365
|
-
|
366
|
-
|
375
|
+
specify do
|
376
|
+
payload = subscribe_notification
|
377
|
+
|
378
|
+
expect(Chewy.client).to receive(:bulk).once.and_call_original
|
379
|
+
import(objects, update_fields: %i[name])
|
380
|
+
|
381
|
+
expect(payload).to eq(
|
382
|
+
import: {index: 6},
|
383
|
+
type: CitiesIndex::City
|
384
|
+
)
|
385
|
+
expect(imported_cities).to match_array([
|
386
|
+
{'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 1}},
|
387
|
+
{'id' => '2', 'name' => 'Name12', 'object' => {'foo' => 2}},
|
388
|
+
{'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 3}},
|
389
|
+
{'id' => '4', 'name' => 'Name14', 'object' => {'foo' => 4}},
|
390
|
+
{'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 5}},
|
391
|
+
{'id' => '6', 'name' => 'Name6', 'object' => {'foo' => 6}}
|
392
|
+
])
|
367
393
|
end
|
368
394
|
end
|
369
|
-
end
|
370
395
|
|
371
|
-
|
372
|
-
|
373
|
-
let(:country) { CountriesIndex::Country }
|
396
|
+
context do
|
397
|
+
before { CitiesIndex::City.import!(old_objects) }
|
374
398
|
|
375
|
-
|
376
|
-
|
399
|
+
specify do
|
400
|
+
payload = subscribe_notification
|
401
|
+
|
402
|
+
expect(Chewy.client).to receive(:bulk).once.and_call_original
|
403
|
+
import(objects, update_fields: %i[object])
|
404
|
+
|
405
|
+
expect(payload).to eq(
|
406
|
+
errors: {update: {{'type' => 'mapper_parsing_exception', 'reason' => 'object mapping for [object] tried to parse field [object] as object, but found a concrete value'} => %w[2 4]}},
|
407
|
+
import: {index: 6},
|
408
|
+
type: CitiesIndex::City
|
409
|
+
)
|
410
|
+
expect(imported_cities).to match_array([
|
411
|
+
{'id' => '1', 'name' => 'Name1', 'object' => {'foo' => 11}},
|
412
|
+
{'id' => '2', 'name' => 'Name2', 'object' => {'foo' => 2}},
|
413
|
+
{'id' => '3', 'name' => 'Name3', 'object' => {'foo' => 13}},
|
414
|
+
{'id' => '4', 'name' => 'Name4', 'object' => {'foo' => 4}},
|
415
|
+
{'id' => '5', 'name' => 'Name5', 'object' => {'foo' => 15}},
|
416
|
+
{'id' => '6', 'name' => 'Name6', 'object' => {'foo' => 6}}
|
417
|
+
])
|
418
|
+
end
|
377
419
|
end
|
420
|
+
end
|
378
421
|
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
field :
|
422
|
+
context 'error handling' do
|
423
|
+
context do
|
424
|
+
before do
|
425
|
+
stub_index(:cities) do
|
426
|
+
define_type City do
|
427
|
+
field :name, type: 'object'
|
385
428
|
end
|
386
429
|
end
|
387
430
|
end
|
388
|
-
end
|
389
|
-
|
390
|
-
specify { expect(country.import(canada)).to eq(true) }
|
391
|
-
specify { expect { country.import(canada) }.to update_index(country).and_reindex(canada.country_code) }
|
392
|
-
|
393
|
-
specify do
|
394
|
-
expect(CountriesIndex.client).to receive(:bulk).with(hash_including(
|
395
|
-
body: [{ index: { _id: canada.country_code, data: { 'name' => 'Canada', 'rating' => 4 } } }]
|
396
|
-
))
|
397
|
-
|
398
|
-
country.import canada
|
399
|
-
end
|
400
|
-
|
401
|
-
specify do
|
402
|
-
canada.update_attributes(rating: 9)
|
403
431
|
|
404
|
-
expect(
|
405
|
-
|
406
|
-
))
|
407
|
-
|
408
|
-
country.import canada
|
432
|
+
specify { expect(import(dummy_cities)).to eq(false) }
|
433
|
+
specify { expect(import(dummy_cities.map(&:id))).to eq(false) }
|
434
|
+
specify { expect(import(dummy_cities, batch_size: 1)).to eq(false) }
|
409
435
|
end
|
410
436
|
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
437
|
+
context do
|
438
|
+
before do
|
439
|
+
stub_index(:cities) do
|
440
|
+
define_type City do
|
441
|
+
field :name, type: 'object', value: -> { name == 'name1' ? name : {name: name} }
|
442
|
+
end
|
443
|
+
end
|
444
|
+
end
|
417
445
|
|
418
|
-
|
446
|
+
specify { expect(import(dummy_cities)).to eq(false) }
|
447
|
+
specify { expect(import(dummy_cities.map(&:id))).to eq(false) }
|
448
|
+
specify { expect(import(dummy_cities, batch_size: 2)).to eq(false) }
|
419
449
|
end
|
420
450
|
end
|
421
451
|
|
422
|
-
context 'default_import_options
|
452
|
+
context 'default_import_options are set' do
|
423
453
|
before do
|
424
|
-
CitiesIndex::City.default_import_options(batch_size: 500
|
454
|
+
CitiesIndex::City.default_import_options(batch_size: 500)
|
425
455
|
end
|
426
456
|
|
427
457
|
specify do
|
428
|
-
expect(CitiesIndex::City.adapter).to receive(:import).with(batch_size: 500
|
458
|
+
expect(CitiesIndex::City.adapter).to receive(:import).with(any_args, hash_including(batch_size: 500))
|
429
459
|
CitiesIndex::City.import
|
430
460
|
end
|
431
461
|
end
|
432
462
|
end
|
433
463
|
|
464
|
+
describe '.import', :orm do
|
465
|
+
def import(*args)
|
466
|
+
CitiesIndex::City.import(*args)
|
467
|
+
end
|
468
|
+
|
469
|
+
it_behaves_like 'importing'
|
470
|
+
|
471
|
+
context 'parallel' do
|
472
|
+
def import(*args)
|
473
|
+
options = args.extract_options!
|
474
|
+
options[:parallel] = 0
|
475
|
+
CitiesIndex::City.import(*args, options)
|
476
|
+
end
|
477
|
+
|
478
|
+
it_behaves_like 'importing'
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
434
482
|
describe '.import!', :orm do
|
435
|
-
specify { expect {
|
483
|
+
specify { expect { CitiesIndex::City.import! }.not_to raise_error }
|
436
484
|
|
437
485
|
context do
|
438
486
|
before do
|
@@ -443,21 +491,58 @@ describe Chewy::Type::Import do
|
|
443
491
|
end
|
444
492
|
end
|
445
493
|
|
446
|
-
specify { expect {
|
494
|
+
specify { expect { CitiesIndex::City.import!(dummy_cities) }.to raise_error Chewy::ImportFailed }
|
447
495
|
end
|
496
|
+
end
|
448
497
|
|
449
|
-
|
450
|
-
|
451
|
-
|
498
|
+
describe '.compose' do
|
499
|
+
before do
|
500
|
+
stub_index(:cities) do
|
501
|
+
define_type :city do
|
502
|
+
crutch :names do |collection|
|
503
|
+
collection.map { |o| [o.name, o.name + '42'] }.to_h
|
504
|
+
end
|
505
|
+
field :name, value: ->(o, c) { c.names[o.name] }
|
506
|
+
field :rating
|
507
|
+
end
|
452
508
|
end
|
509
|
+
end
|
510
|
+
|
511
|
+
specify do
|
512
|
+
expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42)))
|
513
|
+
.to eq('name' => 'Name42', 'rating' => 42)
|
514
|
+
end
|
515
|
+
|
516
|
+
specify do
|
517
|
+
expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42), fields: %i[name]))
|
518
|
+
.to eq('name' => 'Name42')
|
519
|
+
end
|
520
|
+
|
521
|
+
context 'witchcraft' do
|
522
|
+
before { CitiesIndex::City.witchcraft! }
|
453
523
|
|
454
524
|
specify do
|
455
|
-
expect(
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
525
|
+
expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42)))
|
526
|
+
.to eq('name' => 'Name42', 'rating' => 42)
|
527
|
+
end
|
528
|
+
|
529
|
+
specify do
|
530
|
+
expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42), fields: %i[name]))
|
531
|
+
.to eq('name' => 'Name42')
|
532
|
+
end
|
533
|
+
end
|
534
|
+
|
535
|
+
context 'custom crutches' do
|
536
|
+
let(:crutches) { double(names: {'Name' => 'Name43'}) }
|
537
|
+
|
538
|
+
specify do
|
539
|
+
expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42), crutches))
|
540
|
+
.to eq('name' => 'Name43', 'rating' => 42)
|
541
|
+
end
|
542
|
+
|
543
|
+
specify do
|
544
|
+
expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42), crutches, fields: %i[name]))
|
545
|
+
.to eq('name' => 'Name43')
|
461
546
|
end
|
462
547
|
end
|
463
548
|
end
|