Home > Net >  set 3 shards by default when creating a new index in elasticsearch
set 3 shards by default when creating a new index in elasticsearch

Time:01-26

Whenever a new index is created I want to create it using 3 shards and 1 replica by default, but not providing the details in curl as below.

curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "index": {
      "number_of_shards": 3,  
      "number_of_replicas": 1
    }
  }
}
'

What property can I set in elasticsearch.yml so that any new index creation takes 3 physical shards and 1 replica?

So,

curl -X PUT "localhost:9200/my-index-000001?pretty"

Should create index with 3 shards and 1 replica. I know that by default it creates 1 shard and 1 replica.

Doc: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html

CodePudding user response:

The easiest way is to create an index template that matches all indexes, like this:

PUT _index_template/all_indexes
{
  "index_patterns": ["*"],
  "template": {
    "settings": {
      "number_of_shards": 3,
      "number_of_replicas": 1
    },
    "mappings": {},
    "aliases": {}
  },
  "priority": 1,
  "composed_of": []
}

Since all indexes will match *, they will all get created with the desired settings, which you can also override with more specific index templates, if necessary.

CodePudding user response:

I found one more way. This only works on version prior to 5.X We can add the property in elasticsearch.yml file

index.number_of_shards: 3
index.number_of_replicas: 1
  •  Tags:  
  • Related