A primary shard is the authoritative storage unit for part of an Elasticsearch index. It holds documents; replica shards keep copies for redundancy and search capacity. An index can distribute its primary shards across one or more cluster nodes. section 3.2.2
Each document has one permanent primary-shard home. Elasticsearch hashes the document ID, applies modulo against the index’s number of primary shards, and uses the resulting shard number for indexing and retrieval. section 3.5
Creating a chats index
PUT chats { "settings": { "number_of_shards": 1, "number_of_replicas": 1 } } This creates one primary shard and one replica. On a single-node cluster, Elasticsearch does not place the replica on the same node as the primary, so the replica remains unassigned until another suitable node is available. section 14.1.1
Plan primary shards before creating an index:
- Choose the number of primary shards for expected current and future data size; the setting cannot be changed on a live index. section 3.2.2 section 3.5
- If growth exhausts the shards, create a new index with appropriate settings and reindex the existing data. section 3.5
- Increase replicas when read throughput needs to rise, but budget memory because every replica copies its primary shard. section 14.1.3 section 14.3.1
shard_number = hash(id) % number_of_shards
Use the routing formula to see why a document belongs to exactly one primary shard and why changing the shard count disrupts lookup. section 3.5
