use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.
the class ClusterClientDocumentationIT method testClusterHealth.
@SuppressWarnings("unused")
public void testClusterHealth() throws IOException {
RestHighLevelClient client = highLevelClient();
client.indices().create(new CreateIndexRequest("index"), RequestOptions.DEFAULT);
{
// tag::health-request
ClusterHealthRequest request = new ClusterHealthRequest();
// end::health-request
}
{
// tag::health-request-indices-ctr
ClusterHealthRequest request = new ClusterHealthRequest("index1", "index2");
// end::health-request-indices-ctr
}
{
// tag::health-request-indices-setter
ClusterHealthRequest request = new ClusterHealthRequest();
request.indices("index1", "index2");
// end::health-request-indices-setter
}
ClusterHealthRequest request = new ClusterHealthRequest();
// tag::health-request-timeout
// <1>
request.timeout(TimeValue.timeValueSeconds(50));
// <2>
request.timeout("50s");
// end::health-request-timeout
// tag::health-request-master-timeout
// <1>
request.masterNodeTimeout(TimeValue.timeValueSeconds(20));
// <2>
request.masterNodeTimeout("20s");
// end::health-request-master-timeout
// tag::health-request-wait-status
// <1>
request.waitForStatus(ClusterHealthStatus.YELLOW);
// <2>
request.waitForYellowStatus();
// end::health-request-wait-status
// tag::health-request-wait-events
// <1>
request.waitForEvents(Priority.NORMAL);
// end::health-request-wait-events
// tag::health-request-level
// <1>
request.level(ClusterHealthRequest.Level.SHARDS);
// end::health-request-level
// tag::health-request-wait-relocation
// <1>
request.waitForNoRelocatingShards(true);
// end::health-request-wait-relocation
// tag::health-request-wait-initializing
// <1>
request.waitForNoInitializingShards(true);
// end::health-request-wait-initializing
// tag::health-request-wait-nodes
// <1>
request.waitForNodes("2");
// <2>
request.waitForNodes(">=2");
// <3>
request.waitForNodes("le(2)");
// end::health-request-wait-nodes
// tag::health-request-wait-active
// <1>
request.waitForActiveShards(ActiveShardCount.ALL);
// <2>
request.waitForActiveShards(1);
// end::health-request-wait-active
// tag::health-request-local
// <1>
request.local(true);
// end::health-request-local
// tag::health-execute
ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
// end::health-execute
assertThat(response.isTimedOut(), equalTo(false));
assertThat(response.status(), equalTo(RestStatus.OK));
assertThat(response.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
assertThat(response, notNullValue());
// tag::health-response-general
// <1>
String clusterName = response.getClusterName();
// <2>
ClusterHealthStatus status = response.getStatus();
// end::health-response-general
// tag::health-response-request-status
// <1>
boolean timedOut = response.isTimedOut();
// <2>
RestStatus restStatus = response.status();
// end::health-response-request-status
// tag::health-response-nodes
// <1>
int numberOfNodes = response.getNumberOfNodes();
// <2>
int numberOfDataNodes = response.getNumberOfDataNodes();
// end::health-response-nodes
{
// tag::health-response-shards
// <1>
int activeShards = response.getActiveShards();
// <2>
int activePrimaryShards = response.getActivePrimaryShards();
// <3>
int relocatingShards = response.getRelocatingShards();
// <4>
int initializingShards = response.getInitializingShards();
// <5>
int unassignedShards = response.getUnassignedShards();
// <6>
int delayedUnassignedShards = response.getDelayedUnassignedShards();
// <7>
double activeShardsPercent = response.getActiveShardsPercent();
// end::health-response-shards
}
// tag::health-response-task
// <1>
TimeValue taskMaxWaitingTime = response.getTaskMaxWaitingTime();
// <2>
int numberOfPendingTasks = response.getNumberOfPendingTasks();
// <3>
int numberOfInFlightFetch = response.getNumberOfInFlightFetch();
// end::health-response-task
// tag::health-response-indices
// <1>
Map<String, ClusterIndexHealth> indices = response.getIndices();
// end::health-response-indices
{
// tag::health-response-index
// <1>
ClusterIndexHealth index = indices.get("index");
ClusterHealthStatus indexStatus = index.getStatus();
int numberOfShards = index.getNumberOfShards();
int numberOfReplicas = index.getNumberOfReplicas();
int activeShards = index.getActiveShards();
int activePrimaryShards = index.getActivePrimaryShards();
int initializingShards = index.getInitializingShards();
int relocatingShards = index.getRelocatingShards();
int unassignedShards = index.getUnassignedShards();
// end::health-response-index
// tag::health-response-shard-details
// <1>
Map<Integer, ClusterShardHealth> shards = index.getShards();
ClusterShardHealth shardHealth = shards.get(0);
int shardId = shardHealth.getShardId();
ClusterHealthStatus shardStatus = shardHealth.getStatus();
int active = shardHealth.getActiveShards();
int initializing = shardHealth.getInitializingShards();
int unassigned = shardHealth.getUnassignedShards();
int relocating = shardHealth.getRelocatingShards();
boolean primaryActive = shardHealth.isPrimaryActive();
// end::health-response-shard-details
}
}
use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testValidateQuery.
@SuppressWarnings("unused")
public void testValidateQuery() throws IOException, InterruptedException {
RestHighLevelClient client = highLevelClient();
String index = "some_index";
createIndex(index, Settings.EMPTY);
// tag::indices-validate-query-request
// <1>
ValidateQueryRequest request = new ValidateQueryRequest(index);
// end::indices-validate-query-request
// tag::indices-validate-query-request-query
QueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.queryStringQuery("*:*")).filter(QueryBuilders.termQuery("user", "foobar"));
// <2>
request.query(builder);
// end::indices-validate-query-request-query
// tag::indices-validate-query-request-explain
// <1>
request.explain(true);
// end::indices-validate-query-request-explain
// tag::indices-validate-query-request-allShards
// <1>
request.allShards(true);
// end::indices-validate-query-request-allShards
// tag::indices-validate-query-request-rewrite
// <1>
request.rewrite(true);
// end::indices-validate-query-request-rewrite
// tag::indices-validate-query-execute
// <1>
ValidateQueryResponse response = client.indices().validateQuery(request, RequestOptions.DEFAULT);
// end::indices-validate-query-execute
// tag::indices-validate-query-response
// <1>
boolean isValid = response.isValid();
// <2>
int totalShards = response.getTotalShards();
// <3>
int successfulShards = response.getSuccessfulShards();
// <4>
int failedShards = response.getFailedShards();
if (failedShards > 0) {
for (DefaultShardOperationFailedException failure : response.getShardFailures()) {
// <5>
// <6>
String failedIndex = failure.index();
// <7>
int shardId = failure.shardId();
// <8>
String reason = failure.reason();
}
}
for (QueryExplanation explanation : response.getQueryExplanation()) {
// <9>
// <10>
String explanationIndex = explanation.getIndex();
// <11>
int shardId = explanation.getShard();
// <12>
String explanationString = explanation.getExplanation();
}
// end::indices-validate-query-response
// tag::indices-validate-query-execute-listener
ActionListener<ValidateQueryResponse> listener = new ActionListener<ValidateQueryResponse>() {
@Override
public void onResponse(ValidateQueryResponse validateQueryResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::indices-validate-query-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::indices-validate-query-execute-async
// <1>
client.indices().validateQueryAsync(request, RequestOptions.DEFAULT, listener);
// end::indices-validate-query-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testSimulateIndexTemplate.
public void testSimulateIndexTemplate() throws Exception {
RestHighLevelClient client = highLevelClient();
{
// <1>
PutComposableIndexTemplateRequest request = new PutComposableIndexTemplateRequest().name("my-template");
Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null);
ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(Arrays.asList("pattern-1", "log-*"), template, null, null, null, null);
request.indexTemplate(composableIndexTemplate);
assertTrue(client.indices().putIndexTemplate(request, RequestOptions.DEFAULT).isAcknowledged());
}
// tag::simulate-index-template-request
// <1>
SimulateIndexTemplateRequest simulateRequest = new SimulateIndexTemplateRequest("log-000001");
PutComposableIndexTemplateRequest newIndexTemplateRequest = new PutComposableIndexTemplateRequest().name("used-for-simulation");
Settings settings = Settings.builder().put("index.number_of_shards", 6).build();
// <2>
Template template = new Template(settings, null, null);
ComposableIndexTemplate composableIndexTemplate = new ComposableIndexTemplate(Arrays.asList("log-*"), template, null, 90L, null, null);
newIndexTemplateRequest.indexTemplate(composableIndexTemplate);
// <2>
simulateRequest.indexTemplateV2Request(newIndexTemplateRequest);
// end::simulate-index-template-request
// tag::simulate-index-template-response
SimulateIndexTemplateResponse simulateIndexTemplateResponse = client.indices().simulateIndexTemplate(simulateRequest, RequestOptions.DEFAULT);
// <1>
assertThat(simulateIndexTemplateResponse.resolvedTemplate().settings().get("index.number_of_shards"), is("6"));
assertThat(simulateIndexTemplateResponse.overlappingTemplates().get("my-template"), // <2>
containsInAnyOrder("pattern-1", "log-*"));
// end::simulate-index-template-response
// tag::simulate-index-template-execute-listener
ActionListener<SimulateIndexTemplateResponse> listener = new ActionListener<SimulateIndexTemplateResponse>() {
@Override
public void onResponse(SimulateIndexTemplateResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::simulate-index-template-execute-listener
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::simulate-index-template-execute-async
// <1>
client.indices().simulateIndexTemplateAsync(simulateRequest, RequestOptions.DEFAULT, listener);
// end::simulate-index-template-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testRolloverIndex.
public void testRolloverIndex() throws Exception {
RestHighLevelClient client = highLevelClient();
{
client.indices().create(new CreateIndexRequest("index-1").alias(new Alias("alias")), RequestOptions.DEFAULT);
}
// tag::rollover-index-request
// <1>
RolloverRequest request = new RolloverRequest("alias", "index-2");
// <2>
request.addMaxIndexAgeCondition(new TimeValue(7, TimeUnit.DAYS));
// <3>
request.addMaxIndexDocsCondition(1000);
// <4>
request.addMaxIndexSizeCondition(new ByteSizeValue(5, ByteSizeUnit.GB));
// end::rollover-index-request
// tag::rollover-index-request-timeout
// <1>
request.setTimeout(TimeValue.timeValueMinutes(2));
// end::rollover-index-request-timeout
// tag::rollover-index-request-masterTimeout
// <1>
request.setMasterTimeout(TimeValue.timeValueMinutes(1));
// end::rollover-index-request-masterTimeout
// tag::rollover-index-request-dryRun
// <1>
request.dryRun(true);
// end::rollover-index-request-dryRun
// tag::rollover-index-request-waitForActiveShards
// <1>
request.getCreateIndexRequest().waitForActiveShards(ActiveShardCount.from(2));
// <2>
request.getCreateIndexRequest().waitForActiveShards(ActiveShardCount.DEFAULT);
// end::rollover-index-request-waitForActiveShards
// tag::rollover-index-request-settings
request.getCreateIndexRequest().settings(Settings.builder().put("index.number_of_shards", // <1>
4));
// end::rollover-index-request-settings
// tag::rollover-index-request-mapping
String mappings = "{\"properties\":{\"field-1\":{\"type\":\"keyword\"}}}";
// <1>
request.getCreateIndexRequest().mapping(mappings, XContentType.JSON);
// end::rollover-index-request-mapping
// tag::rollover-index-request-alias
// <1>
request.getCreateIndexRequest().alias(new Alias("another_alias"));
// end::rollover-index-request-alias
// tag::rollover-index-execute
RolloverResponse rolloverResponse = client.indices().rollover(request, RequestOptions.DEFAULT);
// end::rollover-index-execute
// tag::rollover-index-response
// <1>
boolean acknowledged = rolloverResponse.isAcknowledged();
// <2>
boolean shardsAcked = rolloverResponse.isShardsAcknowledged();
// <3>
String oldIndex = rolloverResponse.getOldIndex();
// <4>
String newIndex = rolloverResponse.getNewIndex();
// <5>
boolean isRolledOver = rolloverResponse.isRolledOver();
// <6>
boolean isDryRun = rolloverResponse.isDryRun();
// <7>
Map<String, Boolean> conditionStatus = rolloverResponse.getConditionStatus();
// end::rollover-index-response
assertFalse(acknowledged);
assertFalse(shardsAcked);
assertEquals("index-1", oldIndex);
assertEquals("index-2", newIndex);
assertFalse(isRolledOver);
assertTrue(isDryRun);
assertEquals(3, conditionStatus.size());
// tag::rollover-index-execute-listener
ActionListener<RolloverResponse> listener = new ActionListener<RolloverResponse>() {
@Override
public void onResponse(RolloverResponse rolloverResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::rollover-index-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::rollover-index-execute-async
// <1>
client.indices().rolloverAsync(request, RequestOptions.DEFAULT, listener);
// end::rollover-index-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testGetFieldMapping.
@SuppressWarnings("unused")
public void testGetFieldMapping() throws IOException, InterruptedException {
RestHighLevelClient client = highLevelClient();
{
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("twitter"), RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
PutMappingRequest request = new PutMappingRequest("twitter");
request.source("{\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"timestamp\": {\n" + " \"type\": \"date\"\n" + " }\n" + " }\n" + // <1>
"}", XContentType.JSON);
AcknowledgedResponse putMappingResponse = client.indices().putMapping(request, RequestOptions.DEFAULT);
assertTrue(putMappingResponse.isAcknowledged());
}
// tag::get-field-mappings-request
// <1>
GetFieldMappingsRequest request = new GetFieldMappingsRequest();
// <2>
request.indices("twitter");
// <3>
request.fields("message", "timestamp");
// end::get-field-mappings-request
// tag::get-field-mappings-request-indicesOptions
// <1>
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// end::get-field-mappings-request-indicesOptions
// tag::get-field-mappings-request-local
// <1>
request.local(true);
// end::get-field-mappings-request-local
{
// tag::get-field-mappings-execute
GetFieldMappingsResponse response = client.indices().getFieldMapping(request, RequestOptions.DEFAULT);
// end::get-field-mappings-execute
// tag::get-field-mappings-response
final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = // <1>
response.mappings();
final Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMappings = // <2>
mappings.get("twitter");
final GetFieldMappingsResponse.FieldMappingMetadata metadata = // <3>
fieldMappings.get("message");
// <4>
final String fullName = metadata.fullName();
// <5>
final Map<String, Object> source = metadata.sourceAsMap();
// end::get-field-mappings-response
}
{
// tag::get-field-mappings-execute-listener
ActionListener<GetFieldMappingsResponse> listener = new ActionListener<GetFieldMappingsResponse>() {
@Override
public void onResponse(GetFieldMappingsResponse putMappingResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-field-mappings-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
final ActionListener<GetFieldMappingsResponse> latchListener = new LatchedActionListener<>(listener, latch);
listener = ActionListener.wrap(r -> {
final Map<String, Map<String, GetFieldMappingsResponse.FieldMappingMetadata>> mappings = r.mappings();
final Map<String, GetFieldMappingsResponse.FieldMappingMetadata> fieldMappings = mappings.get("twitter");
final GetFieldMappingsResponse.FieldMappingMetadata metadata1 = fieldMappings.get("message");
final String fullName = metadata1.fullName();
final Map<String, Object> source = metadata1.sourceAsMap();
latchListener.onResponse(r);
}, e -> {
latchListener.onFailure(e);
fail("should not fail");
});
// tag::get-field-mappings-execute-async
// <1>
client.indices().getFieldMappingAsync(request, RequestOptions.DEFAULT, listener);
// end::get-field-mappings-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
}
Aggregations