66package org .opensearch .ml .indices ;
77
88import static org .opensearch .ml .common .CommonValue .META ;
9- import static org .opensearch .ml .common .CommonValue .ML_MODEL_INDEX ;
10- import static org .opensearch .ml .common .CommonValue .ML_TASK_INDEX ;
119import static org .opensearch .ml .common .CommonValue .SCHEMA_VERSION_FIELD ;
1210
1311import java .util .HashMap ;
1715import org .opensearch .action .admin .indices .create .CreateIndexRequest ;
1816import org .opensearch .action .admin .indices .create .CreateIndexResponse ;
1917import org .opensearch .action .admin .indices .mapping .put .PutMappingRequest ;
18+ import org .opensearch .action .admin .indices .settings .put .UpdateSettingsRequest ;
2019import org .opensearch .client .Client ;
2120import org .opensearch .cluster .metadata .IndexMetadata ;
2221import org .opensearch .cluster .service .ClusterService ;
@@ -38,11 +37,13 @@ public class MLIndicesHandler {
3837
3938 ClusterService clusterService ;
4039 Client client ;
41-
40+ private static final Map < String , Object > indexSettings = Map . of ( "index.auto_expand_replicas" , "0-5" );
4241 private static final Map <String , AtomicBoolean > indexMappingUpdated = new HashMap <>();
42+
4343 static {
44- indexMappingUpdated .put (ML_MODEL_INDEX , new AtomicBoolean (false ));
45- indexMappingUpdated .put (ML_TASK_INDEX , new AtomicBoolean (false ));
44+ for (MLIndex mlIndex : MLIndex .values ()) {
45+ indexMappingUpdated .put (mlIndex .getIndexName (), new AtomicBoolean (false ));
46+ }
4647 }
4748
4849 public void initModelGroupIndexIfAbsent (ActionListener <Boolean > listener ) {
@@ -83,7 +84,7 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener<Boolean> listener)
8384 log .error ("Failed to create index " + indexName , e );
8485 internalListener .onFailure (e );
8586 });
86- CreateIndexRequest request = new CreateIndexRequest (indexName ).mapping (mapping );
87+ CreateIndexRequest request = new CreateIndexRequest (indexName ).mapping (mapping ). settings ( indexSettings ) ;
8788 client .admin ().indices ().create (request , actionListener );
8889 } else {
8990 log .debug ("index:{} is already created" , indexName );
@@ -98,8 +99,23 @@ public void initMLIndexIfAbsent(MLIndex index, ActionListener<Boolean> listener)
9899 new PutMappingRequest ().indices (indexName ).source (mapping , XContentType .JSON ),
99100 ActionListener .wrap (response -> {
100101 if (response .isAcknowledged ()) {
101- indexMappingUpdated .get (indexName ).set (true );
102- internalListener .onResponse (true );
102+ UpdateSettingsRequest updateSettingRequest = new UpdateSettingsRequest ();
103+ updateSettingRequest .indices (indexName ).settings (indexSettings );
104+ client
105+ .admin ()
106+ .indices ()
107+ .updateSettings (updateSettingRequest , ActionListener .wrap (updateResponse -> {
108+ if (response .isAcknowledged ()) {
109+ indexMappingUpdated .get (indexName ).set (true );
110+ internalListener .onResponse (true );
111+ } else {
112+ internalListener
113+ .onFailure (new MLException ("Failed to update index setting for: " + indexName ));
114+ }
115+ }, exception -> {
116+ log .error ("Failed to update index setting for: " + indexName , exception );
117+ internalListener .onFailure (exception );
118+ }));
103119 } else {
104120 internalListener .onFailure (new MLException ("Failed to update index: " + indexName ));
105121 }
0 commit comments