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