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