How to Fix Unassigned Shards in Elasticsearch Quickly
_cluster/allocation/explain API to find the cause, then resolve it by restarting nodes, increasing disk space, or adjusting shard allocation settings with _cluster/reroute.Why This Happens
Unassigned shards occur when Elasticsearch cannot place a shard on any node. This can happen if a node crashed, disk space is low, or shard allocation rules prevent placement. For example, if a node is down, its shards become unassigned until the cluster recovers or reallocates them.
GET /_cat/shards?v # Shows shards with state UNASSIGNED GET /_cluster/allocation/explain # Explains why a shard is unassigned
The Fix
First, check the reason for unassigned shards using _cluster/allocation/explain. Then fix the root cause: restart failed nodes, free disk space, or update shard allocation settings. Finally, use POST /_cluster/reroute to manually allocate shards if needed.
POST /_cluster/reroute
{
"commands": [
{
"allocate_empty_primary": {
"index": "my_index",
"shard": 0,
"node": "node-1",
"accept_data_loss": true
}
}
]
}Prevention
To avoid unassigned shards, monitor cluster health regularly and ensure nodes have enough disk space and memory. Use shard allocation awareness settings to distribute shards evenly. Also, configure cluster.routing.allocation settings to control shard placement and avoid single points of failure.
Related Errors
Similar issues include yellow cluster status caused by missing replicas and cluster red status when primary shards are unassigned. Fixes often involve adding nodes, increasing resources, or adjusting shard allocation settings.