Skip to content

Commit fb8bfd9

Browse files
committed
[MODEL] Improved the support for returning an array of error elements from import
Changed the option name to `return`, to allow future expansion for other return values, and for improved expressivity: Article.import return: 'errors' Added documentation, tweaked tests. Related: 107
1 parent 9dadbc4 commit fb8bfd9

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

elasticsearch-model/lib/elasticsearch/model/importing.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,18 @@ module ClassMethods
9090
#
9191
# Article.import preprocess: enrich
9292
#
93+
# @example Return an array of error elements instead of the number of errors, eg.
94+
# to try importing these records again
95+
#
96+
# Article.import return: 'errors'
97+
#
9398
def import(options={}, &block)
94-
errors = []
99+
errors = []
95100
refresh = options.delete(:refresh) || false
96101
target_index = options.delete(:index) || index_name
97102
target_type = options.delete(:type) || document_type
98103
transform = options.delete(:transform) || __transform
99-
return_errors = options.delete(:return_errors) || false
104+
return_value = options.delete(:return) || 'count'
100105

101106
unless transform.respond_to?(:call)
102107
raise ArgumentError,
@@ -115,15 +120,16 @@ def import(options={}, &block)
115120

116121
yield response if block_given?
117122

118-
errors += response['items'].select { |i| i['index']['error'] }
123+
errors += response['items'].select { |k, v| k.values.first['error'] }
119124
end
120125

121126
self.refresh_index! if refresh
122127

123-
if return_errors
124-
errors
125-
else
126-
errors.size
128+
case return_value
129+
when 'errors'
130+
errors
131+
else
132+
errors.size
127133
end
128134
end
129135

elasticsearch-model/test/unit/importing_test.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def importing_mixin
4848
assert_equal 0, DummyImportingModel.import
4949
end
5050

51-
should "return number of errors" do
51+
should "return the number of errors" do
5252
Elasticsearch::Model::Adapter.expects(:from_class)
5353
.with(DummyImportingModel)
5454
.returns(DummyImportingAdapter)
@@ -66,24 +66,22 @@ def importing_mixin
6666
assert_equal 1, DummyImportingModel.import
6767
end
6868

69-
should "return list of errors if passing option 'return_errors'" do
69+
should "return an array of error elements" do
7070
Elasticsearch::Model::Adapter.expects(:from_class)
7171
.with(DummyImportingModel)
7272
.returns(DummyImportingAdapter)
7373

7474
DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
7575

7676
client = mock('client')
77-
error_text = 'FAILED'
78-
client.expects(:bulk).returns({'items' => [ {'index' => {}}, {'index' => {'error' => error_text}} ]})
77+
client.expects(:bulk).returns({'items' => [ {'index' => {}}, {'index' => {'error' => 'FAILED'}} ]})
7978

8079
DummyImportingModel.stubs(:client).returns(client)
8180
DummyImportingModel.stubs(:index_name).returns('foo')
8281
DummyImportingModel.stubs(:document_type).returns('foo')
82+
DummyImportingModel.stubs(:__batch_to_bulk)
8383

84-
expected_error_element = {'index' => {'error' => error_text}}
85-
86-
assert_equal [expected_error_element], DummyImportingModel.import(return_errors: true)
84+
assert_equal [{'index' => {'error' => 'FAILED'}}], DummyImportingModel.import(return: 'errors')
8785
end
8886

8987
should "yield the response" do

0 commit comments

Comments
 (0)