Search in sources :

Example 1 with UpdateSettingsRequest

use of org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testIndexPutSettingNonExistent.

public void testIndexPutSettingNonExistent() throws IOException {
    String index = "index";
    UpdateSettingsRequest indexUpdateSettingsRequest = new UpdateSettingsRequest(index);
    String setting = "no_idea_what_you_are_talking_about";
    int value = 10;
    indexUpdateSettingsRequest.settings(Settings.builder().put(setting, value).build());
    OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(indexUpdateSettingsRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync));
    assertEquals(RestStatus.NOT_FOUND, exception.status());
    assertThat(exception.getMessage(), equalTo("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]"));
    createIndex(index, Settings.EMPTY);
    exception = expectThrows(OpenSearchException.class, () -> execute(indexUpdateSettingsRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync));
    assertThat(exception.status(), equalTo(RestStatus.BAD_REQUEST));
    assertThat(exception.getMessage(), equalTo("OpenSearch exception [type=illegal_argument_exception, " + "reason=unknown setting [index.no_idea_what_you_are_talking_about] please check that any required plugins are installed, " + "or check the breaking changes documentation for removed settings]"));
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) OpenSearchException(org.opensearch.OpenSearchException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with UpdateSettingsRequest

use of org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testIndexPutSettings.

public void testIndexPutSettings() throws IOException {
    final Setting<Integer> dynamicSetting = IndexMetadata.INDEX_NUMBER_OF_REPLICAS_SETTING;
    final String dynamicSettingKey = IndexMetadata.SETTING_NUMBER_OF_REPLICAS;
    final int dynamicSettingValue = 0;
    final Setting<String> staticSetting = IndexSettings.INDEX_CHECK_ON_STARTUP;
    final String staticSettingKey = IndexSettings.INDEX_CHECK_ON_STARTUP.getKey();
    final String staticSettingValue = "true";
    final Setting<Integer> unmodifiableSetting = IndexMetadata.INDEX_NUMBER_OF_SHARDS_SETTING;
    final String unmodifiableSettingKey = IndexMetadata.SETTING_NUMBER_OF_SHARDS;
    final int unmodifiableSettingValue = 3;
    String index = "index";
    createIndex(index, Settings.EMPTY);
    assertThat(dynamicSetting.getDefault(Settings.EMPTY), not(dynamicSettingValue));
    UpdateSettingsRequest dynamicSettingRequest = new UpdateSettingsRequest(index);
    dynamicSettingRequest.settings(Settings.builder().put(dynamicSettingKey, dynamicSettingValue).build());
    AcknowledgedResponse response = execute(dynamicSettingRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync);
    assertTrue(response.isAcknowledged());
    Map<String, Object> indexSettingsAsMap = getIndexSettingsAsMap(index);
    assertThat(indexSettingsAsMap.get(dynamicSettingKey), equalTo(String.valueOf(dynamicSettingValue)));
    assertThat(staticSetting.getDefault(Settings.EMPTY), not(staticSettingValue));
    UpdateSettingsRequest staticSettingRequest = new UpdateSettingsRequest(index);
    staticSettingRequest.settings(Settings.builder().put(staticSettingKey, staticSettingValue).build());
    OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(staticSettingRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync));
    assertThat(exception.getMessage(), startsWith("OpenSearch exception [type=illegal_argument_exception, " + "reason=Can't update non dynamic settings [[index.shard.check_on_startup]] for open indices [[index/"));
    indexSettingsAsMap = getIndexSettingsAsMap(index);
    assertNull(indexSettingsAsMap.get(staticSettingKey));
    closeIndex(index);
    response = execute(staticSettingRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync);
    assertTrue(response.isAcknowledged());
    openIndex(index);
    indexSettingsAsMap = getIndexSettingsAsMap(index);
    assertThat(indexSettingsAsMap.get(staticSettingKey), equalTo(staticSettingValue));
    assertThat(unmodifiableSetting.getDefault(Settings.EMPTY), not(unmodifiableSettingValue));
    UpdateSettingsRequest unmodifiableSettingRequest = new UpdateSettingsRequest(index);
    unmodifiableSettingRequest.settings(Settings.builder().put(unmodifiableSettingKey, unmodifiableSettingValue).build());
    exception = expectThrows(OpenSearchException.class, () -> execute(unmodifiableSettingRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync));
    assertThat(exception.getMessage(), startsWith("OpenSearch exception [type=illegal_argument_exception, " + "reason=Can't update non dynamic settings [[index.number_of_shards]] for open indices [[index/"));
    closeIndex(index);
    exception = expectThrows(OpenSearchException.class, () -> execute(unmodifiableSettingRequest, highLevelClient().indices()::putSettings, highLevelClient().indices()::putSettingsAsync));
    assertThat(exception.getMessage(), startsWith("OpenSearch exception [type=illegal_argument_exception, " + "reason=final index setting [index.number_of_shards], not updateable"));
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) OpenSearchException(org.opensearch.OpenSearchException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with UpdateSettingsRequest

use of org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConvertersTests method testIndexPutSettings.

public void testIndexPutSettings() throws IOException {
    String[] indices = OpenSearchTestCase.randomBoolean() ? null : RequestConvertersTests.randomIndicesNames(0, 2);
    UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(indices);
    Map<String, String> expectedParams = new HashMap<>();
    RequestConvertersTests.setRandomMasterTimeout(updateSettingsRequest, expectedParams);
    RequestConvertersTests.setRandomTimeout(updateSettingsRequest::timeout, AcknowledgedRequest.DEFAULT_ACK_TIMEOUT, expectedParams);
    RequestConvertersTests.setRandomIndicesOptions(updateSettingsRequest::indicesOptions, updateSettingsRequest::indicesOptions, expectedParams);
    if (OpenSearchTestCase.randomBoolean()) {
        updateSettingsRequest.setPreserveExisting(OpenSearchTestCase.randomBoolean());
        if (updateSettingsRequest.isPreserveExisting()) {
            expectedParams.put("preserve_existing", "true");
        }
    }
    Request request = IndicesRequestConverters.indexPutSettings(updateSettingsRequest);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    if (indices != null && indices.length > 0) {
        endpoint.add(String.join(",", indices));
    }
    endpoint.add("_settings");
    Assert.assertThat(endpoint.toString(), equalTo(request.getEndpoint()));
    Assert.assertEquals(HttpPut.METHOD_NAME, request.getMethod());
    RequestConvertersTests.assertToXContentBody(updateSettingsRequest, request.getEntity());
    Assert.assertEquals(expectedParams, request.getParameters());
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) HashMap(java.util.HashMap) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) StringJoiner(java.util.StringJoiner)

