3737import org .elasticsearch .cluster .metadata .IndexAbstraction ;
3838import org .elasticsearch .cluster .metadata .IndexMetadata ;
3939import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver ;
40+ import org .elasticsearch .cluster .metadata .IndexNameExpressionResolver .ResolvedExpression ;
4041import org .elasticsearch .cluster .node .DiscoveryNode ;
4142import org .elasticsearch .cluster .node .DiscoveryNodes ;
4243import org .elasticsearch .cluster .routing .GroupShardsIterator ;
110111import java .util .function .BiFunction ;
111112import java .util .function .Function ;
112113import java .util .function .LongSupplier ;
114+ import java .util .stream .Collectors ;
113115
114116import static org .elasticsearch .action .search .SearchType .DFS_QUERY_THEN_FETCH ;
115117import static org .elasticsearch .action .search .SearchType .QUERY_THEN_FETCH ;
@@ -202,14 +204,17 @@ public TransportSearchAction(
202204
203205 private Map <String , OriginalIndices > buildPerIndexOriginalIndices (
204206 ClusterState clusterState ,
205- Set <String > indicesAndAliases ,
207+ Set <ResolvedExpression > indicesAndAliases ,
206208 String [] indices ,
207209 IndicesOptions indicesOptions
208210 ) {
209211 Map <String , OriginalIndices > res = Maps .newMapWithExpectedSize (indices .length );
210212 var blocks = clusterState .blocks ();
211213 // optimization: mostly we do not have any blocks so there's no point in the expensive per-index checking
212214 boolean hasBlocks = blocks .global ().isEmpty () == false || blocks .indices ().isEmpty () == false ;
215+ // Get a distinct set of index abstraction names present from the resolved expressions to help with the reverse resolution from
216+ // concrete index to the expression that produced it.
217+ Set <String > indicesAndAliasesResources = indicesAndAliases .stream ().map (ResolvedExpression ::resource ).collect (Collectors .toSet ());
213218 for (String index : indices ) {
214219 if (hasBlocks ) {
215220 blocks .indexBlockedRaiseException (ClusterBlockLevel .READ , index );
@@ -226,8 +231,8 @@ private Map<String, OriginalIndices> buildPerIndexOriginalIndices(
226231 String [] finalIndices = Strings .EMPTY_ARRAY ;
227232 if (aliases == null
228233 || aliases .length == 0
229- || indicesAndAliases .contains (index )
230- || hasDataStreamRef (clusterState , indicesAndAliases , index )) {
234+ || indicesAndAliasesResources .contains (index )
235+ || hasDataStreamRef (clusterState , indicesAndAliasesResources , index )) {
231236 finalIndices = new String [] { index };
232237 }
233238 if (aliases != null ) {
@@ -246,7 +251,11 @@ private static boolean hasDataStreamRef(ClusterState clusterState, Set<String> i
246251 return indicesAndAliases .contains (ret .getParentDataStream ().getName ());
247252 }
248253
249- Map <String , AliasFilter > buildIndexAliasFilters (ClusterState clusterState , Set <String > indicesAndAliases , Index [] concreteIndices ) {
254+ Map <String , AliasFilter > buildIndexAliasFilters (
255+ ClusterState clusterState ,
256+ Set <ResolvedExpression > indicesAndAliases ,
257+ Index [] concreteIndices
258+ ) {
250259 final Map <String , AliasFilter > aliasFilterMap = new HashMap <>();
251260 for (Index index : concreteIndices ) {
252261 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 (
@@ -1834,7 +1846,7 @@ List<SearchShardIterator> getLocalShardsIterator(
18341846 ClusterState clusterState ,
18351847 SearchRequest searchRequest ,
18361848 String clusterAlias ,
1837- Set <String > indicesAndAliases ,
1849+ Set <ResolvedExpression > indicesAndAliases ,
18381850 String [] concreteIndices
18391851 ) {
18401852 var routingMap = indexNameExpressionResolver .resolveSearchRouting (clusterState , searchRequest .routing (), searchRequest .indices ());
0 commit comments