FresherTechnicalElasticsearch
What are indices, documents, shards and replicas in Elasticsearch?
Try answering first
Model answer
- Document: a JSON object, the unit you index and search, such as a product or a log line.
- Index: a collection of documents with a similar structure and a mapping, loosely like a table.
- Shard: a slice of an index, and each shard is a self-contained Lucene index. An index is split into primary shards, and each document lives in exactly one of them, chosen by hashing its id (routing).
- Replica: a copy of a primary shard on another node. It gives failover and extra search capacity.
PUT articles
{ "settings": { "number_of_shards": 3, "number_of_replicas": 1 } }
This creates 3 primary shards and 3 replicas, 6 shards in total, spread across the nodes. A replica is never placed on the same node as its primary, so one node loss does not lose data.
The number of primary shards is fixed at creation (you change it with split, shrink or reindex), while the number of replicas can change at any time. More shards spread the data and parallel work, but each shard has overhead.
Primary shards split the data, and replicas copy it for safety and read capacity.
Key points
- A document is a JSON object stored in an index
- Primary shards split data, each document is in one
- Replicas copy primaries on other nodes
- Primary count is fixed, replica count is adjustable
Likely follow-ups
- How does Elasticsearch decide which shard holds a document?
- What is the cost of too many shards?
Common mistakes
- Thinking you can change the primary shard count in place
- Running with zero replicas in production