A document is routed to a particular shard in an index using the following
shard_num = hash(_routing) % num_primary_shards
The default value used for
_routing is the document’s
or the document’s
_parent ID, if present.
Custom routing patterns can be implemented by specifying a custom
value per document. For instance:
"title": "This is a document"
This document uses
user1 as its routing value, instead of its ID.
routing value needs to be provided when
getting, deleting, or updating
The value of the
_routing field is accessible in queries:
"_routing": [ "user1" ]
Querying on the
_routing field (also see the
Searching with custom routingedit
Custom routing can reduce the impact of searches. Instead of having to fan
out a search request to all the shards in an index, the request can be sent to
just the shard that matches the specific routing value (or values):
This search request will only be executed on the shards associated with the
user2 routing values.
Making a routing value requirededit
When using custom routing, it is important to provide the routing value
whenever indexing, getting,
deleting, or updating a document.
Forgetting the routing value can lead to a document being indexed on more than
one shard. As a safeguard, the
_routing field can be configured to make a
routing value required for all CRUD operations:
"text": "No routing value provided"
Routing is required for
This index request throws a
Unique IDs with custom routingedit
When indexing documents specifying a custom
_routing, the uniqueness of the
_id is not guaranteed across all of the shards in the index. In fact,
documents with the same
_id might end up on different shards if indexed with
It is up to the user to ensure that IDs are unique across the index.