@@ -41,18 +41,19 @@ pub(crate) struct ElasticBulkResponse {
41
41
#[ serde( rename = "took" ) ]
42
42
pub took_millis : u64 ,
43
43
pub errors : bool ,
44
- pub items : Vec < ElasticBulkItemAction > ,
44
+ #[ serde( rename = "items" ) ]
45
+ pub actions : Vec < ElasticBulkAction > ,
45
46
}
46
47
47
- #[ derive( Debug , Serialize , Deserialize ) ]
48
- pub ( crate ) enum ElasticBulkItemAction {
48
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
49
+ pub ( crate ) enum ElasticBulkAction {
49
50
#[ serde( rename = "create" ) ]
50
51
Create ( ElasticBulkItem ) ,
51
52
#[ serde( rename = "index" ) ]
52
53
Index ( ElasticBulkItem ) ,
53
54
}
54
55
55
- #[ derive( Debug , Serialize , Deserialize ) ]
56
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
56
57
pub ( crate ) struct ElasticBulkItem {
57
58
#[ serde( rename = "_index" ) ]
58
59
pub index_id : IndexId ,
@@ -63,7 +64,7 @@ pub(crate) struct ElasticBulkItem {
63
64
pub error : Option < ElasticBulkError > ,
64
65
}
65
66
66
- #[ derive( Debug , Serialize , Deserialize ) ]
67
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
67
68
pub ( crate ) struct ElasticBulkError {
68
69
#[ serde( rename = "index" ) ]
69
70
pub index_id : Option < IndexId > ,
@@ -132,8 +133,32 @@ pub(crate) async fn elastic_bulk_ingest_v2(
132
133
} ;
133
134
let ingest_response_v2 = ingest_router. ingest ( ingest_request) . await ?;
134
135
let errors = !ingest_response_v2. failures . is_empty ( ) ;
135
- let mut items = Vec :: new ( ) ;
136
+ let mut actions : Vec < ElasticBulkAction > = Vec :: new ( ) ;
136
137
138
+ for success in ingest_response_v2. successes {
139
+ let es_doc_ids = per_subrequest_id_es_doc_ids
140
+ . remove ( & success. subrequest_id )
141
+ . ok_or_else ( || {
142
+ ElasticsearchError :: new (
143
+ StatusCode :: INTERNAL_SERVER_ERROR ,
144
+ format ! (
145
+ "could not find subrequest `{}` in bulk request" ,
146
+ success. subrequest_id
147
+ ) ,
148
+ None ,
149
+ )
150
+ } ) ?;
151
+ for es_doc_id in es_doc_ids {
152
+ let item = ElasticBulkItem {
153
+ index_id : success. index_uid ( ) . index_id . clone ( ) ,
154
+ es_doc_id,
155
+ status : StatusCode :: CREATED ,
156
+ error : None ,
157
+ } ;
158
+ let action = ElasticBulkAction :: Index ( item) ;
159
+ actions. push ( action) ;
160
+ }
161
+ }
137
162
for failure in ingest_response_v2. failures {
138
163
let es_doc_ids = per_subrequest_id_es_doc_ids
139
164
. remove ( & failure. subrequest_id )
@@ -161,7 +186,25 @@ pub(crate) async fn elastic_bulk_ingest_v2(
161
186
status : StatusCode :: NOT_FOUND ,
162
187
error : Some ( error) ,
163
188
} ;
164
- items. push ( ElasticBulkItemAction :: Index ( item) ) ;
189
+ let action = ElasticBulkAction :: Index ( item) ;
190
+ actions. push ( action) ;
191
+ }
192
+ }
193
+ IngestFailureReason :: Timeout => {
194
+ for es_doc_id in es_doc_ids {
195
+ let error = ElasticBulkError {
196
+ index_id : Some ( failure. index_id . clone ( ) ) ,
197
+ exception : ErrorCauseException :: Timeout ,
198
+ reason : format ! ( "timeout [{}]" , failure. index_id) ,
199
+ } ;
200
+ let item = ElasticBulkItem {
201
+ index_id : failure. index_id . clone ( ) ,
202
+ es_doc_id,
203
+ status : StatusCode :: REQUEST_TIMEOUT ,
204
+ error : Some ( error) ,
205
+ } ;
206
+ let action = ElasticBulkAction :: Index ( item) ;
207
+ actions. push ( action) ;
165
208
}
166
209
}
167
210
_ => {
@@ -174,7 +217,7 @@ pub(crate) async fn elastic_bulk_ingest_v2(
174
217
let bulk_response = ElasticBulkResponse {
175
218
took_millis,
176
219
errors,
177
- items ,
220
+ actions ,
178
221
} ;
179
222
Ok ( bulk_response)
180
223
}
@@ -274,6 +317,33 @@ mod tests {
274
317
275
318
let bulk_response: ElasticBulkResponse = serde_json:: from_slice ( response. body ( ) ) . unwrap ( ) ;
276
319
assert ! ( !bulk_response. errors) ;
320
+
321
+ let mut items = bulk_response
322
+ . actions
323
+ . into_iter ( )
324
+ . map ( |action| match action {
325
+ ElasticBulkAction :: Create ( item) => item,
326
+ ElasticBulkAction :: Index ( item) => item,
327
+ } )
328
+ . collect :: < Vec < _ > > ( ) ;
329
+ assert_eq ! ( items. len( ) , 3 ) ;
330
+
331
+ items. sort_by ( |left, right| {
332
+ left. index_id
333
+ . cmp ( & right. index_id )
334
+ . then ( left. es_doc_id . cmp ( & right. es_doc_id ) )
335
+ } ) ;
336
+ assert_eq ! ( items[ 0 ] . index_id, "my-index-1" ) ;
337
+ assert ! ( items[ 0 ] . es_doc_id. is_none( ) ) ;
338
+ assert_eq ! ( items[ 0 ] . status, StatusCode :: CREATED ) ;
339
+
340
+ assert_eq ! ( items[ 1 ] . index_id, "my-index-1" ) ;
341
+ assert_eq ! ( items[ 1 ] . es_doc_id. as_ref( ) . unwrap( ) , "1" ) ;
342
+ assert_eq ! ( items[ 1 ] . status, StatusCode :: CREATED ) ;
343
+
344
+ assert_eq ! ( items[ 2 ] . index_id, "my-index-2" ) ;
345
+ assert_eq ! ( items[ 2 ] . es_doc_id. as_ref( ) . unwrap( ) , "1" ) ;
346
+ assert_eq ! ( items[ 2 ] . status, StatusCode :: CREATED ) ;
277
347
}
278
348
279
349
#[ tokio:: test]
@@ -466,6 +536,6 @@ mod tests {
466
536
467
537
let bulk_response: ElasticBulkResponse = serde_json:: from_slice ( response. body ( ) ) . unwrap ( ) ;
468
538
assert ! ( bulk_response. errors) ;
469
- assert_eq ! ( bulk_response. items . len( ) , 3 ) ;
539
+ assert_eq ! ( bulk_response. actions . len( ) , 3 ) ;
470
540
}
471
541
}
0 commit comments