Index settings control how data is stored and copied in Elasticsearch. Shards split data for speed, replicas copy data for safety.
Index settings (shards, replicas) in Elasticsearch
Introduction
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
Elasticsearch
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}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
}
}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.
Practice
1. What is the main purpose of setting
number_of_shards in an Elasticsearch index?easy
Solution
Step 1: Understand shards role
Shards divide the index data into smaller parts to allow parallel searching.Step 2: Differentiate shards from replicas
Replicas are copies for safety, not for splitting data.Final Answer:
To split data into parts for faster searching -> Option CQuick Check:
Shards = split data for speed [OK]
Hint: Shards split data, replicas copy data [OK]
Common Mistakes:
- Confusing shards with replicas
- Thinking shards create backups
- Assuming shards set index size
2. Which of the following is the correct way to set 3 shards and 2 replicas in an Elasticsearch index creation JSON?
easy
Solution
Step 1: Recall correct setting keys
The official keys arenumber_of_shardsandnumber_of_replicas.Step 2: Match keys with options
Only { "settings": { "number_of_shards": 3, "number_of_replicas": 2 } } uses the exact correct keys and valid JSON format.Final Answer:
{ "settings": { "number_of_shards": 3, "number_of_replicas": 2 } } -> Option AQuick Check:
Correct keys = number_of_shards, number_of_replicas [OK]
Hint: Use full keys: number_of_shards and number_of_replicas [OK]
Common Mistakes:
- Using shorthand keys like shards or replicas
- Wrong JSON syntax
- Mixing shard and replica counts
3. Given an index with
number_of_shards set to 4 and number_of_replicas set to 1, how many total shard copies exist in the cluster?medium
Solution
Step 1: Calculate primary shards
There are 4 primary shards as set.Step 2: Calculate replica shards
Each primary shard has 1 replica, so 4 replicas total.Step 3: Add primary and replicas
Total shards = 4 primary + 4 replicas = 8 shards.Final Answer:
8 -> Option DQuick Check:
Total shards = primary + replicas = 8 [OK]
Hint: Total shards = shards x (1 + replicas) [OK]
Common Mistakes:
- Counting only primary shards
- Adding shards and replicas incorrectly
- Confusing replicas with shards
4. You tried to create an index with this setting:
{ "settings": { "number_of_shards": 2, "number_of_replicas": -1 } }. What is the problem?medium
Solution
Step 1: Check shards value
Number of shards is 2, which is valid.Step 2: Check replicas value
Replicas cannot be negative; -1 is invalid.Final Answer:
Negative replicas value is invalid -> Option AQuick Check:
Replicas must be zero or positive [OK]
Hint: Replicas cannot be negative numbers [OK]
Common Mistakes:
- Assuming negative replicas mean no replicas
- Thinking shards must be > 3
- Ignoring JSON correctness
5. You want to create an index optimized for high availability with 5 shards. Which setting combination is best to ensure data safety and fast search?
hard
Solution
Step 1: Understand shards and replicas roles
Shards split data for speed; replicas provide copies for safety.Step 2: Evaluate options for availability and speed
{ "number_of_shards": 5, "number_of_replicas": 1 } keeps 5 shards for speed and 1 replica for safety, balancing both.Step 3: Reject other options
The option with zero replicas has no safety, the option with 1 shard is slow for search, the option with 10 shards has overhead.Final Answer:
{ "number_of_shards": 5, "number_of_replicas": 1 } -> Option BQuick Check:
Balance shards and replicas for speed and safety [OK]
Hint: Balance shards for speed and replicas for safety [OK]
Common Mistakes:
- Setting zero replicas for safety
- Using too few shards for speed
- Over-sharding causing overhead
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
