Skip to content

Commit 9dadbc4

Browse files
eturinokarmi
authored andcommitted
[MODEL] Added an option to the import method to return an array of error messages
Instead of returning just the number of errors, return all error messages from Elasticsearch, when using the `return_errors` option. Closes elastic#107
1 parent b500bf0 commit 9dadbc4

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,12 @@ module ClassMethods
9191
# Article.import preprocess: enrich
9292
#
9393
def import(options={}, &block)
94-
errors = 0
94+
errors = []
9595
refresh = options.delete(:refresh) || false
9696
target_index = options.delete(:index) || index_name
9797
target_type = options.delete(:type) || document_type
9898
transform = options.delete(:transform) || __transform
99+
return_errors = options.delete(:return_errors) || false
99100

100101
unless transform.respond_to?(:call)
101102
raise ArgumentError,
@@ -114,12 +115,16 @@ def import(options={}, &block)
114115

115116
yield response if block_given?
116117

117-
errors += response['items'].map { |k, v| k.values.first['error'] }.compact.length
118+
errors += response['items'].select { |i| i['index']['error'] }
118119
end
119120

120121
self.refresh_index! if refresh
121122

122-
return errors
123+
if return_errors
124+
errors
125+
else
126+
errors.size
127+
end
123128
end
124129

125130
def __batch_to_bulk(batch, transform)

elasticsearch-model/test/unit/importing_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ def importing_mixin
6666
assert_equal 1, DummyImportingModel.import
6767
end
6868

69+
should "return list of errors if passing option 'return_errors'" do
70+
Elasticsearch::Model::Adapter.expects(:from_class)
71+
.with(DummyImportingModel)
72+
.returns(DummyImportingAdapter)
73+
74+
DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
75+
76+
client = mock('client')
77+
error_text = 'FAILED'
78+
client.expects(:bulk).returns({'items' => [ {'index' => {}}, {'index' => {'error' => error_text}} ]})
79+
80+
DummyImportingModel.stubs(:client).returns(client)
81+
DummyImportingModel.stubs(:index_name).returns('foo')
82+
DummyImportingModel.stubs(:document_type).returns('foo')
83+
84+
expected_error_element = {'index' => {'error' => error_text}}
85+
86+
assert_equal [expected_error_element], DummyImportingModel.import(return_errors: true)
87+
end
88+
6989
should "yield the response" do
7090
Elasticsearch::Model::Adapter.expects(:from_class)
7191
.with(DummyImportingModel)

0 commit comments

Comments
 (0)