routing-field.asciidoc 4.2 KB

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