Elasticsearch & Lucene (50 Qs)•Part 2: Core Architecture & Implementation (Mid-Level)
How does Elasticsearch route a document to a specific shard?
senior LevelSystem Design
In Simple Words: When indexing a document, Elasticsearch determines the target primary shard using a hash formula: $$shard = \text{hash}(routing) \pmod{\text{number_of_primary_shards}}$$
- By default,
routingis the document's_id. - Custom Routing: You can pass
?routing=tenant_123to force all documents belonging to a specific customer to live on the same shard, dramatically speeding up tenant-specific queries.