Search in sources :

Example 1 with GetIndexTemplatesRequest

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

the class IndicesRequestConverters method getIndexTemplates.

static Request getIndexTemplates(GetComposableIndexTemplateRequest getIndexTemplatesRequest) {
    final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_index_template").addPathPart(getIndexTemplatesRequest.name()).build();
    final Request request = new Request(HttpGet.METHOD_NAME, endpoint);
    final RequestConverters.Params params = new RequestConverters.Params();
    params.withLocal(getIndexTemplatesRequest.isLocal());
    params.withMasterTimeout(getIndexTemplatesRequest.getMasterNodeTimeout());
    request.addParameters(params.asMap());
    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)

Example 2 with GetIndexTemplatesRequest

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

the class IndicesRequestConverters method getTemplates.

static Request getTemplates(GetIndexTemplatesRequest getIndexTemplatesRequest) {
    final String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_template").addCommaSeparatedPathParts(getIndexTemplatesRequest.names()).build();
    final Request request = new Request(HttpGet.METHOD_NAME, endpoint);
    final RequestConverters.Params params = new RequestConverters.Params();
    params.withLocal(getIndexTemplatesRequest.isLocal());
    params.withMasterTimeout(getIndexTemplatesRequest.getMasterNodeTimeout());
    request.addParameters(params.asMap());
    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)

Example 3 with GetIndexTemplatesRequest

use of org.opensearch.client.indices.GetIndexTemplatesRequest 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 4 with GetIndexTemplatesRequest

use of org.opensearch.client.indices.GetIndexTemplatesRequest 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)

Example 5 with GetIndexTemplatesRequest

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

the class IndicesRequestConvertersTests method testGetTemplateRequest.

public void testGetTemplateRequest() throws Exception {
    Map<String, String> encodes = new HashMap<>();
    encodes.put("log", "log");
    encodes.put("1", "1");
    encodes.put("template#1", "template%231");
    encodes.put("template-*", "template-*");
    encodes.put("foo^bar", "foo%5Ebar");
    List<String> names = OpenSearchTestCase.randomSubsetOf(1, encodes.keySet());
    GetIndexTemplatesRequest getTemplatesRequest = new GetIndexTemplatesRequest(names);
    Map<String, String> expectedParams = new HashMap<>();
    RequestConvertersTests.setRandomMasterTimeout(getTemplatesRequest::setMasterNodeTimeout, expectedParams);
    RequestConvertersTests.setRandomLocal(getTemplatesRequest::setLocal, expectedParams);
    Request request = IndicesRequestConverters.getTemplates(getTemplatesRequest);
    Assert.assertThat(request.getEndpoint(), equalTo("/_template/" + names.stream().map(encodes::get).collect(Collectors.joining(","))));
    Assert.assertThat(request.getParameters(), equalTo(expectedParams));
    Assert.assertThat(request.getEntity(), nullValue());
    expectThrows(NullPointerException.class, () -> new GetIndexTemplatesRequest((String[]) null));
    expectThrows(NullPointerException.class, () -> new GetIndexTemplatesRequest((List<String>) null));
    expectThrows(IllegalArgumentException.class, () -> new GetIndexTemplatesRequest(singletonList(randomBoolean() ? "" : null)));
    expectThrows(IllegalArgumentException.class, () -> new GetIndexTemplatesRequest(new String[] { (randomBoolean() ? "" : null) }));
}
Also used : 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) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Aggregations

GetIndexTemplatesRequest (org.opensearch.client.indices.GetIndexTemplatesRequest)5 PutIndexTemplateRequest (org.opensearch.client.indices.PutIndexTemplateRequest)5 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)4 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 GetSettingsRequest (org.opensearch.action.admin.indices.settings.get.GetSettingsRequest)3 UpdateSettingsRequest (org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest)3 ValidateQueryRequest (org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest)3 AnalyzeRequest (org.opensearch.client.indices.AnalyzeRequest)3 CloseIndexRequest (org.opensearch.client.indices.CloseIndexRequest)3 CreateDataStreamRequest (org.opensearch.client.indices.CreateDataStreamRequest)3 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)3 DeleteAliasRequest (org.opensearch.client.indices.DeleteAliasRequest)3 DeleteDataStreamRequest (org.opensearch.client.indices.DeleteDataStreamRequest)3