Search in sources :

Example 6 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)

Example 7 with PutIndexTemplateRequest

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

the class IndicesRequestConverters method putTemplate.

static Request putTemplate(PutIndexTemplateRequest putIndexTemplateRequest) throws IOException {
    String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_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)

Example 8 with PutIndexTemplateRequest

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

the class IndicesClientDocumentationIT method testPutTemplate.

public void testPutTemplate() throws Exception {
    RestHighLevelClient client = highLevelClient();
    // tag::put-template-request
    // <1>
    PutIndexTemplateRequest request = new PutIndexTemplateRequest("my-template");
    // <2>
    request.patterns(Arrays.asList("pattern-1", "log-*"));
    // end::put-template-request
    // tag::put-template-request-settings
    request.settings(// <1>
    Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1));
    // end::put-template-request-settings
    {
        // tag::put-template-request-mappings-json
        // <1>
        request.mapping("{\n" + "  \"properties\": {\n" + "    \"message\": {\n" + "      \"type\": \"text\"\n" + "    }\n" + "  }\n" + "}", XContentType.JSON);
        // end::put-template-request-mappings-json
        assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
    }
    {
        // tag::put-template-request-mappings-map
        Map<String, Object> jsonMap = new HashMap<>();
        {
            Map<String, Object> properties = new HashMap<>();
            {
                Map<String, Object> message = new HashMap<>();
                message.put("type", "text");
                properties.put("message", message);
            }
            jsonMap.put("properties", properties);
        }
        // <1>
        request.mapping(jsonMap);
        // end::put-template-request-mappings-map
        assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
    }
    {
        // tag::put-template-request-mappings-xcontent
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject("properties");
            {
                builder.startObject("message");
                {
                    builder.field("type", "text");
                }
                builder.endObject();
            }
            builder.endObject();
        }
        builder.endObject();
        // <1>
        request.mapping(builder);
        // end::put-template-request-mappings-xcontent
        assertTrue(client.indices().putTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::put-template-request-aliases
    // <1>
    request.alias(new Alias("twitter_alias").filter(QueryBuilders.termQuery("user", "foobar")));
    // <2>
    request.alias(new Alias("{index}_alias").searchRouting("xyz"));
    // end::put-template-request-aliases
    // tag::put-template-request-order
    // <1>
    request.order(20);
    // end::put-template-request-order
    // tag::put-template-request-version
    // <1>
    request.version(4);
    // end::put-template-request-version
    // tag::put-template-whole-source
    request.source("{\n" + "  \"index_patterns\": [\n" + "    \"log-*\",\n" + "    \"pattern-1\"\n" + "  ],\n" + "  \"order\": 1,\n" + "  \"settings\": {\n" + "    \"number_of_shards\": 1\n" + "  },\n" + "  \"mappings\": {\n" + "    \"properties\": {\n" + "      \"message\": {\n" + "        \"type\": \"text\"\n" + "      }\n" + "    }\n" + "  },\n" + "  \"aliases\": {\n" + "    \"alias-1\": {},\n" + "    \"{index}-alias\": {}\n" + "  }\n" + "}", // <1>
    XContentType.JSON);
    // end::put-template-whole-source
    // tag::put-template-request-create
    // <1>
    request.create(true);
    // end::put-template-request-create
    // tag::put-template-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::put-template-request-masterTimeout
    // make test happy
    request.create(false);
    // tag::put-template-execute
    AcknowledgedResponse putTemplateResponse = client.indices().putTemplate(request, RequestOptions.DEFAULT);
    // end::put-template-execute
    // tag::put-template-response
    // <1>
    boolean acknowledged = putTemplateResponse.isAcknowledged();
    // end::put-template-response
    assertTrue(acknowledged);
    // tag::put-template-execute-listener
    ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::put-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::put-template-execute-async
    // <1>
    client.indices().putTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::put-template-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : HashMap(java.util.HashMap) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) Alias(org.opensearch.action.admin.indices.alias.Alias) Map(java.util.Map) HashMap(java.util.HashMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 9 with PutIndexTemplateRequest

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

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

the class IndicesClientIT method testPutTemplateBadRequests.

public void testPutTemplateBadRequests() throws Exception {
    RestHighLevelClient client = highLevelClient();
    // Failed to validate because index patterns are missing
    PutIndexTemplateRequest withoutPattern = new PutIndexTemplateRequest("t1");
    ValidationException withoutPatternError = expectThrows(ValidationException.class, () -> execute(withoutPattern, client.indices()::putTemplate, client.indices()::putTemplateAsync));
    assertThat(withoutPatternError.validationErrors(), contains("index patterns are missing"));
    // Create-only specified but an template exists already
    PutIndexTemplateRequest goodTemplate = new PutIndexTemplateRequest("t2").patterns(Arrays.asList("qa-*", "prod-*"));
    assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged());
    goodTemplate.create(true);
    OpenSearchException alreadyExistsError = expectThrows(OpenSearchException.class, () -> execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync));
    assertThat(alreadyExistsError.getDetailedMessage(), containsString("[type=illegal_argument_exception, reason=index_template [t2] already exists]"));
    goodTemplate.create(false);
    assertTrue(execute(goodTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync).isAcknowledged());
    // Rejected due to unknown settings
    PutIndexTemplateRequest unknownSettingTemplate = new PutIndexTemplateRequest("t3").patterns(Collections.singletonList("any")).settings(Settings.builder().put("this-setting-does-not-exist", 100));
    OpenSearchStatusException unknownSettingError = expectThrows(OpenSearchStatusException.class, () -> execute(unknownSettingTemplate, client.indices()::putTemplate, client.indices()::putTemplateAsync));
    assertThat(unknownSettingError.getDetailedMessage(), containsString("unknown setting [index.this-setting-does-not-exist]"));
}
Also used : ValidationException(org.opensearch.common.ValidationException) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) OpenSearchException(org.opensearch.OpenSearchException) OpenSearchStatusException(org.opensearch.OpenSearchStatusException)

Aggregations

PutIndexTemplateRequest (org.opensearch.client.indices.PutIndexTemplateRequest)12 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