9951 explained code solutions for 126 technologies


elasticsearchHow can I resolve unassigned shards in Elasticsearch?


Unassigned shards in Elasticsearch can be resolved by performing a cluster re-routing operation. This will cause the cluster to re-allocate the shards to the available nodes. To do this, use the Cluster Reroute API with the allocate_stale_primary option. This will cause the cluster to try to re-allocate any unassigned shards to their primary node:

curl -XPOST 'localhost:9200/_cluster/reroute?retry_failed&pretty' -H 'Content-Type: application/json' -d'
{
  "commands" : [
    {
      "allocate_stale_primary" : {
        "index" : "my_index",
        "shard" : 0,
        "node" : "node_name",
        "accept_data_loss" : true
      }
    }
  ]
}
'

The output of this command will be something like this:

{
  "state" : "yellow",
  "allocated_shards" : 3,
  "unassigned_shards" : 0
}

This indicates that the cluster re-routing operation was successful and all shards have been assigned to nodes.

Parts of the code:

  • curl: command used to make a request to the Elasticsearch API
  • -XPOST: flag used to specify the type of request being made (in this case, a POST request)
  • localhost:9200: address of the Elasticsearch cluster
  • _cluster/reroute: endpoint of the Cluster Reroute API
  • retry_failed: flag used to retry failed re-routing operations
  • allocate_stale_primary: command used to re-allocate unassigned shards
  • index: name of the index containing the unassigned shards
  • shard: number of the shard to be re-allocated
  • node: name of the node to which the shard should be re-allocated
  • accept_data_loss: flag used to indicate that data loss is acceptable

Helpful links

Edit this code on GitHub