3
votes

In ElasticSearch 2.X you can move shards around using reroute:

https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-reroute.html#cluster-reroute

Other than specifying a move for each shard, is there an easy way to move all off them from one node to another?

2

2 Answers

4
votes

Rerouting will not ensure the shards remain in the new node. Elasticsearch may try to balance things out and move some shards back to the original node. If you want to pin the shards to the new node, you need to use Shard Allocation Filtering.

If you want to move all shards of index index1 to node node1, then the command to execute is:

PUT index1/_settings
{
  "index.routing.allocation.include._name": "node1"
}
3
votes

I've not found a move "all" option yet, but thanks to awk and the ES _cat API, I can generate the needed JSON for moving all shards from node1 to node2:

#!/bin/bash

OLD=node1
NEW=node2

SHARDS=`
curl -s -XGET "https://my.elasticsearch.cluster/_cat/shards/" | awk -v NEW=$NEW -v OLD=$OLD '$8==OLD {
  print "      { \"move\": { \"index\": \""$1"\", \"shard\": "$2", \"from_node\": \""OLD"\", \"to_node\": \""NEW"\" }},"
}'|sed '$ s/.$//'
`
echo '{"commands": ['
echo $SHARDS
echo ']}'