Last Updated: February 25, 2016
·
1.312K
· muhqu

Change number_of_replicas for all Indexes

why?

For example you're running a 2 node cluster with a number_of_replicas of 1. When you like to replace one of the instances, by just adding a 3rd instance and later on droping the old one, you will very likely end up moving a lot of data.

To solve the issue you just need to increase the number_of_replicas before your new instance joins the cluster. That way all the shards are just being synced to the new instance, but every shard will also remain on the old instances. When the new instance is full in sync, you want to retire the old instance and reduce the number_of_replicas back to what it was.

requirements

  • curl + xargs: you should already have them... ;-)
  • [jq][]: json commandline processing util

one-liner

curl -sg 'http://your-elastic-search:9200/*/_settings' \
  | jq -r 'to_entries
     | .[]
     | "curl -sg -XPUT http://your-elastic-search:9200/"
        + .key + "/_settings -d " + (
       {index:{number_of_replicas:(
         .value.settings.index.number_of_replicas 
           | tonumber | . +1 | tostring
       )}}|tojson|@sh
      )' \
  | bash

NOTE: There is an issue with coderwalls code-block renderer, <a href="/sh">@sh</a> should be @sh.
[jq]: https://stedolan.github.io/jq/

To decrease just change the jq script . +1 to . -1