use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.
the class IndicesClientIT method testPutTemplate.
@SuppressWarnings("unchecked")
public void testPutTemplate() throws Exception {
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template").patterns(Arrays.asList("pattern-1", "name-*")).order(10).create(randomBoolean()).settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")).mapping("{ \"properties\": { \"host_name\": { \"type\": \"keyword\" } } }", XContentType.JSON).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));
AcknowledgedResponse putTemplateResponse = execute(putTemplateRequest, highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync);
assertThat(putTemplateResponse.isAcknowledged(), equalTo(true));
Map<String, Object> templates = getAsMap("/_template/my-template");
assertThat(templates.keySet(), hasSize(1));
assertThat(extractValue("my-template.order", templates), equalTo(10));
assertThat(extractRawValues("my-template.index_patterns", templates), contains("pattern-1", "name-*"));
assertThat(extractValue("my-template.settings.index.number_of_shards", templates), equalTo("3"));
assertThat(extractValue("my-template.settings.index.number_of_replicas", templates), equalTo("0"));
assertThat(extractValue("my-template.mappings.properties.host_name.type", templates), equalTo("keyword"));
assertThat((Map<String, String>) extractValue("my-template.aliases.alias-1", templates), hasEntry("index_routing", "abc"));
assertThat((Map<String, String>) extractValue("my-template.aliases.{index}-write", templates), hasEntry("search_routing", "xyz"));
}
use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.
the class IndicesClientIT method testPutTemplateWithTypesUsingUntypedAPI.
public void testPutTemplateWithTypesUsingUntypedAPI() throws Exception {
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest("my-template").patterns(Arrays.asList("pattern-1", "name-*")).order(10).create(randomBoolean()).settings(Settings.builder().put("number_of_shards", "3").put("number_of_replicas", "0")).mapping("{" + " \"my_doc_type\": {" + " \"properties\": {" + " \"host_name\": {" + " \"type\": \"keyword\"" + " }" + " }" + " }" + "}", XContentType.JSON).alias(new Alias("alias-1").indexRouting("abc")).alias(new Alias("{index}-write").searchRouting("xyz"));
OpenSearchStatusException badMappingError = expectThrows(OpenSearchStatusException.class, () -> execute(putTemplateRequest, highLevelClient().indices()::putTemplate, highLevelClient().indices()::putTemplateAsync));
assertThat(badMappingError.getDetailedMessage(), containsString("Root mapping definition has unsupported parameters: [my_doc_type"));
}
use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.
the class IndicesRequestConvertersTests method testPutTemplateRequest.
public void testPutTemplateRequest() throws Exception {
Map<String, String> names = new HashMap<>();
names.put("log", "log");
names.put("template#1", "template%231");
names.put("-#template", "-%23template");
names.put("foo^bar", "foo%5Ebar");
PutIndexTemplateRequest putTemplateRequest = new PutIndexTemplateRequest(OpenSearchTestCase.randomFrom(names.keySet())).patterns(Arrays.asList(OpenSearchTestCase.generateRandomStringArray(20, 100, false, false)));
if (OpenSearchTestCase.randomBoolean()) {
putTemplateRequest.order(OpenSearchTestCase.randomInt());
}
if (OpenSearchTestCase.randomBoolean()) {
putTemplateRequest.version(OpenSearchTestCase.randomInt());
}
if (OpenSearchTestCase.randomBoolean()) {
putTemplateRequest.settings(Settings.builder().put("setting-" + OpenSearchTestCase.randomInt(), OpenSearchTestCase.randomTimeValue()));
}
Map<String, String> expectedParams = new HashMap<>();
if (OpenSearchTestCase.randomBoolean()) {
putTemplateRequest.mapping("{ \"properties\": { \"field-" + OpenSearchTestCase.randomInt() + "\" : { \"type\" : \"" + OpenSearchTestCase.randomFrom("text", "keyword") + "\" }}}", XContentType.JSON);
}
if (OpenSearchTestCase.randomBoolean()) {
putTemplateRequest.alias(new Alias("alias-" + OpenSearchTestCase.randomInt()));
}
if (OpenSearchTestCase.randomBoolean()) {
expectedParams.put("create", Boolean.TRUE.toString());
putTemplateRequest.create(true);
}
if (OpenSearchTestCase.randomBoolean()) {
String cause = OpenSearchTestCase.randomUnicodeOfCodepointLengthBetween(1, 50);
putTemplateRequest.cause(cause);
expectedParams.put("cause", cause);
}
RequestConvertersTests.setRandomMasterTimeout(putTemplateRequest, expectedParams);
Request request = IndicesRequestConverters.putTemplate(putTemplateRequest);
Assert.assertThat(request.getEndpoint(), equalTo("/_template/" + names.get(putTemplateRequest.name())));
Assert.assertThat(request.getParameters(), equalTo(expectedParams));
RequestConvertersTests.assertToXContentBody(putTemplateRequest, request.getEntity());
}
use of org.opensearch.client.indices.PutIndexTemplateRequest in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testDeleteTemplate.
public void testDeleteTemplate() 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));
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
}
// tag::delete-template-request
DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest();
// <1>
request.name("my-template");
// end::delete-template-request
// tag::delete-template-request-masterTimeout
// <1>
request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
// <2>
request.masterNodeTimeout("1m");
// end::delete-template-request-masterTimeout
// tag::delete-template-execute
AcknowledgedResponse deleteTemplateAcknowledge = client.indices().deleteTemplate(request, RequestOptions.DEFAULT);
// end::delete-template-execute
// tag::delete-template-response
// <1>
boolean acknowledged = deleteTemplateAcknowledge.isAcknowledged();
// end::delete-template-response
assertThat(acknowledged, equalTo(true));
{
PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
}
// tag::delete-template-execute-listener
ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {
@Override
public void onResponse(AcknowledgedResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::delete-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::delete-template-execute-async
// <1>
client.indices().deleteTemplateAsync(request, RequestOptions.DEFAULT, listener);
// end::delete-template-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
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;
}
Aggregations