Example 4 with UpdateSettingsRequest

use of org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testIndexPutSettings.

@SuppressWarnings("unused")
public void testIndexPutSettings() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index"), RequestOptions.DEFAULT);
        assertTrue(createIndexResponse.isAcknowledged());
    }
    // tag::indices-put-settings-request
    // <1>
    UpdateSettingsRequest request = new UpdateSettingsRequest("index1");
    UpdateSettingsRequest requestMultiple = // <2>
    new UpdateSettingsRequest("index1", "index2");
    // <3>
    UpdateSettingsRequest requestAll = new UpdateSettingsRequest();
    // end::indices-put-settings-request
    // tag::indices-put-settings-create-settings
    String settingKey = "index.number_of_replicas";
    int settingValue = 0;
    Settings settings = Settings.builder().put(settingKey, settingValue).build();
    // end::indices-put-settings-create-settings
    // tag::indices-put-settings-request-index-settings
    request.settings(settings);
    // end::indices-put-settings-request-index-settings
    {
        // tag::indices-put-settings-settings-builder
        Settings.Builder settingsBuilder = Settings.builder().put(settingKey, settingValue);
        // <1>
        request.settings(settingsBuilder);
    // end::indices-put-settings-settings-builder
    }
    {
        // tag::indices-put-settings-settings-map
        Map<String, Object> map = new HashMap<>();
        map.put(settingKey, settingValue);
        // <1>
        request.settings(map);
    // end::indices-put-settings-settings-map
    }
    {
        // tag::indices-put-settings-settings-source
        request.settings("{\"index.number_of_replicas\": \"2\"}", // <1>
        XContentType.JSON);
    // end::indices-put-settings-settings-source
    }
    // tag::indices-put-settings-request-preserveExisting
    // <1>
    request.setPreserveExisting(false);
    // end::indices-put-settings-request-preserveExisting
    // tag::indices-put-settings-request-timeout
    // <1>
    request.timeout(TimeValue.timeValueMinutes(2));
    // <2>
    request.timeout("2m");
    // end::indices-put-settings-request-timeout
    // tag::indices-put-settings-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::indices-put-settings-request-masterTimeout
    // tag::indices-put-settings-request-indicesOptions
    // <1>
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    // end::indices-put-settings-request-indicesOptions
    // tag::indices-put-settings-execute
    AcknowledgedResponse updateSettingsResponse = client.indices().putSettings(request, RequestOptions.DEFAULT);
    // end::indices-put-settings-execute
    // tag::indices-put-settings-response
    // <1>
    boolean acknowledged = updateSettingsResponse.isAcknowledged();
    // end::indices-put-settings-response
    assertTrue(acknowledged);
    // tag::indices-put-settings-execute-listener
    ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {

        @Override
        public void onResponse(AcknowledgedResponse updateSettingsResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::indices-put-settings-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::indices-put-settings-execute-async
    // <1>
    client.indices().putSettingsAsync(request, RequestOptions.DEFAULT, listener);
    // end::indices-put-settings-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 5 with UpdateSettingsRequest

use of org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest in project OpenSearch by opensearch-project.

the class IndicesRequestIT method testUpdateSettings.

public void testUpdateSettings() {
    interceptTransportActions(UpdateSettingsAction.NAME);
    UpdateSettingsRequest updateSettingsRequest = new UpdateSettingsRequest(randomIndicesOrAliases()).settings(Settings.builder().put("refresh_interval", -1));
    internalCluster().coordOnlyNodeClient().admin().indices().updateSettings(updateSettingsRequest).actionGet();
    clearInterceptedActions();
    assertSameIndices(updateSettingsRequest, UpdateSettingsAction.NAME);
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest)

Aggregations

UpdateSettingsRequest (org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest)9 HashMap (java.util.HashMap)3 OpenSearchException (org.opensearch.OpenSearchException)3 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)3 OpenIndexRequest (org.opensearch.action.admin.indices.open.OpenIndexRequest)3 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)3 Settings (org.opensearch.common.settings.Settings)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ClusterRerouteRequest (org.opensearch.action.admin.cluster.reroute.ClusterRerouteRequest)2 IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)2 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)2 ClearIndicesCacheRequest (org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)2 CreateIndexRequest (org.opensearch.action.admin.indices.create.CreateIndexRequest)2 FlushRequest (org.opensearch.action.admin.indices.flush.FlushRequest)2 ForceMergeRequest (org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest)2 RefreshRequest (org.opensearch.action.admin.indices.refresh.RefreshRequest)2 GetSettingsRequest (org.opensearch.action.admin.indices.settings.get.GetSettingsRequest)2 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)2