Search in sources :

Example 1 with GetIndexTemplatesResponse

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

the class IndicesClientDocumentationIT method testGetTemplates.

public void testGetTemplates() 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).put("index.number_of_replicas", 1));
        putRequest.mapping("{ \"properties\": { \"message\": { \"type\": \"text\" } } }", XContentType.JSON);
        assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::get-templates-request
    // <1>
    GetIndexTemplatesRequest request = new GetIndexTemplatesRequest("my-template");
    // <2>
    request = new GetIndexTemplatesRequest("template-1", "template-2");
    // <3>
    request = new GetIndexTemplatesRequest("my-*");
    // end::get-templates-request
    // tag::get-templates-request-masterTimeout
    // <1>
    request.setMasterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.setMasterNodeTimeout("1m");
    // end::get-templates-request-masterTimeout
    // tag::get-templates-execute
    GetIndexTemplatesResponse getTemplatesResponse = client.indices().getIndexTemplate(request, RequestOptions.DEFAULT);
    // end::get-templates-execute
    // tag::get-templates-response
    // <1>
    List<IndexTemplateMetadata> templates = getTemplatesResponse.getIndexTemplates();
    // end::get-templates-response
    assertThat(templates, hasSize(1));
    assertThat(templates.get(0).name(), equalTo("my-template"));
    // tag::get-templates-execute-listener
    ActionListener<GetIndexTemplatesResponse> listener = new ActionListener<GetIndexTemplatesResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::get-templates-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-templates-execute-async
    // <1>
    client.indices().getIndexTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::get-templates-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) GetIndexTemplatesResponse(org.opensearch.client.indices.GetIndexTemplatesResponse) IndexTemplateMetadata(org.opensearch.client.indices.IndexTemplateMetadata) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException)

Example 2 with GetIndexTemplatesResponse

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

the class IndicesClientIT method testCRUDIndexTemplate.

public void testCRUDIndexTemplate() throws Exception {
    RestHighLevelClient client = highLevelClient();
    PutIndexTemplateRequest putTemplate1 = new PutIndexTemplateRequest("template-1").patterns(Arrays.asList("pattern-1", "name-1")).alias(new Alias("alias-1"));
    assertThat(execute(putTemplate1, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged(), equalTo(true));
    PutIndexTemplateRequest putTemplate2 = new PutIndexTemplateRequest("template-2").patterns(Arrays.asList("pattern-2", "name-2")).mapping("{\"properties\": { \"name\": { \"type\": \"text\" }}}", XContentType.JSON).settings(Settings.builder().put("number_of_shards", "2").put("number_of_replicas", "0"));
    assertThat(execute(putTemplate2, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged(), equalTo(true));
    GetIndexTemplatesResponse getTemplate1 = execute(new GetIndexTemplatesRequest("template-1"), client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync);
    assertThat(getTemplate1.getIndexTemplates(), hasSize(1));
    IndexTemplateMetadata template1 = getTemplate1.getIndexTemplates().get(0);
    assertThat(template1.name(), equalTo("template-1"));
    assertThat(template1.patterns(), contains("pattern-1", "name-1"));
    assertTrue(template1.aliases().containsKey("alias-1"));
    GetIndexTemplatesResponse getTemplate2 = execute(new GetIndexTemplatesRequest("template-2"), client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync);
    assertThat(getTemplate2.getIndexTemplates(), hasSize(1));
    IndexTemplateMetadata template2 = getTemplate2.getIndexTemplates().get(0);
    assertThat(template2.name(), equalTo("template-2"));
    assertThat(template2.patterns(), contains("pattern-2", "name-2"));
    assertTrue(template2.aliases().isEmpty());
    assertThat(template2.settings().get("index.number_of_shards"), equalTo("2"));
    assertThat(template2.settings().get("index.number_of_replicas"), equalTo("0"));
    // New API returns a MappingMetadata class rather than CompressedXContent for the mapping
    assertTrue(template2.mappings().sourceAsMap().containsKey("properties"));
    @SuppressWarnings("unchecked") Map<String, Object> props = (Map<String, Object>) template2.mappings().sourceAsMap().get("properties");
    assertTrue(props.containsKey("name"));
    List<String> names = randomBoolean() ? Arrays.asList("*plate-1", "template-2") : Arrays.asList("template-*");
    GetIndexTemplatesRequest getBothRequest = new GetIndexTemplatesRequest(names);
    GetIndexTemplatesResponse getBoth = execute(getBothRequest, client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync);
    assertThat(getBoth.getIndexTemplates(), hasSize(2));
    assertThat(getBoth.getIndexTemplates().stream().map(IndexTemplateMetadata::name).toArray(), arrayContainingInAnyOrder("template-1", "template-2"));
    GetIndexTemplatesRequest getAllRequest = new GetIndexTemplatesRequest();
    GetIndexTemplatesResponse getAll = execute(getAllRequest, client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync);
    assertThat(getAll.getIndexTemplates().size(), greaterThanOrEqualTo(2));
    assertThat(getAll.getIndexTemplates().stream().map(IndexTemplateMetadata::name).collect(Collectors.toList()), hasItems("template-1", "template-2"));
    assertTrue(execute(new DeleteIndexTemplateRequest("template-1"), client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
    assertThat(expectThrows(OpenSearchException.class, () -> execute(new GetIndexTemplatesRequest("template-1"), client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
    assertThat(expectThrows(OpenSearchException.class, () -> execute(new DeleteIndexTemplateRequest("template-1"), client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
    assertThat(execute(new GetIndexTemplatesRequest("template-*"), client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync).getIndexTemplates(), hasSize(1));
    assertThat(execute(new GetIndexTemplatesRequest("template-*"), client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync).getIndexTemplates().get(0).name(), equalTo("template-2"));
    assertTrue(execute(new DeleteIndexTemplateRequest("template-*"), client.indices()::deleteTemplate, client.indices()::deleteTemplateAsync).isAcknowledged());
    assertThat(expectThrows(OpenSearchException.class, () -> execute(new GetIndexTemplatesRequest("template-*"), client.indices()::getIndexTemplate, client.indices()::getIndexTemplateAsync)).status(), equalTo(RestStatus.NOT_FOUND));
}
Also used : IndexTemplateMetadata(org.opensearch.client.indices.IndexTemplateMetadata) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) Alias(org.opensearch.action.admin.indices.alias.Alias) GetIndexTemplatesResponse(org.opensearch.client.indices.GetIndexTemplatesResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

GetIndexTemplatesRequest (org.opensearch.client.indices.GetIndexTemplatesRequest)2 GetIndexTemplatesResponse (org.opensearch.client.indices.GetIndexTemplatesResponse)2 IndexTemplateMetadata (org.opensearch.client.indices.IndexTemplateMetadata)2 PutIndexTemplateRequest (org.opensearch.client.indices.PutIndexTemplateRequest)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 OpenSearchException (org.opensearch.OpenSearchException)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 Alias (org.opensearch.action.admin.indices.alias.Alias)1 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)1 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1