Updating the default index number_of_replicas setting for new indices

Joe Taylor picture Joe Taylor · Jul 3, 2014 · Viewed 15.4k times · Source

I've tried updating the number of replicas as follows, according to the documentation

curl -XPUT 'localhost:9200/_settings' -d '
{ "index" : { "number_of_replicas" : 4 } }'

This correctly changes the replica count for existing nodes. However, when logstash creates a new index the following day, number_of_replicas is set to the old value.

Is there a way to permanently change the default value for this setting without updating all the elasticsearch.yml files in the cluster and restarting the services?

FWIW I've also tried

curl -XPUT 'localhost:9200/logstash-*/_settings' -d '
{ "index" : { "number_of_replicas" : 4 } }'

to no avail.

Answer

John Petrone picture John Petrone · Jul 3, 2014

Yes, you can use index templates. Index templates are a great way to set default settings (including mappings) for new indices created in a cluster.

Index Templates

Index templates allow to define templates that will automatically be applied to new indices created. The templates include both settings and mappings, and a simple pattern template that controls if the template will be applied to the index created.

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-templates.html

For your example:

curl -XPUT 'localhost:9200/_template/logstash_template' -d ' 
{ 
  "template" : "logstash-*", 
  "settings" : {"number_of_replicas" : 4 }
} '

This will set the default number of replicas to 4 for all new indexes that match the name "logstash-*". Note that this will not change existing indexes, only newly created ones.