WARNING: Version 2.0 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
_routing field
edit_routing
field
editA document is routed to a particular shard in an index using the following formula:
shard_num = hash(_routing) % num_primary_shards
The default value used for _routing
is the document’s _id
or the document’s _parent
ID, if present.
Custom routing patterns can be implemented by specifying a custom routing
value per document. For instance:
PUT my_index/my_type/1?routing=user1 { "title": "This is a document" } GET my_index/my_type/1?routing=user1
This document uses |
|
The the same |
The value of the _routing
field is accessible in queries, aggregations, scripts,
and when sorting:
GET my_index/_search { "query": { "terms": { "_routing": [ "user1" ] } }, "aggs": { "Routing values": { "terms": { "field": "_routing", "size": 10 } } }, "sort": [ { "_routing": { "order": "desc" } } ], "script_fields": { "Routing value": { "script": "doc['_routing']" } } }
Querying on the |
|
Aggregating on the |
|
Sorting on the |
|
Accessing the |
Searching with custom routing
editCustom 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):
Making a routing value required
editWhen 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
custom routing
value required for all CRUD operations:
Unique IDs with custom routing
editWhen 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
different _routing
values.
It is up to the user to ensure that IDs are unique across the index.