39
39
import org .opensearch .action .support .WriteRequest ;
40
40
import org .opensearch .action .support .master .AcknowledgedResponse ;
41
41
import org .opensearch .action .support .replication .ReplicationResponse ;
42
- import org .opensearch .ad .transport .IndexAnomalyDetectorResponse ;
43
42
import org .opensearch .client .Client ;
44
43
import org .opensearch .cluster .service .ClusterService ;
45
44
import org .opensearch .common .settings .Settings ;
@@ -456,16 +455,14 @@ protected void prepareConfigIndexing(boolean indexingDryRun, ActionListener<T> l
456
455
457
456
private void handlePutRequest (boolean indexingDryRun , ActionListener <T > listener ) {
458
457
handler .confirmJobRunning (clusterService , client , id , listener , () -> {
459
- handleFlattenResultIndexMappingUpdate (listener );
460
458
updateConfig (id , indexingDryRun , listener );
461
459
}, xContentRegistry );
462
460
}
463
461
464
462
private void handlePostRequest (boolean indexingDryRun , ActionListener <T > listener ) {
465
463
createConfig (indexingDryRun , ActionListener .wrap (createConfigResponse -> {
466
- if (shouldHandleFlattening (indexingDryRun , createConfigResponse )) {
467
- IndexAnomalyDetectorResponse response = (IndexAnomalyDetectorResponse ) createConfigResponse ;
468
- String configId = response .getId ();
464
+ if (shouldHandleFlattening (indexingDryRun )) {
465
+ String configId = RestHandlerUtils .getConfigIdFromIndexResponse (createConfigResponse );
469
466
String flattenedResultIndexAlias = timeSeriesIndices
470
467
.getFlattenedResultIndexAlias (config .getCustomResultIndexOrAlias (), configId );
471
468
String pipelineId = timeSeriesIndices .getFlattenResultIndexIngestPipelineId (configId );
@@ -487,13 +484,10 @@ private void handlePostRequest(boolean indexingDryRun, ActionListener<T> listene
487
484
}, listener ::onFailure ));
488
485
}
489
486
490
- private boolean shouldHandleFlattening (boolean indexingDryRun , Object createConfigResponse ) {
487
+ private boolean shouldHandleFlattening (boolean indexingDryRun ) {
491
488
Boolean flattenResultIndexMapping = config .getFlattenResultIndexMapping ();
492
489
493
- return !indexingDryRun
494
- && config .getCustomResultIndexOrAlias () != null
495
- && Boolean .TRUE .equals (flattenResultIndexMapping )
496
- && createConfigResponse instanceof IndexAnomalyDetectorResponse ;
490
+ return !indexingDryRun && config .getCustomResultIndexOrAlias () != null && Boolean .TRUE .equals (flattenResultIndexMapping );
497
491
}
498
492
499
493
protected void setupIngestPipeline (String configId , ActionListener <T > listener ) {
@@ -574,13 +568,13 @@ protected void updateResultIndexSetting(String pipelineId, String flattenedResul
574
568
}));
575
569
}
576
570
577
- private void handleFlattenResultIndexMappingUpdate (ActionListener <T > listener ) {
571
+ private void handleFlattenResultIndexMappingUpdate (Config existingConfig , ActionListener <T > listener ) {
578
572
if (config .getCustomResultIndexOrAlias () == null ) {
579
573
return ;
580
574
}
581
- if (config . getFlattenResultIndexMapping () != null && config .getFlattenResultIndexMapping ()) {
582
- setupIngestPipeline ( id , listener );
583
- } else {
575
+ if (Boolean . TRUE . equals ( existingConfig .getFlattenResultIndexMapping ())
576
+ && Boolean . FALSE . equals ( config . getFlattenResultIndexMapping ())
577
+ && existingConfig . getCustomResultIndexOrAlias () != null ) {
584
578
String pipelineId = timeSeriesIndices .getFlattenResultIndexIngestPipelineId (config .getId ());
585
579
client .admin ().cluster ().deletePipeline (new DeletePipelineRequest (pipelineId ), new ActionListener <AcknowledgedResponse >() {
586
580
@@ -611,6 +605,24 @@ public void onFailure(Exception e) {
611
605
}
612
606
}
613
607
});
608
+ } else if (Boolean .FALSE .equals (existingConfig .getFlattenResultIndexMapping ())
609
+ && Boolean .TRUE .equals (config .getFlattenResultIndexMapping ())
610
+ && existingConfig .getCustomResultIndexOrAlias () != null
611
+ ) {
612
+ String flattenedResultIndexAlias = timeSeriesIndices
613
+ .getFlattenedResultIndexAlias (config .getCustomResultIndexOrAlias (), config .getId ());
614
+ String pipelineId = timeSeriesIndices .getFlattenResultIndexIngestPipelineId (config .getId ());
615
+ timeSeriesIndices
616
+ .initFlattenedResultIndex (
617
+ flattenedResultIndexAlias ,
618
+ ActionListener .wrap (initResponse -> setupIngestPipeline (config .getId (), ActionListener .wrap (pipelineResponse -> {
619
+ updateResultIndexSetting (
620
+ pipelineId ,
621
+ flattenedResultIndexAlias ,
622
+ ActionListener .wrap (updateResponse -> listener .onResponse (updateResponse ), listener ::onFailure )
623
+ );
624
+ }, listener ::onFailure )), listener ::onFailure )
625
+ );
614
626
}
615
627
}
616
628
@@ -661,6 +673,7 @@ private void onGetConfigResponse(GetResponse response, boolean indexingDryRun, S
661
673
breakingUIChange = true ;
662
674
}
663
675
}
676
+ handleFlattenResultIndexMappingUpdate (existingConfig , listener );
664
677
665
678
ActionListener <Void > confirmBatchRunningListener = ActionListener
666
679
.wrap (
0 commit comments