0
0
Elasticsearchquery~5 mins

Cluster health API in Elasticsearch

Choose your learning style9 modes available
Introduction

The Cluster Health API helps you check the overall health and status of your Elasticsearch cluster quickly.

You want to see if your Elasticsearch cluster is running well or has problems.
Before running heavy searches, you want to confirm the cluster is healthy.
You want to monitor cluster status automatically in your app or script.
You want to check if all nodes and shards are working properly.
You want to get a quick summary of cluster performance and availability.
Syntax
Elasticsearch
GET /_cluster/health

# Optional parameters:
GET /_cluster/health?level=cluster|indices|shards
GET /_cluster/health?wait_for_status=green|yellow|red&timeout=30s

Use GET /_cluster/health to get the overall cluster health.

You can add parameters like level to get more details or wait_for_status to wait until the cluster reaches a certain health.

Examples
Get the overall health status of the cluster.
Elasticsearch
GET /_cluster/health
Get health status for each index in the cluster.
Elasticsearch
GET /_cluster/health?level=indices
Wait up to 30 seconds for the cluster to become green (fully healthy).
Elasticsearch
GET /_cluster/health?wait_for_status=green&timeout=30s
Sample Program

This Python program sends a request to the Cluster Health API and prints the cluster health details as JSON.

Elasticsearch
import requests

# URL of your Elasticsearch cluster
url = 'http://localhost:9200/_cluster/health'

# Send GET request to Cluster Health API
response = requests.get(url)

# Print the JSON response
print(response.json())
OutputSuccess
Important Notes

The cluster health status can be green (all good), yellow (some replicas not assigned), or red (some data missing).

Use the wait_for_status parameter to pause your request until the cluster reaches a desired health.

Checking cluster health regularly helps avoid surprises in your Elasticsearch setup.

Summary

The Cluster Health API shows the current health of your Elasticsearch cluster.

You can get overall or detailed health info using parameters.

It helps you monitor and keep your cluster running smoothly.