3838import org .elasticsearch .cluster .metadata .IndexAbstraction ;
3939import org .elasticsearch .cluster .metadata .IndexMetadata ;
4040import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
41+ import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver .ResolvedExpression ;
4142import org .elasticsearch .cluster .node .DiscoveryNode ;
4243import org .elasticsearch .cluster .node .DiscoveryNodes ;
4344import org .elasticsearch .cluster .routing .GroupShardsIterator ;
111112import java .util .function .BiFunction ;
112113import java .util .function .Function ;
113114import java .util .function .LongSupplier ;
115+ import java .util .stream .Collectors ;
114116
115117import static org .elasticsearch .action .search .SearchType .DFS_QUERY_THEN_FETCH ;
116118import static org .elasticsearch .action .search .SearchType .QUERY_THEN_FETCH ;
@@ -207,14 +209,17 @@ public TransportSearchAction(
207209
208210 private Map <String , OriginalIndices > buildPerIndexOriginalIndices (
209211 ClusterState clusterState ,
210- Set <String > indicesAndAliases ,
212+ Set <ResolvedExpression > indicesAndAliases ,
211213 String [] indices ,
212214 IndicesOptions indicesOptions
213215 ) {
214216 Map <String , OriginalIndices > res = Maps .newMapWithExpectedSize (indices .length );
215217 var blocks = clusterState .blocks ();
216218 // optimization: mostly we do not have any blocks so there's no point in the expensive per-index checking
217219 boolean hasBlocks = blocks .global ().isEmpty () == false || blocks .indices ().isEmpty () == false ;
220+ // Get a distinct set of index abstraction names present from the resolved expressions to help with the reverse resolution from
221+ // concrete index to the expression that produced it.
222+ Set <String > indicesAndAliasesResources = indicesAndAliases .stream ().map (ResolvedExpression ::resource ).collect (Collectors .toSet ());
218223 for (String index : indices ) {
219224 if (hasBlocks ) {
220225 blocks .indexBlockedRaiseException (ClusterBlockLevel .READ , index );
@@ -231,8 +236,8 @@ private Map<String, OriginalIndices> buildPerIndexOriginalIndices(
231236 String [] finalIndices = Strings .EMPTY_ARRAY ;
232237 if (aliases == null
233238 || aliases .length == 0
234- || indicesAndAliases .contains (index )
235- || hasDataStreamRef (clusterState , indicesAndAliases , index )) {
239+ || indicesAndAliasesResources .contains (index )
240+ || hasDataStreamRef (clusterState , indicesAndAliasesResources , index )) {
236241 finalIndices = new String [] { index };
237242 }
238243 if (aliases != null ) {
@@ -251,7 +256,11 @@ private static boolean hasDataStreamRef(ClusterState clusterState, Set<String> i
251256 return indicesAndAliases .contains (ret .getParentDataStream ().getName ());
252257 }
253258
254- Map <String , AliasFilter > buildIndexAliasFilters (ClusterState clusterState , Set <String > indicesAndAliases , Index [] concreteIndices ) {
259+ Map <String , AliasFilter > buildIndexAliasFilters (
260+ ClusterState clusterState ,
261+ Set <ResolvedExpression > indicesAndAliases ,
262+ Index [] concreteIndices
263+ ) {
255264 final Map <String , AliasFilter > aliasFilterMap = new HashMap <>();
256265 for (Index index : concreteIndices ) {
257266 clusterState .blocks ().indexBlockedRaiseException (ClusterBlockLevel .READ , index .getName ());
@@ -1236,7 +1245,10 @@ private void executeSearch(
12361245 } else {
12371246 final Index [] indices = resolvedIndices .getConcreteLocalIndices ();
12381247 concreteLocalIndices = Arrays .stream (indices ).map (Index ::getName ).toArray (String []::new );
1239- final Set <String > indicesAndAliases = indexNameExpressionResolver .resolveExpressions (clusterState , searchRequest .indices ());
1248+ final Set <ResolvedExpression > indicesAndAliases = indexNameExpressionResolver .resolveExpressions (
1249+ clusterState ,
1250+ searchRequest .indices ()
1251+ );
12401252 aliasFilter = buildIndexAliasFilters (clusterState , indicesAndAliases , indices );
12411253 aliasFilter .putAll (remoteAliasMap );
12421254 localShardIterators = getLocalShardsIterator (
@@ -1835,7 +1847,7 @@ List<SearchShardIterator> getLocalShardsIterator(
18351847 ClusterState clusterState ,
18361848 SearchRequest searchRequest ,
18371849 String clusterAlias ,
1838- Set <String > indicesAndAliases ,
1850+ Set <ResolvedExpression > indicesAndAliases ,
18391851 String [] concreteIndices
18401852 ) {
18411853 var routingMap = indexNameExpressionResolver .resolveSearchRouting (clusterState , searchRequest .routing (), searchRequest .indices ());
0 commit comments