0
0
Elasticsearchquery~5 mins

Index settings (shards, replicas) in Elasticsearch

Choose your learning style9 modes available
Introduction

Index settings control how data is stored and copied in Elasticsearch. Shards split data for speed, replicas copy data for safety.

When you want to make your search faster by splitting data into parts.
When you want to keep extra copies of your data to avoid losing it.
When you need to balance storage and performance on your Elasticsearch cluster.
When setting up a new index and deciding how many pieces and copies it should have.
When you want to improve search availability during server failures.
Syntax
Elasticsearch
{
  "settings": {
    "number_of_shards": <number>,
    "number_of_replicas": <number>
  }
}

number_of_shards is how many parts your data is split into.

number_of_replicas is how many copies of each shard exist besides the original.

Examples
This creates an index with 3 shards and 1 replica for each shard.
Elasticsearch
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  }
}
This creates an index with 1 shard and no replicas, meaning no extra copies.
Elasticsearch
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  }
}
Sample Program

This command creates an index named my_sample_index with 2 shards and 1 replica each. This means data is split into 2 parts, and each part has one extra copy for safety.

Elasticsearch
PUT /my_sample_index
{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 1
  }
}
OutputSuccess
Important Notes

You cannot change the number of shards after creating the index.

You can change the number of replicas anytime to add or remove copies.

Summary

Shards split your data to make searching faster.

Replicas keep extra copies to protect your data.

Set shards and replicas when creating an index to balance speed and safety.