Cluster Health APIedit

The Cluster Health API allows getting cluster health.

Cluster Health Requestedit

A 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.

Indicesedit

Indices 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 parametersedit

Other parameters can be passed only through setter methods:

request.timeout(TimeValue.timeValueSeconds(50)); 
request.timeout("50s"); 

Timeout for the request as a TimeValue. Defaults to 30 seconds

As a String

request.masterNodeTimeout(TimeValue.timeValueSeconds(20)); 
request.masterNodeTimeout("20s"); 

Timeout to connect to the master node as a TimeValue. Defaults to the same as timeout

As a String

request.waitForStatus(ClusterHealthStatus.YELLOW); 
request.waitForYellowStatus(); 

The status to wait (e.g. green, yellow, or red). Accepts a ClusterHealthStatus value.

Using predefined method

request.waitForEvents(Priority.NORMAL); 

The priority of the events to wait for. Accepts a Priority value.

request.level(ClusterHealthRequest.Level.SHARDS); 

The level of detail of the returned health information. Accepts a ClusterHealthRequest.Level value.

request.waitForNoRelocatingShards(true); 

Wait for 0 relocating shards. Defaults to false

request.waitForNoInitializingShards(true); 

Wait for 0 initializing shards. Defaults to false

request.waitForNodes("2"); 
request.waitForNodes(">=2"); 
request.waitForNodes("le(2)"); 

Wait for N nodes in the cluster. Defaults to 0

Using >=N, <=N, >N and <N notation

Using ge(N), le(N), gt(N), lt(N) notation

request.waitForActiveShards(ActiveShardCount.ALL); 
request.waitForActiveShards(1); 

Wait for all shards to be active in the cluster

Wait for N shards to be active in the cluster

request.local(true); 

Non-master node can be used for this request. Defaults to false

Synchronous Executionedit

ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);

Asynchronous Executionedit

The asynchronous execution of a cluster health request requires both the ClusterHealthRequest instance and an ActionListener instance to be passed to the asynchronous method:

client.cluster().healthAsync(request, RequestOptions.DEFAULT, listener); 

The ClusterHealthRequest to execute and the ActionListener to use when the execution completes

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.

A typical listener for ClusterHealthResponse looks like:

ActionListener<ClusterHealthResponse> listener =
    new ActionListener<ClusterHealthResponse>() {
        @Override
        public void onResponse(ClusterHealthResponse response) {
            
        }

        @Override
        public void onFailure(Exception e) {
            
        }
    };

Called when the execution is successfully completed. The response is provided as an argument

Called in case of a failure. The raised exception is provided as an argument

Cluster Health Responseedit

The returned ClusterHealthResponse contains the next information about the cluster:

String clusterName = response.getClusterName(); 
ClusterHealthStatus status = response.getStatus(); 

Name of the cluster

Cluster status (green, yellow or red)

boolean timedOut = response.isTimedOut(); 
RestStatus restStatus = response.status(); 

Whether request was timed out while processing

Status of the request (OK or REQUEST_TIMEOUT). Other errors will be thrown as exceptions

int numberOfNodes = response.getNumberOfNodes(); 
int numberOfDataNodes = response.getNumberOfDataNodes(); 

Number of nodes in the cluster

Number of data nodes in the cluster

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

Map<String, ClusterIndexHealth> indices = response.getIndices(); 

Detailed information about indices in the cluster

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();

Detailed information about a specific index

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();

Detailed information about a specific shard