Search in sources :

Example 11 with ComposableIndexTemplate

use of org.opensearch.cluster.metadata.ComposableIndexTemplate in project OpenSearch by opensearch-project.

the class RestTemplatesAction method buildTable.

private Table buildTable(RestRequest request, ClusterStateResponse clusterStateResponse, String patternString) {
    Table table = getTableWithHeader(request);
    Metadata metadata = clusterStateResponse.getState().metadata();
    for (ObjectObjectCursor<String, IndexTemplateMetadata> entry : metadata.templates()) {
        IndexTemplateMetadata indexData = entry.value;
        if (patternString == null || Regex.simpleMatch(patternString, indexData.name())) {
            table.startRow();
            table.addCell(indexData.name());
            table.addCell("[" + String.join(", ", indexData.patterns()) + "]");
            table.addCell(indexData.getOrder());
            table.addCell(indexData.getVersion());
            table.addCell("");
            table.endRow();
        }
    }
    for (Map.Entry<String, ComposableIndexTemplate> entry : metadata.templatesV2().entrySet()) {
        String name = entry.getKey();
        ComposableIndexTemplate template = entry.getValue();
        if (patternString == null || Regex.simpleMatch(patternString, name)) {
            table.startRow();
            table.addCell(name);
            table.addCell("[" + String.join(", ", template.indexPatterns()) + "]");
            table.addCell(template.priorityOrZero());
            table.addCell(template.version());
            table.addCell("[" + String.join(", ", template.composedOf()) + "]");
            table.endRow();
        }
    }
    return table;
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Table(org.opensearch.common.Table) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) Map(java.util.Map)

Example 12 with ComposableIndexTemplate

use of org.opensearch.cluster.metadata.ComposableIndexTemplate in project OpenSearch by opensearch-project.

the class IndicesClientIT method testSimulateIndexTemplate.

public void testSimulateIndexTemplate() 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, org.opensearch.common.collect.Map.of("alias", alias));
    List<String> pattern = org.opensearch.common.collect.List.of("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));
    SimulateIndexTemplateRequest simulateIndexTemplateRequest = new SimulateIndexTemplateRequest("pattern");
    AliasMetadata simulationAlias = AliasMetadata.builder("simulation-alias").writeIndex(true).build();
    ComposableIndexTemplate simulationTemplate = new ComposableIndexTemplate(pattern, new Template(null, null, org.opensearch.common.collect.Map.of("simulation-alias", simulationAlias)), Collections.emptyList(), 2L, 1L, new HashMap<>(), null);
    PutComposableIndexTemplateRequest newIndexTemplateReq = new PutComposableIndexTemplateRequest().name("used-for-simulation").create(true).indexTemplate(indexTemplate);
    newIndexTemplateReq.indexTemplate(simulationTemplate);
    simulateIndexTemplateRequest.indexTemplateV2Request(newIndexTemplateReq);
    SimulateIndexTemplateResponse simulateResponse = execute(simulateIndexTemplateRequest, highLevelClient().indices()::simulateIndexTemplate, highLevelClient().indices()::simulateIndexTemplateAsync);
    Map<String, AliasMetadata> aliases = simulateResponse.resolvedTemplate().aliases();
    assertThat(aliases, is(notNullValue()));
    assertThat("the template we provided for the simulation has a higher priority than the one in the system", aliases.get("simulation-alias"), is(notNullValue()));
    assertThat(aliases.get("simulation-alias").getAlias(), is("simulation-alias"));
    assertThat("existing template overlaps the higher priority template we provided for the simulation", simulateResponse.overlappingTemplates().get("my-template").get(0), is("pattern"));
}
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) ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) Template(org.opensearch.cluster.metadata.Template) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SimulateIndexTemplateResponse(org.opensearch.client.indices.SimulateIndexTemplateResponse) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Example 13 with ComposableIndexTemplate

use of org.opensearch.cluster.metadata.ComposableIndexTemplate 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 14 with ComposableIndexTemplate

use of org.opensearch.cluster.metadata.ComposableIndexTemplate in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testSimulateIndexTemplate.

public void testSimulateIndexTemplate() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        // <1>
        PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest().name("my-template");
        Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null);
        ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(Arrays.asList("pattern-1", "log-*"), template, null, null, null, null);
        request.indexTemplate(composableIndexTemplate);
        assertTrue(client.indices().putIndexTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::simulate-index-template-request
    // <1>
    SimulateIndexTemplateRequest simulateRequest = new SimulateIndexTemplateRequest("log-000001");
    PutComposableIndexTemplateRequest newIndexTemplateRequest = new PutComposableIndexTemplateRequest().name("used-for-simulation");
    Settings settings = Settings.builder().put("index.number_of_shards", 6).build();
    // <2>
    Template template = new Template(settings, null, null);
    ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(Arrays.asList("log-*"), template, null, 90L, null, null);
    newIndexTemplateRequest.indexTemplate(composableIndexTemplate);
    // <2>
    simulateRequest.indexTemplateV2Request(newIndexTemplateRequest);
    // end::simulate-index-template-request
    // tag::simulate-index-template-response
    SimulateIndexTemplateResponse simulateIndexTemplateResponse = client.indices().simulateIndexTemplate(simulateRequest, RequestOptions.DEFAULT);
    // <1>
    assertThat(simulateIndexTemplateResponse.resolvedTemplate().settings().get("index.number_of_shards"), is("6"));
    assertThat(simulateIndexTemplateResponse.overlappingTemplates().get("my-template"), // <2>
    containsInAnyOrder("pattern-1", "log-*"));
    // end::simulate-index-template-response
    // tag::simulate-index-template-execute-listener
    ActionListener<SimulateIndexTemplateResponse> listener = new ActionListener<SimulateIndexTemplateResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::simulate-index-template-execute-listener
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::simulate-index-template-execute-async
    // <1>
    client.indices().simulateIndexTemplateAsync(simulateRequest, RequestOptions.DEFAULT, listener);
    // end::simulate-index-template-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : ComposableIndexTemplate(org.opensearch.cluster.metadata.ComposableIndexTemplate) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SimulateIndexTemplateResponse(org.opensearch.client.indices.SimulateIndexTemplateResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) 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)

Example 15 with ComposableIndexTemplate

use of org.opensearch.cluster.metadata.ComposableIndexTemplate 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

ComposableIndexTemplate (org.opensearch.cluster.metadata.ComposableIndexTemplate)24 Template (org.opensearch.cluster.metadata.Template)16 Matchers.containsString (org.hamcrest.Matchers.containsString)10 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)10 Metadata (org.opensearch.cluster.metadata.Metadata)9 HashMap (java.util.HashMap)8 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)8 IndexTemplateMetadata (org.opensearch.cluster.metadata.IndexTemplateMetadata)8 ComponentTemplate (org.opensearch.cluster.metadata.ComponentTemplate)7 PutComposableIndexTemplateRequest (org.opensearch.client.indices.PutComposableIndexTemplateRequest)6 CompressedXContent (org.opensearch.common.compress.CompressedXContent)6 ClusterState (org.opensearch.cluster.ClusterState)5 IOException (java.io.IOException)4 Map (java.util.Map)4 ActionListener (org.opensearch.action.ActionListener)4 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)4 Settings (org.opensearch.common.settings.Settings)4 IndexSettings (org.opensearch.index.IndexSettings)4 List (java.util.List)3 CountDownLatch (java.util.concurrent.CountDownLatch)3