File tree Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Expand file tree Collapse file tree 2 files changed +28
-3
lines changed Original file line number Diff line number Diff line change @@ -91,11 +91,12 @@ module ClassMethods
91
91
# Article.import preprocess: enrich
92
92
#
93
93
def import ( options = { } , &block )
94
- errors = 0
94
+ errors = [ ]
95
95
refresh = options . delete ( :refresh ) || false
96
96
target_index = options . delete ( :index ) || index_name
97
97
target_type = options . delete ( :type ) || document_type
98
98
transform = options . delete ( :transform ) || __transform
99
+ return_errors = options . delete ( :return_errors ) || false
99
100
100
101
unless transform . respond_to? ( :call )
101
102
raise ArgumentError ,
@@ -114,12 +115,16 @@ def import(options={}, &block)
114
115
115
116
yield response if block_given?
116
117
117
- errors += response [ 'items' ] . map { |k , v | k . values . first [ ' error'] } . compact . length
118
+ errors += response [ 'items' ] . select { |i | i [ 'index' ] [ ' error'] }
118
119
end
119
120
120
121
self . refresh_index! if refresh
121
122
122
- return errors
123
+ if return_errors
124
+ errors
125
+ else
126
+ errors . size
127
+ end
123
128
end
124
129
125
130
def __batch_to_bulk ( batch , transform )
Original file line number Diff line number Diff line change @@ -66,6 +66,26 @@ def importing_mixin
66
66
assert_equal 1 , DummyImportingModel . import
67
67
end
68
68
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
+
69
89
should "yield the response" do
70
90
Elasticsearch ::Model ::Adapter . expects ( :from_class )
71
91
. with ( DummyImportingModel )
You can’t perform that action at this time.
0 commit comments