routing-field.asciidoc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. [[mapping-routing-field]]
  2. === `_routing` field
  3. A document is routed to a particular shard in an index using the following
  4. formula:
  5. shard_num = hash(_routing) % num_primary_shards
  6. The default value used for `_routing` is the document's <<mapping-id-field,`_id`>>.
  7. Custom routing patterns can be implemented by specifying a custom `routing`
  8. value per document. For instance:
  9. [source,console]
  10. ------------------------------
  11. PUT my_index/_doc/1?routing=user1&refresh=true <1>
  12. {
  13. "title": "This is a document"
  14. }
  15. GET my_index/_doc/1?routing=user1 <2>
  16. ------------------------------
  17. // TESTSETUP
  18. <1> This document uses `user1` as its routing value, instead of its ID.
  19. <2> The same `routing` value needs to be provided when
  20. <<docs-get,getting>>, <<docs-delete,deleting>>, or <<docs-update,updating>>
  21. the document.
  22. The value of the `_routing` field is accessible in queries:
  23. [source,console]
  24. --------------------------
  25. GET my_index/_search
  26. {
  27. "query": {
  28. "terms": {
  29. "_routing": [ "user1" ] <1>
  30. }
  31. }
  32. }
  33. --------------------------
  34. <1> Querying on the `_routing` field (also see the <<query-dsl-ids-query,`ids` query>>)
  35. ==== Searching with custom routing
  36. Custom routing can reduce the impact of searches. Instead of having to fan
  37. out a search request to all the shards in an index, the request can be sent to
  38. just the shard that matches the specific routing value (or values):
  39. [source,console]
  40. ------------------------------
  41. GET my_index/_search?routing=user1,user2 <1>
  42. {
  43. "query": {
  44. "match": {
  45. "title": "document"
  46. }
  47. }
  48. }
  49. ------------------------------
  50. <1> This search request will only be executed on the shards associated with the `user1` and `user2` routing values.
  51. ==== Making a routing value required
  52. When using custom routing, it is important to provide the routing value
  53. whenever <<docs-index_,indexing>>, <<docs-get,getting>>,
  54. <<docs-delete,deleting>>, or <<docs-update,updating>> a document.
  55. Forgetting the routing value can lead to a document being indexed on more than
  56. one shard. As a safeguard, the `_routing` field can be configured to make a
  57. custom `routing` value required for all CRUD operations:
  58. [source,console]
  59. ------------------------------
  60. PUT my_index2
  61. {
  62. "mappings": {
  63. "_routing": {
  64. "required": true <1>
  65. }
  66. }
  67. }
  68. PUT my_index2/_doc/1 <2>
  69. {
  70. "text": "No routing value provided"
  71. }
  72. ------------------------------
  73. // TEST[catch:bad_request]
  74. <1> Routing is required for all documents.
  75. <2> This index request throws a `routing_missing_exception`.
  76. ==== Unique IDs with custom routing
  77. When indexing documents specifying a custom `_routing`, the uniqueness of the
  78. `_id` is not guaranteed across all of the shards in the index. In fact,
  79. documents with the same `_id` might end up on different shards if indexed with
  80. different `_routing` values.
  81. It is up to the user to ensure that IDs are unique across the index.
  82. [[routing-index-partition]]
  83. ==== Routing to an index partition
  84. An index can be configured such that custom routing values will go to a subset of the shards rather
  85. than a single shard. This helps mitigate the risk of ending up with an imbalanced cluster while still
  86. reducing the impact of searches.
  87. This is done by providing the index level setting <<routing-partition-size,`index.routing_partition_size`>> at index creation.
  88. As the partition size increases, the more evenly distributed the data will become at the
  89. expense of having to search more shards per request.
  90. When this setting is present, the formula for calculating the shard becomes:
  91. shard_num = (hash(_routing) + hash(_id) % routing_partition_size) % num_primary_shards
  92. That is, the `_routing` field is used to calculate a set of shards within the index and then the
  93. `_id` is used to pick a shard within that set.
  94. To enable this feature, the `index.routing_partition_size` should have a value greater than 1 and
  95. less than `index.number_of_shards`.
  96. Once enabled, the partitioned index will have the following limitations:
  97. * Mappings with <<parent-join,`join` field>> relationships cannot be created within it.
  98. * All mappings within the index must have the `_routing` field marked as required.