@@ -346,27 +346,18 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
346
346
}
347
347
}
348
348
349
- boolean excludedDataStreams = false ;
350
349
final Set <Index > concreteIndices = Sets .newLinkedHashSetWithExpectedSize (expressions .size ());
351
350
final SortedMap <String , IndexAbstraction > indicesLookup = context .state .metadata ().getIndicesLookup ();
352
351
for (String expression : expressions ) {
352
+ if (options .ignoreUnavailable () == false ) {
353
+ ensureAliasOrIndexExists (context , expression );
354
+ }
353
355
IndexAbstraction indexAbstraction = indicesLookup .get (expression );
354
356
if (indexAbstraction == null ) {
355
- if (options .ignoreUnavailable () == false ) {
356
- assert options .expandWildcardExpressions () == false ;
357
- throw notFoundException (expression );
358
- } else {
359
- continue ;
360
- }
357
+ continue ;
361
358
} else if (indexAbstraction .getType () == Type .ALIAS && context .getOptions ().ignoreAliases ()) {
362
- if (options .ignoreUnavailable () == false ) {
363
- assert options .expandWildcardExpressions () == false ;
364
- throw aliasesNotSupportedException (expression );
365
- } else {
366
- continue ;
367
- }
359
+ continue ;
368
360
} else if (indexAbstraction .isDataStreamRelated () && context .includeDataStreams () == false ) {
369
- excludedDataStreams = true ;
370
361
continue ;
371
362
}
372
363
@@ -415,33 +406,12 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
415
406
}
416
407
417
408
if (options .allowNoIndices () == false && concreteIndices .isEmpty ()) {
418
- IndexNotFoundException infe = notFoundException (indexExpressions );
419
- if (excludedDataStreams ) {
420
- // Allows callers to handle IndexNotFoundException differently based on whether data streams were excluded.
421
- infe .addMetadata (EXCLUDED_DATA_STREAMS_KEY , "true" );
422
- }
423
- throw infe ;
409
+ throw notFoundException (indexExpressions );
424
410
}
425
411
checkSystemIndexAccess (context , concreteIndices );
426
412
return concreteIndices .toArray (Index .EMPTY_ARRAY );
427
413
}
428
414
429
- private IndexNotFoundException notFoundException (String ... indexExpressions ) {
430
- IndexNotFoundException infe ;
431
- if (indexExpressions .length == 1 ) {
432
- if (indexExpressions [0 ].equals (Metadata .ALL )) {
433
- infe = new IndexNotFoundException ("no indices exist" , indexExpressions [0 ]);
434
- } else {
435
- infe = new IndexNotFoundException (indexExpressions [0 ]);
436
- }
437
- infe .setResources ("index_expression" , indexExpressions [0 ]);
438
- } else {
439
- infe = new IndexNotFoundException ((String ) null );
440
- infe .setResources ("index_expression" , indexExpressions );
441
- }
442
- return infe ;
443
- }
444
-
445
415
private void checkSystemIndexAccess (Context context , Set <Index > concreteIndices ) {
446
416
final Metadata metadata = context .getState ().metadata ();
447
417
final Predicate <String > systemIndexAccessPredicate = context .getSystemIndexAccessPredicate ().negate ();
@@ -488,6 +458,40 @@ private void checkSystemIndexAccess(Context context, Set<Index> concreteIndices)
488
458
}
489
459
}
490
460
461
+ private static IndexNotFoundException notFoundException (String ... indexExpressions ) {
462
+ IndexNotFoundException infe ;
463
+ if (indexExpressions != null && indexExpressions .length == 1 ) {
464
+ if (Metadata .ALL .equals (indexExpressions [0 ])) {
465
+ infe = new IndexNotFoundException ("no indices exist" , indexExpressions [0 ]);
466
+ } else {
467
+ infe = new IndexNotFoundException (indexExpressions [0 ]);
468
+ }
469
+ infe .setResources ("index_or_alias" , indexExpressions [0 ]);
470
+ } else {
471
+ infe = new IndexNotFoundException ((String ) null );
472
+ infe .setResources ("index_expression" , indexExpressions );
473
+ }
474
+ return infe ;
475
+ }
476
+
477
+ @ Nullable
478
+ private static void ensureAliasOrIndexExists (Context context , String expression ) {
479
+ IndexAbstraction indexAbstraction = context .getState ().getMetadata ().getIndicesLookup ().get (expression );
480
+ if (indexAbstraction == null ) {
481
+ throw notFoundException (expression );
482
+ }
483
+ // treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api)
484
+ if (indexAbstraction .getType () == Type .ALIAS && context .getOptions ().ignoreAliases ()) {
485
+ throw aliasesNotSupportedException (expression );
486
+ }
487
+ if (indexAbstraction .isDataStreamRelated () && context .includeDataStreams () == false ) {
488
+ IndexNotFoundException infe = notFoundException (expression );
489
+ // Allows callers to handle IndexNotFoundException differently based on whether data streams were excluded.
490
+ infe .addMetadata (EXCLUDED_DATA_STREAMS_KEY , "true" );
491
+ throw infe ;
492
+ }
493
+ }
494
+
491
495
private static boolean shouldTrackConcreteIndex (Context context , IndicesOptions options , Index index ) {
492
496
if (context .systemIndexAccessLevel == SystemIndexAccessLevel .BACKWARDS_COMPATIBLE_ONLY
493
497
&& context .netNewSystemIndexPredicate .test (index .getName ())) {
@@ -1215,7 +1219,7 @@ private static Collection<String> innerResolve(Context context, List<String> exp
1215
1219
matchingOpenClosedNames .forEachOrdered (result ::add );
1216
1220
}
1217
1221
if (emptyWildcardExpansion .get ()) {
1218
- throw indexNotFoundException (expression );
1222
+ throw notFoundException (expression );
1219
1223
}
1220
1224
} else {
1221
1225
if (isExclusion ) {
@@ -1247,7 +1251,7 @@ private static Collection<String> innerResolve(Context context, List<String> exp
1247
1251
1248
1252
private static String validateAliasOrIndex (String expression ) {
1249
1253
if (Strings .isEmpty (expression )) {
1250
- throw indexNotFoundException (expression );
1254
+ throw notFoundException (expression );
1251
1255
}
1252
1256
// Expressions can not start with an underscore. This is reserved for APIs. If the check gets here, the API
1253
1257
// does not exist and the path is interpreted as an expression. If the expression begins with an underscore,
@@ -1259,28 +1263,6 @@ private static String validateAliasOrIndex(String expression) {
1259
1263
return expression ;
1260
1264
}
1261
1265
1262
- @ Nullable
1263
- private static void ensureAliasOrIndexExists (Context context , String expression ) {
1264
- final IndicesOptions options = context .getOptions ();
1265
- IndexAbstraction indexAbstraction = context .getState ().getMetadata ().getIndicesLookup ().get (expression );
1266
- if (indexAbstraction == null ) {
1267
- throw indexNotFoundException (expression );
1268
- }
1269
- // treat aliases as unavailable indices when ignoreAliases is set to true (e.g. delete index and update aliases api)
1270
- if (indexAbstraction .getType () == Type .ALIAS && options .ignoreAliases ()) {
1271
- throw aliasesNotSupportedException (expression );
1272
- }
1273
- if (indexAbstraction .isDataStreamRelated () && context .includeDataStreams () == false ) {
1274
- throw indexNotFoundException (expression );
1275
- }
1276
- }
1277
-
1278
- private static IndexNotFoundException indexNotFoundException (String expression ) {
1279
- IndexNotFoundException infe = new IndexNotFoundException (expression );
1280
- infe .setResources ("index_or_alias" , expression );
1281
- return infe ;
1282
- }
1283
-
1284
1266
private static IndexMetadata .State excludeState (IndicesOptions options ) {
1285
1267
final IndexMetadata .State excludeState ;
1286
1268
if (options .expandWildcardsOpen () && options .expandWildcardsClosed ()) {
0 commit comments