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
Just remember
"allow_primary": true
means "I'm ok with losing all the data that might have been in that shard". This script will lose data. If you are stuck and need to get unstuck then this'll do the job though. The shards will come back online, but you'll lose all the data that was in them.