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;
}
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;
}
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));
}
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));
}
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]"));
}
Aggregations