125125import org .elasticsearch .xpack .core .ml .stats .ForecastStats ;
126126import org .elasticsearch .xpack .core .ml .stats .StatsAccumulator ;
127127import org .elasticsearch .xpack .core .ml .utils .ExceptionsHelper ;
128+ import org .elasticsearch .xpack .core .ml .utils .MlIndexAndAlias ;
128129import org .elasticsearch .xpack .core .security .support .Exceptions ;
129130import org .elasticsearch .xpack .ml .MachineLearning ;
130131import org .elasticsearch .xpack .ml .job .categorization .GrokPatternCreator ;
@@ -306,11 +307,15 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen
306307 String readAliasName = AnomalyDetectorsIndex .jobResultsAliasedName (job .getId ());
307308 String writeAliasName = AnomalyDetectorsIndex .resultsWriteAlias (job .getId ());
308309 String tempIndexName = job .getInitialResultsIndexName ();
310+ // Find all indices starting with this name and pick the latest one
311+ String [] concreteIndices = resolver .concreteIndexNames (state , IndicesOptions .lenientExpandOpen (), tempIndexName + "*" );
312+ if (concreteIndices .length > 0 ) {
313+ tempIndexName = MlIndexAndAlias .latestIndex (concreteIndices );
314+ }
309315
310316 // Our read/write aliases should point to the concrete index
311317 // If the initial index is NOT an alias, either it is already a concrete index, or it does not exist yet
312318 if (state .getMetadata ().getProject ().hasAlias (tempIndexName )) {
313- String [] concreteIndices = resolver .concreteIndexNames (state , IndicesOptions .lenientExpandOpen (), tempIndexName );
314319
315320 // SHOULD NOT be closed as in typical call flow checkForLeftOverDocuments already verified this
316321 // if it is closed, we bailout and return an error
@@ -324,8 +329,8 @@ public void createJobResultIndex(Job job, ClusterState state, final ActionListen
324329 );
325330 return ;
326331 }
327- tempIndexName = concreteIndices [0 ];
328332 }
333+
329334 final String indexName = tempIndexName ;
330335
331336 ActionListener <Boolean > indexAndMappingsListener = ActionListener .wrap (success -> {
0 commit comments