Routing

OpenSearch uses a hashing algorithm to route documents to specific shards in an index. By default, the document’s _id field is used as the routing value, but you can also specify a custom routing value for each document.

Default routing

The following is the default OpenSearch routing formula. The _routing value is the document’s _id.

  1. shard_num = hash(_routing) % num_primary_shards

Custom routing

You can specify a custom routing value when indexing a document, as shown in the following example request:

  1. PUT sample-index1/_doc/1?routing=JohnDoe1
  2. {
  3. "title": "This is a document"
  4. }

copy

In this example, the document is routed using the value JohnDoe1 instead of the default _id.

You must provide the same routing value when retrieving, deleting, or updating the document, as shown in the following example request:

  1. GET sample-index1/_doc/1?routing=JohnDoe1

copy

Querying by routing

You can query documents based on their routing value by using the _routing field, as shown in the following example. This query only searches the shard(s) associated with the JohnDoe1 routing value:

  1. GET sample-index1/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "_routing": [ "JohnDoe1" ]
  6. }
  7. }
  8. }

copy

Required routing

You can make custom routing required for all CRUD operations on an index, as shown in the following example request. If you try to index a document without providing a routing value, OpenSearch will throw an exception.

  1. PUT sample-index2
  2. {
  3. "mappings": {
  4. "_routing": {
  5. "required": true
  6. }
  7. }
  8. }

copy

Routing to specific shards

You can configure an index to route custom values to a subset of shards rather than a single shard. This is done by setting index.routing_partition_size at the time of index creation. The formula for calculating the shard is shard_num = (hash(_routing) + hash(_id)) % routing_partition_size) % num_primary_shards.

The following example request routes documents to one of four shards in the index:

  1. PUT sample-index3
  2. {
  3. "settings": {
  4. "index.routing_partition_size": 4
  5. },
  6. "mappings": {
  7. "_routing": {
  8. "required": true
  9. }
  10. }
  11. }

copy