99
1010import org .elasticsearch .action .ActionListener ;
1111import org .elasticsearch .action .OriginalIndices ;
12- import org .elasticsearch .action .search .ShardSearchFailure ;
1312import org .elasticsearch .action .support .IndicesOptions ;
1413import org .elasticsearch .action .support .SubscribableListener ;
1514import org .elasticsearch .common .Strings ;
1918import org .elasticsearch .compute .data .Page ;
2019import org .elasticsearch .compute .operator .DriverProfile ;
2120import org .elasticsearch .core .Releasables ;
22- import org .elasticsearch .core .TimeValue ;
2321import org .elasticsearch .index .IndexMode ;
2422import org .elasticsearch .index .query .QueryBuilder ;
2523import org .elasticsearch .indices .IndicesExpressionGrouper ;
@@ -362,16 +360,12 @@ public void analyzedPlan(
362360 final List <IndexPattern > indices = preAnalysis .indices ;
363361
364362 EsqlCCSUtils .checkForCcsLicense (executionInfo , indices , indicesExpressionGrouper , configuredClusters , verifier .licenseState ());
363+ initializeClusterData (indices , executionInfo );
365364
366- final Set <String > targetClusters = enrichPolicyResolver .groupIndicesPerCluster (
367- configuredClusters ,
368- indices .stream ()
369- .flatMap (index -> Arrays .stream (Strings .commaDelimitedListToStringArray (index .indexPattern ())))
370- .toArray (String []::new )
371- ).keySet ();
365+ final Set <String > targetClusters = executionInfo .getClusters ().keySet ();
372366
373367 var listener = SubscribableListener .<EnrichResolution >newForked (
374- l -> enrichPolicyResolver .resolvePolicies (targetClusters , unresolvedPolicies , l )
368+ l -> enrichPolicyResolver .resolvePolicies (targetClusters , unresolvedPolicies , executionInfo , l )
375369 )
376370 .<PreAnalysisResult >andThen ((l , enrichResolution ) -> resolveFieldNames (parsed , enrichResolution , l ))
377371 .<PreAnalysisResult >andThen ((l , preAnalysisResult ) -> resolveInferences (preAnalysis .inferencePlans , preAnalysisResult , l ));
@@ -432,6 +426,26 @@ private void preAnalyzeLookupIndex(IndexPattern table, PreAnalysisResult result,
432426 // TODO: Verify that the resolved index actually has indexMode: "lookup"
433427 }
434428
429+ private void initializeClusterData (List <IndexPattern > indices , EsqlExecutionInfo executionInfo ) {
430+ if (indices .isEmpty ()) {
431+ return ;
432+ }
433+ assert indices .size () == 1 : "Only single index pattern is supported" ;
434+ Map <String , OriginalIndices > clusterIndices = indicesExpressionGrouper .groupIndices (
435+ configuredClusters ,
436+ IndicesOptions .DEFAULT ,
437+ indices .getFirst ().indexPattern ()
438+ );
439+ for (Map .Entry <String , OriginalIndices > entry : clusterIndices .entrySet ()) {
440+ final String clusterAlias = entry .getKey ();
441+ String indexExpr = Strings .arrayToCommaDelimitedString (entry .getValue ().indices ());
442+ executionInfo .swapCluster (clusterAlias , (k , v ) -> {
443+ assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet" ;
444+ return new EsqlExecutionInfo .Cluster (clusterAlias , indexExpr , executionInfo .isSkipUnavailable (clusterAlias ));
445+ });
446+ }
447+ }
448+
435449 private void preAnalyzeIndices (
436450 List <IndexPattern > indices ,
437451 EsqlExecutionInfo executionInfo ,
@@ -444,38 +458,8 @@ private void preAnalyzeIndices(
444458 // Note: JOINs are not supported but we detect them when
445459 listener .onFailure (new MappingException ("Queries with multiple indices are not supported" ));
446460 } else if (indices .size () == 1 ) {
447- // known to be unavailable from the enrich policy API call
448- Map <String , Exception > unavailableClusters = result .enrichResolution .getUnavailableClusters ();
449461 IndexPattern table = indices .getFirst ();
450462
451- Map <String , OriginalIndices > clusterIndices = indicesExpressionGrouper .groupIndices (
452- configuredClusters ,
453- IndicesOptions .DEFAULT ,
454- table .indexPattern ()
455- );
456- for (Map .Entry <String , OriginalIndices > entry : clusterIndices .entrySet ()) {
457- final String clusterAlias = entry .getKey ();
458- String indexExpr = Strings .arrayToCommaDelimitedString (entry .getValue ().indices ());
459- executionInfo .swapCluster (clusterAlias , (k , v ) -> {
460- assert v == null : "No cluster for " + clusterAlias + " should have been added to ExecutionInfo yet" ;
461- if (unavailableClusters .containsKey (k )) {
462- return new EsqlExecutionInfo .Cluster (
463- clusterAlias ,
464- indexExpr ,
465- executionInfo .isSkipUnavailable (clusterAlias ),
466- EsqlExecutionInfo .Cluster .Status .SKIPPED ,
467- 0 ,
468- 0 ,
469- 0 ,
470- 0 ,
471- List .of (new ShardSearchFailure (unavailableClusters .get (k ))),
472- new TimeValue (0 )
473- );
474- } else {
475- return new EsqlExecutionInfo .Cluster (clusterAlias , indexExpr , executionInfo .isSkipUnavailable (clusterAlias ));
476- }
477- });
478- }
479463 // if the preceding call to the enrich policy API found unavailable clusters, recreate the index expression to search
480464 // based only on available clusters (which could now be an empty list)
481465 String indexExpressionToResolve = EsqlCCSUtils .createIndexExpressionFromAvailableClusters (executionInfo );
0 commit comments