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