@@ -532,6 +532,8 @@ protected void doRun() {
532
532
}
533
533
final ConcreteIndices concreteIndices = new ConcreteIndices (clusterState , indexNameExpressionResolver );
534
534
Metadata metadata = clusterState .metadata ();
535
+ // go over all the requests and create a ShardId -> Operations mapping
536
+ Map <ShardId , List <BulkItemRequest >> requestsByShard = new HashMap <>();
535
537
for (int i = 0 ; i < bulkRequest .requests .size (); i ++) {
536
538
DocWriteRequest <?> docWriteRequest = bulkRequest .requests .get (i );
537
539
// the request can only be null because we set it to null in the previous step, so it gets ignored
@@ -587,6 +589,12 @@ protected void doRun() {
587
589
default :
588
590
throw new AssertionError ("request type not supported: [" + docWriteRequest .opType () + "]" );
589
591
}
592
+
593
+ ShardId shardId = clusterService .operationRouting ()
594
+ .indexShards (clusterState , concreteIndex .getName (), docWriteRequest .id (), docWriteRequest .routing ())
595
+ .shardId ();
596
+ List <BulkItemRequest > shardRequests = requestsByShard .computeIfAbsent (shardId , shard -> new ArrayList <>());
597
+ shardRequests .add (new BulkItemRequest (i , docWriteRequest ));
590
598
} catch (OpenSearchParseException | IllegalArgumentException | RoutingMissingException e ) {
591
599
BulkItemResponse .Failure failure = new BulkItemResponse .Failure (concreteIndex .getName (), docWriteRequest .id (), e );
592
600
BulkItemResponse bulkItemResponse = new BulkItemResponse (i , docWriteRequest .opType (), failure );
@@ -596,21 +604,6 @@ protected void doRun() {
596
604
}
597
605
}
598
606
599
- // first, go over all the requests and create a ShardId -> Operations mapping
600
- Map <ShardId , List <BulkItemRequest >> requestsByShard = new HashMap <>();
601
- for (int i = 0 ; i < bulkRequest .requests .size (); i ++) {
602
- DocWriteRequest <?> request = bulkRequest .requests .get (i );
603
- if (request == null ) {
604
- continue ;
605
- }
606
- String concreteIndex = concreteIndices .getConcreteIndex (request .index ()).getName ();
607
- ShardId shardId = clusterService .operationRouting ()
608
- .indexShards (clusterState , concreteIndex , request .id (), request .routing ())
609
- .shardId ();
610
- List <BulkItemRequest > shardRequests = requestsByShard .computeIfAbsent (shardId , shard -> new ArrayList <>());
611
- shardRequests .add (new BulkItemRequest (i , request ));
612
- }
613
-
614
607
if (requestsByShard .isEmpty ()) {
615
608
BulkItemResponse [] response = responses .toArray (new BulkItemResponse [responses .length ()]);
616
609
long tookMillis = buildTookInMillis (startTimeNanos );
0 commit comments