Search in sources :

Example 1 with GetComposableIndexTemplatesResponse

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

the class IndicesClientIT method testIndexTemplates.

public void testIndexTemplates() throws Exception {
    String templateName = "my-template";
    Settings settings = Settings.builder().put("index.number_of_shards", 1).build();
    CompressedXContent mappings = new CompressedXContent("{\"properties\":{\"host_name\":{\"type\":\"keyword\"}}}");
    AliasMetadata alias = AliasMetadata.builder("alias").writeIndex(true).build();
    Template template = new Template(settings, mappings, Collections.singletonMap("alias", alias));
    List<String> pattern = Collections.singletonList("pattern");
    ComposableIndexTemplate indexTemplate = new ComposableIndexTemplate(pattern, template, Collections.emptyList(), 1L, 1L, new HashMap<>(), null);
    PutComposableIndexTemplateRequest putComposableIndexTemplateRequest = new PutComposableIndexTemplateRequest().name(templateName).create(true).indexTemplate(indexTemplate);
    AcknowledgedResponse response = execute(putComposableIndexTemplateRequest, highLevelClient().indices()::putIndexTemplate, highLevelClient().indices()::putIndexTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    ComposableIndexTemplateExistRequest composableIndexTemplateExistRequest = new ComposableIndexTemplateExistRequest(templateName);
    boolean exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
    assertTrue(exist);
    GetComposableIndexTemplateRequest getComposableIndexTemplateRequest = new GetComposableIndexTemplateRequest(templateName);
    GetComposableIndexTemplatesResponse getResponse = execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync);
    assertThat(getResponse.getIndexTemplates().size(), equalTo(1));
    assertThat(getResponse.getIndexTemplates().containsKey(templateName), equalTo(true));
    assertThat(getResponse.getIndexTemplates().get(templateName), equalTo(indexTemplate));
    DeleteComposableIndexTemplateRequest deleteComposableIndexTemplateRequest = new DeleteComposableIndexTemplateRequest(templateName);
    response = execute(deleteComposableIndexTemplateRequest, highLevelClient().indices()::deleteIndexTemplate, highLevelClient().indices()::deleteIndexTemplateAsync);
    assertThat(response.isAcknowledged(), equalTo(true));
    OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComposableIndexTemplateRequest, highLevelClient().indices()::getIndexTemplate, highLevelClient().indices()::getIndexTemplateAsync));
    assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
    exist = execute(composableIndexTemplateExistRequest, highLevelClient().indices()::existsIndexTemplate, highLevelClient().indices()::existsIndexTemplateAsync);
    assertFalse(exist);
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template) CompressedXContent(org.opensearch.common.compress.CompressedXContent) GetComposableIndexTemplatesResponse(org.opensearch.client.indices.GetComposableIndexTemplatesResponse) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Example 2 with GetComposableIndexTemplatesResponse

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

the class IndicesClientDocumentationIT method testGetIndexTemplatesV2.

public void testGetIndexTemplatesV2() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Template template = new Template(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build(), new CompressedXContent("{ \"properties\": { \"message\": { \"type\": \"text\" } } }"), null);
        PutComposableIndexTemplateRequest putRequest = new PutComposableIndexTemplateRequest().name("my-template").indexTemplate(new ComposableIndexTemplate(Arrays.asList("pattern-1", "log-*"), template, null, null, null, null));
        assertTrue(client.indices().putIndexTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::get-index-templates-v2-request
    // <1>
    GetComposableIndexTemplateRequest request = new GetComposableIndexTemplateRequest("my-template");
    // <2>
    request = new GetComposableIndexTemplateRequest("my-*");
    // end::get-index-templates-v2-request
    // tag::get-index-templates-v2-request-masterTimeout
    // <1>
    request.setMasterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.setMasterNodeTimeout("1m");
    // end::get-index-templates-v2-request-masterTimeout
    // tag::get-index-templates-v2-execute
    GetComposableIndexTemplatesResponse getTemplatesResponse = client.indices().getIndexTemplate(request, RequestOptions.DEFAULT);
    // end::get-index-templates-v2-execute
    // tag::get-index-templates-v2-response
    // <1>
    Map<String, ComposableIndexTemplate> templates = getTemplatesResponse.getIndexTemplates();
    // end::get-index-templates-v2-response
    assertThat(templates.size(), is(1));
    assertThat(templates.get("my-template"), is(notNullValue()));
    // tag::get-index-templates-v2-execute-listener
    ActionListener<GetComposableIndexTemplatesResponse> listener = new ActionListener<GetComposableIndexTemplatesResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::get-index-templates-v2-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::get-index-templates-v2-execute-async
    // <1>
    client.indices().getIndexTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::get-index-templates-v2-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) CompressedXContent(org.opensearch.common.compress.CompressedXContent) GetComposableIndexTemplatesResponse(org.opensearch.client.indices.GetComposableIndexTemplatesResponse) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest)

Aggregations

GetComposableIndexTemplateRequest (org.opensearch.client.indices.GetComposableIndexTemplateRequest)2 GetComposableIndexTemplatesResponse (org.opensearch.client.indices.GetComposableIndexTemplatesResponse)2 PutComposableIndexTemplateRequest (org.opensearch.client.indices.PutComposableIndexTemplateRequest)2 ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)2 Template (org.opensearch.cluster.metadata.Template)2 CompressedXContent (org.opensearch.common.compress.CompressedXContent)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchException (org.opensearch.OpenSearchException)1 OpenSearchStatusException (org.opensearch.OpenSearchStatusException)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)1 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1 ComposableIndexTemplateExistRequest (org.opensearch.client.indices.ComposableIndexTemplateExistRequest)1 DeleteComposableIndexTemplateRequest (org.opensearch.client.indices.DeleteComposableIndexTemplateRequest)1 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)1 ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)1