Search in sources :

Example 1 with PutIndexTemplateRequest

use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testPutTemplate.

@SuppressWarnings("unchecked")
public void testPutTemplate() throws Exception {
    PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template").patterns(Arrays.asList("pattern-1", "name-*")).order(10).create(randomBoolean()).settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")).mapping("{ \"properties\": { \"host_name\": { \"type\": \"keyword\" } } }", XContentType.JSON).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));
    AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest, highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync);
    assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));
    Map<String, Object> templates = getAsMap("/_template/my-template");
    assertThat(templates.keySet(), hasSize(1));
    assertThat(extractValue("my-template.order", templates), equalTo(10));
    assertThat(extractRawValues("my-template.index_patterns", templates), contains("pattern-1", "name-*"));
    assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3"));
    assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0"));
    assertThat(extractValue("my-template.mappings.properties.host_name.type", templates), equalTo("keyword"));
    assertThat((Map<String, String>) extractValue("my-template.aliases.alias-1", templates), hasEntry("index_routing", "abc"));
    assertThat((Map<String, String>) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz"));
}
Also used : Alias(org.opensearch.action.admin.indices.alias.Alias) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with PutIndexTemplateRequest

use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testPutTemplateWithTypesUsingUntypedAPI.

public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception {
    PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template").patterns(Arrays.asList("pattern-1", "name-*")).order(10).create(randomBoolean()).settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")).mapping("{" + "  \"my_doc_type\": {" + "    \"properties\": {" + "      \"host_name\": {" + "        \"type\": \"keyword\"" + "      }" + "    }" + "  }" + "}", XContentType.JSON).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));
    OpenSearchStatusException badMappingError = expectThrows(OpenSearchStatusException.class, () -> execute(putTemplateRequest, highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync));
    assertThat(badMappingError.getDetailedMessage(), containsString("Root mapping definition has unsupported parameters:  [my_doc_type"));
}
Also used : Alias(org.opensearch.action.admin.indices.alias.Alias) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 3 with PutIndexTemplateRequest

use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConvertersTests method testPutTemplateRequest.

public void testPutTemplateRequest() throws Exception {
    Map<String, String> names = new HashMap<>();
    names.put("log", "log");
    names.put("template#1", "template%231");
    names.put("-#template", "-%23template");
    names.put("foo^bar", "foo%5Ebar");
    PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest(OpenSearchTestCase.randomFrom(names.keySet())).patterns(Arrays.asList(OpenSearchTestCase.generateRandomStringArray(20, 100, false, false)));
    if (OpenSearchTestCase.randomBoolean()) {
        putTemplateRequest.order(OpenSearchTestCase.randomInt());
    }
    if (OpenSearchTestCase.randomBoolean()) {
        putTemplateRequest.version(OpenSearchTestCase.randomInt());
    }
    if (OpenSearchTestCase.randomBoolean()) {
        putTemplateRequest.settings(Settings.builder().put("setting-" + OpenSearchTestCase.randomInt(), OpenSearchTestCase.randomTimeValue()));
    }
    Map<String, String> expectedParams = new HashMap<>();
    if (OpenSearchTestCase.randomBoolean()) {
        putTemplateRequest.mapping("{ \"properties\": { \"field-" + OpenSearchTestCase.randomInt() + "\" : { \"type\" : \"" + OpenSearchTestCase.randomFrom("text", "keyword") + "\" }}}", XContentType.JSON);
    }
    if (OpenSearchTestCase.randomBoolean()) {
        putTemplateRequest.alias(new Alias("alias-" + OpenSearchTestCase.randomInt()));
    }
    if (OpenSearchTestCase.randomBoolean()) {
        expectedParams.put("create", Boolean.TRUE.toString());
        putTemplateRequest.create(true);
    }
    if (OpenSearchTestCase.randomBoolean()) {
        String cause = OpenSearchTestCase.randomUnicodeOfCodepointLengthBetween(1, 50);
        putTemplateRequest.cause(cause);
        expectedParams.put("cause", cause);
    }
    RequestConvertersTests.setRandomMasterTimeout(putTemplateRequest, expectedParams);
    Request request = IndicesRequestConverters.putTemplate(putTemplateRequest);
    Assert.assertThat(request.getEndpoint(), equalTo("/_template/" + names.get(putTemplateRequest.name())));
    Assert.assertThat(request.getParameters(), equalTo(expectedParams));
    RequestConvertersTests.assertToXContentBody(putTemplateRequest, request.getEntity());
}
Also used : HashMap(java.util.HashMap) Alias(org.opensearch.action.admin.indices.alias.Alias) RandomCreateIndexGenerator.randomAlias(org.opensearch.index.RandomCreateIndexGenerator.randomAlias) 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) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest)

Example 4 with PutIndexTemplateRequest

use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testDeleteTemplate.

public void testDeleteTemplate() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
        putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
        putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
        assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::delete-template-request
    DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest();
    // <1>
    request.name("my-template");
    // end::delete-template-request
    // tag::delete-template-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::delete-template-request-masterTimeout
    // tag::delete-template-execute
    AcknowledgedResponse deleteTemplateAcknowledge = client.indices().deleteTemplate(request, RequestOptions.DEFAULT);
    // end::delete-template-execute
    // tag::delete-template-response
    // <1>
    boolean acknowledged = deleteTemplateAcknowledge.isAcknowledged();
    // end::delete-template-response
    assertThat(acknowledged, equalTo(true));
    {
        PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
        putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
        putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
        assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::delete-template-execute-listener
    ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::delete-template-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::delete-template-execute-async
    // <1>
    client.indices().deleteTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::delete-template-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException)

Example 5 with PutIndexTemplateRequest

use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConverters method putIndexTemplate.

static Request putIndexTemplate(PutComposableIndexTemplateRequest putIndexTemplateRequest) throws IOException {
    String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_index_template").addPathPart(putIndexTemplateRequest.name()).build();
    Request request = new Request(HttpPut.METHOD_NAME, endpoint);
    RequestConverters.Params params = new RequestConverters.Params();
    params.withMasterTimeout(putIndexTemplateRequest.masterNodeTimeout());
    if (putIndexTemplateRequest.create()) {
        params.putParam("create", Boolean.TRUE.toString());
    }
    if (Strings.hasText(putIndexTemplateRequest.cause())) {
        params.putParam("cause", putIndexTemplateRequest.cause());
    }
    request.addParameters(params.asMap());
    request.setEntity(RequestConverters.createEntity(putIndexTemplateRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
    return request;
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) DataStreamsStatsRequest(org.opensearch.client.indices.DataStreamsStatsRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) 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) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest)

Aggregations

PutIndexTemplateRequest (org.opensearch.client.indices.PutIndexTemplateRequest)11 Alias (org.opensearch.action.admin.indices.alias.Alias)5 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)5 GetIndexTemplatesRequest (org.opensearch.client.indices.GetIndexTemplatesRequest)5 OpenSearchException (org.opensearch.OpenSearchException)4 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 ActionListener (org.opensearch.action.ActionListener)3 LatchedActionListener (org.opensearch.action.LatchedActionListener)3 IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)3 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)3 ClearIndicesCacheRequest (org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)3 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)3 FlushRequest (org.opensearch.action.admin.indices.flush.FlushRequest)3 ForceMergeRequest (org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest)3 OpenIndexRequest (org.opensearch.action.admin.indices.open.OpenIndexRequest)3 RefreshRequest (org.opensearch.action.admin.indices.refresh.RefreshRequest)3