Allow routing allocations:
curl -XPUT localhost:9200/_cluster/settings -d '{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}'
Reallocate unassigned shards, allowing primary allocations:
curl -s localhost:9200/_cat/shards | grep UNASS | while read line ; do \
read -a fields <<<"$line" ;
curl -XPOST -d '{
"commands" : [
{
"allocate" : {
"index" : "'${fields[0]}'",
"shard" : '${fields[1]}',
"node" : "elasticsearch-'$(hostname)'",
"allow_primary": "true"
}
}
]
}' http://localhost:9200/_cluster/reroute?pretty ; done
A better solution in 5.x is to run the allocation explain API first to find out why the shard is not assigned.
https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-allocation-explain.html
Often times the problem is very simple to solve, even without data loss.