use of org.opensearch.client.indices.PutComponentTemplateRequest in project OpenSearch by opensearch-project.
the class ClusterClientDocumentationIT method testGetComponentTemplates.
public void testGetComponentTemplates() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null);
ComponentTemplate componentTemplate = new ComponentTemplate(template, null, null);
PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name("ct1").componentTemplate(componentTemplate);
client.cluster().putComponentTemplate(putComponentTemplateRequest, RequestOptions.DEFAULT);
assertTrue(client.cluster().putComponentTemplate(putComponentTemplateRequest, RequestOptions.DEFAULT).isAcknowledged());
}
// tag::get-component-templates-request
// <1>
GetComponentTemplatesRequest request = new GetComponentTemplatesRequest("ct1");
// end::get-component-templates-request
// tag::get-component-templates-request-masterTimeout
// <1>
request.setMasterNodeTimeout(TimeValue.timeValueMinutes(1));
// <2>
request.setMasterNodeTimeout("1m");
// end::get-component-templates-request-masterTimeout
// tag::get-component-templates-execute
GetComponentTemplatesResponse getTemplatesResponse = client.cluster().getComponentTemplate(request, RequestOptions.DEFAULT);
// end::get-component-templates-execute
// tag::get-component-templates-response
// <1>
Map<String, ComponentTemplate> templates = getTemplatesResponse.getComponentTemplates();
// end::get-component-templates-response
assertThat(templates.size(), is(1));
assertThat(templates.get("ct1"), is(notNullValue()));
// tag::get-component-templates-execute-listener
ActionListener<GetComponentTemplatesResponse> listener = new ActionListener<GetComponentTemplatesResponse>() {
@Override
public void onResponse(GetComponentTemplatesResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-component-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-component-templates-execute-async
// <1>
client.cluster().getComponentTemplateAsync(request, RequestOptions.DEFAULT, listener);
// end::get-component-templates-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.indices.PutComponentTemplateRequest in project OpenSearch by opensearch-project.
the class ClusterClientDocumentationIT method testDeleteComponentTemplate.
public void testDeleteComponentTemplate() throws Exception {
RestHighLevelClient client = highLevelClient();
{
PutComponentTemplateRequest request = new PutComponentTemplateRequest().name("ct1");
Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build();
String mappingJson = "{\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + "}";
AliasMetadata twitterAlias = AliasMetadata.builder("twitter_alias").build();
Map<String, AliasMetadata> aliases = new HashMap<>();
aliases.put("twitter_alias", twitterAlias);
Template template = new Template(settings, new CompressedXContent(mappingJson), aliases);
request.componentTemplate(new ComponentTemplate(template, null, null));
assertTrue(client.cluster().putComponentTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
}
// tag::delete-component-template-request
// <1>
DeleteComponentTemplateRequest deleteRequest = new DeleteComponentTemplateRequest("ct1");
// end::delete-component-template-request
// tag::delete-component-template-request-masterTimeout
// <1>
deleteRequest.setMasterTimeout(TimeValue.timeValueMinutes(1));
// end::delete-component-template-request-masterTimeout
// tag::delete-component-template-execute
AcknowledgedResponse deleteTemplateAcknowledge = client.cluster().deleteComponentTemplate(deleteRequest, RequestOptions.DEFAULT);
// end::delete-component-template-execute
// tag::delete-component-template-response
// <1>
boolean acknowledged = deleteTemplateAcknowledge.isAcknowledged();
// end::delete-component-template-response
assertThat(acknowledged, equalTo(true));
{
PutComponentTemplateRequest request = new PutComponentTemplateRequest().name("ct1");
Settings settings = Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 1).build();
Template template = new Template(settings, null, null);
request.componentTemplate(new ComponentTemplate(template, null, null));
assertTrue(client.cluster().putComponentTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
}
// tag::delete-component-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-component-template-execute-listener
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::delete-component-template-execute-async
// <1>
client.cluster().deleteComponentTemplateAsync(deleteRequest, RequestOptions.DEFAULT, listener);
// end::delete-component-template-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.indices.PutComponentTemplateRequest in project OpenSearch by opensearch-project.
the class ClusterClientIT method testComponentTemplates.
public void testComponentTemplates() 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));
ComponentTemplate componentTemplate = new ComponentTemplate(template, 1L, new HashMap<>());
PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name(templateName).create(true).componentTemplate(componentTemplate);
AcknowledgedResponse response = execute(putComponentTemplateRequest, highLevelClient().cluster()::putComponentTemplate, highLevelClient().cluster()::putComponentTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
ComponentTemplatesExistRequest componentTemplatesExistRequest = new ComponentTemplatesExistRequest(templateName);
boolean exist = execute(componentTemplatesExistRequest, highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync);
assertTrue(exist);
GetComponentTemplatesRequest getComponentTemplatesRequest = new GetComponentTemplatesRequest(templateName);
GetComponentTemplatesResponse getResponse = execute(getComponentTemplatesRequest, highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync);
assertThat(getResponse.getComponentTemplates().size(), equalTo(1));
assertThat(getResponse.getComponentTemplates().containsKey(templateName), equalTo(true));
assertThat(getResponse.getComponentTemplates().get(templateName), equalTo(componentTemplate));
DeleteComponentTemplateRequest deleteComponentTemplateRequest = new DeleteComponentTemplateRequest(templateName);
response = execute(deleteComponentTemplateRequest, highLevelClient().cluster()::deleteComponentTemplate, highLevelClient().cluster()::deleteComponentTemplateAsync);
assertThat(response.isAcknowledged(), equalTo(true));
OpenSearchStatusException statusException = expectThrows(OpenSearchStatusException.class, () -> execute(getComponentTemplatesRequest, highLevelClient().cluster()::getComponentTemplate, highLevelClient().cluster()::getComponentTemplateAsync));
assertThat(statusException.status(), equalTo(RestStatus.NOT_FOUND));
exist = execute(componentTemplatesExistRequest, highLevelClient().cluster()::existsComponentTemplate, highLevelClient().cluster()::existsComponentTemplateAsync);
assertFalse(exist);
}
use of org.opensearch.client.indices.PutComponentTemplateRequest in project OpenSearch by opensearch-project.
the class ClusterRequestConverters method putComponentTemplate.
static Request putComponentTemplate(PutComponentTemplateRequest putComponentTemplateRequest) throws IOException {
String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_component_template").addPathPart(putComponentTemplateRequest.name()).build();
Request request = new Request(HttpPut.METHOD_NAME, endpoint);
RequestConverters.Params params = new RequestConverters.Params();
params.withMasterTimeout(putComponentTemplateRequest.masterNodeTimeout());
if (putComponentTemplateRequest.create()) {
params.putParam("create", Boolean.TRUE.toString());
}
if (Strings.hasText(putComponentTemplateRequest.cause())) {
params.putParam("cause", putComponentTemplateRequest.cause());
}
request.addParameters(params.asMap());
request.setEntity(RequestConverters.createEntity(putComponentTemplateRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
return request;
}
Aggregations