@@ -103,11 +103,11 @@ public String[] concreteIndexNames(ClusterState state, IndicesOptions options, S
103103 return concreteIndexNames (context , indexExpressions );
104104 }
105105
106- /**
106+ /**
107107 * Translates the provided index expression into actual concrete indices, properly deduplicated.
108108 *
109- * @param state the cluster state containing all the data to resolve to expressions to concrete indices
110- * @param options defines how the aliases or indices need to be resolved to concrete indices
109+ * @param state the cluster state containing all the data to resolve to expressions to concrete indices
110+ * @param options defines how the aliases or indices need to be resolved to concrete indices
111111 * @param indexExpressions expressions that can be resolved to alias or index names.
112112 * @return the resolved concrete indices based on the cluster state, indices options and index expressions
113113 * @throws IndexNotFoundException if one of the index expressions is pointing to a missing index or alias and the
@@ -117,7 +117,26 @@ public String[] concreteIndexNames(ClusterState state, IndicesOptions options, S
117117 * indices options in the context don't allow such a case.
118118 */
119119 public Index [] concreteIndices (ClusterState state , IndicesOptions options , String ... indexExpressions ) {
120- Context context = new Context (state , options );
120+ Context context = new Context (state , options , false , false );
121+ return concreteIndices (context , indexExpressions );
122+ }
123+
124+ /**
125+ * Translates the provided index expression into actual concrete indices, properly deduplicated.
126+ *
127+ * @param state the cluster state containing all the data to resolve to expressions to concrete indices
128+ * @param options defines how the aliases or indices need to be resolved to concrete indices
129+ * @param resolveToWriteIndex defines whether to require that aliases resolve to their respective write indices
130+ * @param indexExpressions expressions that can be resolved to alias or index names.
131+ * @return the resolved concrete indices based on the cluster state, indices options and index expressions
132+ * @throws IndexNotFoundException if one of the index expressions is pointing to a missing index or alias and the
133+ * provided indices options in the context don't allow such a case, or if the final result of the indices resolution
134+ * contains no indices and the indices options in the context don't allow such a case.
135+ * @throws IllegalArgumentException if one of the aliases resolve to multiple indices and the provided
136+ * indices options in the context don't allow such a case.
137+ */
138+ public Index [] concreteIndices (ClusterState state , IndicesOptions options , boolean resolveToWriteIndex , String ... indexExpressions ) {
139+ Context context = new Context (state , options , false , resolveToWriteIndex );
121140 return concreteIndices (context , indexExpressions );
122141 }
123142
@@ -195,7 +214,7 @@ Index[] concreteIndices(Context context, String... indexExpressions) {
195214
196215 Collection <IndexMetaData > resolvedIndices = aliasOrIndex .getIndices ();
197216
198- if (aliasOrIndex .isAlias () && options . requireAliasesToWriteIndex ()) {
217+ if (aliasOrIndex .isAlias () && context . isResolveToWriteIndex ()) {
199218 AliasOrIndex .Alias alias = (AliasOrIndex .Alias ) aliasOrIndex ;
200219 IndexMetaData writeIndex = alias .getWriteIndex ();
201220 if (writeIndex == null ) {
@@ -269,6 +288,24 @@ public Index concreteSingleIndex(ClusterState state, IndicesRequest request) {
269288 return indices [0 ];
270289 }
271290
291+ /**
292+ * Utility method that allows to resolve an index expression to its corresponding single write index.
293+ *
294+ * @param state the cluster state containing all the data to resolve to expression to a concrete index
295+ * @param request The request that defines how the an alias or an index need to be resolved to a concrete index
296+ * and the expression that can be resolved to an alias or an index name.
297+ * @throws IllegalArgumentException if the index resolution does not lead to an index, or leads to more than one index
298+ * @return the write index obtained as a result of the index resolution
299+ */
300+ public Index concreteWriteIndex (ClusterState state , IndicesRequest request ) {
301+ String indexExpression = request .indices () != null && request .indices ().length > 0 ? request .indices ()[0 ] : null ;
302+ Index [] indices = concreteIndices (state , request .indicesOptions (), true , indexExpression );
303+ if (indices .length != 1 ) {
304+ throw new IllegalArgumentException ("unable to return a single index as the index and options provided got resolved to multiple indices" );
305+ }
306+ return indices [0 ];
307+ }
308+
272309 /**
273310 * @return whether the specified alias or index exists. If the alias or index contains datemath then that is resolved too.
274311 */
@@ -306,7 +343,7 @@ public String[] indexAliases(ClusterState state, String index, Predicate<AliasMe
306343 String ... expressions ) {
307344 // expand the aliases wildcard
308345 List <String > resolvedExpressions = expressions != null ? Arrays .asList (expressions ) : Collections .emptyList ();
309- Context context = new Context (state , IndicesOptions .lenientExpandOpen (), true );
346+ Context context = new Context (state , IndicesOptions .lenientExpandOpen (), true , false );
310347 for (ExpressionResolver expressionResolver : expressionResolvers ) {
311348 resolvedExpressions = expressionResolver .resolve (context , resolvedExpressions );
312349 }
@@ -526,24 +563,26 @@ static final class Context {
526563 private final IndicesOptions options ;
527564 private final long startTime ;
528565 private final boolean preserveAliases ;
566+ private final boolean resolveToWriteIndex ;
529567
530568 Context (ClusterState state , IndicesOptions options ) {
531569 this (state , options , System .currentTimeMillis ());
532570 }
533571
534- Context (ClusterState state , IndicesOptions options , boolean preserveAliases ) {
535- this (state , options , System .currentTimeMillis (), preserveAliases );
572+ Context (ClusterState state , IndicesOptions options , boolean preserveAliases , boolean resolveToWriteIndex ) {
573+ this (state , options , System .currentTimeMillis (), preserveAliases , resolveToWriteIndex );
536574 }
537575
538576 Context (ClusterState state , IndicesOptions options , long startTime ) {
539- this (state , options , startTime , false );
577+ this (state , options , startTime , false , false );
540578 }
541579
542- Context (ClusterState state , IndicesOptions options , long startTime , boolean preserveAliases ) {
580+ Context (ClusterState state , IndicesOptions options , long startTime , boolean preserveAliases , boolean resolveToWriteIndex ) {
543581 this .state = state ;
544582 this .options = options ;
545583 this .startTime = startTime ;
546584 this .preserveAliases = preserveAliases ;
585+ this .resolveToWriteIndex = resolveToWriteIndex ;
547586 }
548587
549588 public ClusterState getState () {
@@ -566,6 +605,15 @@ public long getStartTime() {
566605 boolean isPreserveAliases () {
567606 return preserveAliases ;
568607 }
608+
609+ /**
610+ * This is used to require that aliases resolve to their write-index. It is currently not used in conjunction
611+ * with <code>preserveAliases</code>.
612+ */
613+
614+ boolean isResolveToWriteIndex () {
615+ return resolveToWriteIndex ;
616+ }
569617 }
570618
571619 private interface ExpressionResolver {
0 commit comments