Cluster Administrationedit

To access cluster Java API, you need to call cluster() method from an AdminClient:

ClusterAdminClient clusterAdminClient = client.admin().cluster();
Note

In the rest of this guide, we will use client.admin().cluster().

Cluster Healthedit

Healthedit

The cluster health API allows to get a very simple status on the health of the cluster and also can give you some technical information about the cluster status per index:

ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get(); 
String clusterName = healths.getClusterName();              
int numberOfDataNodes = healths.getNumberOfDataNodes();     
int numberOfNodes = healths.getNumberOfNodes();             

for (ClusterIndexHealth health : healths.getIndices().values()) { 
    String index = health.getIndex();                       
    int numberOfShards = health.getNumberOfShards();        
    int numberOfReplicas = health.getNumberOfReplicas();    
    ClusterHealthStatus status = health.getStatus();        
}

Get information for all indices

Access the cluster name

Get the total number of data nodes

Get the total number of nodes

Iterate over all indices

Index name

Number of shards

Number of replicas

Index status

Wait for statusedit

You can use the cluster health API to wait for a specific status for the whole cluster or for a given index:

client.admin().cluster().prepareHealth()            
        .setWaitForYellowStatus()                   
        .get();
client.admin().cluster().prepareHealth("company")   
        .setWaitForGreenStatus()                    
        .get();

client.admin().cluster().prepareHealth("employee")  
        .setWaitForGreenStatus()                    
        .setTimeout(TimeValue.timeValueSeconds(2))  
        .get();

Prepare a health request

Wait for the cluster being yellow

Prepare the health request for index company

Wait for the index being green

Prepare the health request for index employee

Wait for the index being green

Wait at most for 2 seconds

If the index does not have the expected status and you want to fail in that case, you need to explicitly interpret the result:

ClusterHealthResponse response = client.admin().cluster().prepareHealth("company")
        .setWaitForGreenStatus()    
        .get();

ClusterHealthStatus status = response.getIndices().get("company").getStatus();
if (!status.equals(ClusterHealthStatus.GREEN)) {
    throw new RuntimeException("Index is in " + status + " state"); 
}

Wait for the index being green

Throw an exception if not GREEN

Stored Scripts APIedit

The stored script API allows one to interact with scripts and templates stored in Elasticsearch. It can be used to create, update, get, and delete stored scripts and templates.

PutStoredScriptResponse response = client.admin().cluster().preparePutStoredScript()
                .setId("script1")
                .setContent(new BytesArray("{\"script\": {\"lang\": \"painless\", \"source\": \"_score * doc['my_numeric_field'].value\"} }"), XContentType.JSON)
                .get();

GetStoredScriptResponse response = client().admin().cluster().prepareGetStoredScript()
                .setId("script1")
                .get();

DeleteStoredScriptResponse response = client().admin().cluster().prepareDeleteStoredScript()
                .setId("script1")
                .get();

To store templates simply use "mustache" for the scriptLang.

Script Languageedit

The put stored script API allows one to set the language of the stored script. If one is not provided the default scripting language will be used.