NOTE: You are looking at documentation for an older release. For the latest information, see the current release documentation.
Cluster Health API
editCluster Health API
editThe Cluster Health API allows getting cluster health.
Cluster Health Request
editA ClusterHealthRequest
:
ClusterHealthRequest request = new ClusterHealthRequest();
There are no required parameters. By default, the client will check all indices and will not wait for any events.
Indices
editIndices which should be checked can be passed in the constructor:
ClusterHealthRequest request = new ClusterHealthRequest("index1", "index2");
Or using the corresponding setter method:
ClusterHealthRequest request = new ClusterHealthRequest(); request.indices("index1", "index2");
Other parameters
editOther parameters can be passed only through setter methods:
The status to wait (e.g. |
|
Using predefined method |
Wait for |
|
Using |
|
Using |
Synchronous Execution
editWhen executing a ClusterHealthRequest
in the following manner, the client waits
for the ClusterHealthResponse
to be returned before continuing with code execution:
ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
Synchronous calls may throw an IOException
in case of either failing to
parse the REST response in the high-level REST client, the request times out
or similar cases where there is no response coming back from the server.
In cases where the server returns a 4xx
or 5xx
error code, the high-level
client tries to parse the response body error details instead and then throws
a generic ElasticsearchException
and adds the original ResponseException
as a
suppressed exception to it.
Asynchronous Execution
editExecuting a ClusterHealthRequest
can also be done in an asynchronous fashion so that
the client can return directly. Users need to specify how the response or
potential failures will be handled by passing the request and a listener to the
asynchronous health method:
The asynchronous method does not block and returns immediately. Once it is
completed the ActionListener
is called back using the onResponse
method
if the execution successfully completed or using the onFailure
method if
it failed. Failure scenarios and expected exceptions are the same as in the
synchronous execution case.
A typical listener for health
looks like:
Cluster Health Response
editThe returned ClusterHealthResponse
contains the next information about the
cluster:
Whether request was timed out while processing |
|
Status of the request ( |
int numberOfNodes = response.getNumberOfNodes(); int numberOfDataNodes = response.getNumberOfDataNodes();
int activeShards = response.getActiveShards(); int activePrimaryShards = response.getActivePrimaryShards(); int relocatingShards = response.getRelocatingShards(); int initializingShards = response.getInitializingShards(); int unassignedShards = response.getUnassignedShards(); int delayedUnassignedShards = response.getDelayedUnassignedShards(); double activeShardsPercent = response.getActiveShardsPercent();
Number of active shards |
|
Number of primary active shards |
|
Number of relocating shards |
|
Number of initializing shards |
|
Number of unassigned shards |
|
Number of unassigned shards that are currently being delayed |
|
Percent of active shards |
TimeValue taskMaxWaitingTime = response.getTaskMaxWaitingTime(); int numberOfPendingTasks = response.getNumberOfPendingTasks(); int numberOfInFlightFetch = response.getNumberOfInFlightFetch();
Maximum wait time of all tasks in the queue |
|
Number of currently pending tasks |
|
Number of async fetches that are currently ongoing |
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();
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();