routing-field.asciidoc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <1>
  13. {
  14. "title": "This is a document"
  15. }
  16. GET my_index/my_type/1?routing=user1 <2>
  17. ------------------------------
  18. // AUTOSENSE
  19. <1> This document uses `user1` as its routing value, instead of its ID.
  20. <2> The the same `routing` value needs to be provided when
  21. <<docs-get,getting>>, <<docs-delete,deleting>>, or <<docs-update,updating>>
  22. the document.
  23. The value of the `_routing` field is accessible in queries, aggregations, scripts,
  24. and when sorting:
  25. [source,js]
  26. --------------------------
  27. GET my_index/_search
  28. {
  29. "query": {
  30. "terms": {
  31. "_routing": [ "user1" ] <1>
  32. }
  33. },
  34. "aggs": {
  35. "Routing values": {
  36. "terms": {
  37. "field": "_routing", <2>
  38. "size": 10
  39. }
  40. }
  41. },
  42. "sort": [
  43. {
  44. "_routing": { <3>
  45. "order": "desc"
  46. }
  47. }
  48. ],
  49. "script_fields": {
  50. "Routing value": {
  51. "script": "doc['_routing']" <4>
  52. }
  53. }
  54. }
  55. --------------------------
  56. // AUTOSENSE
  57. <1> Querying on the `_routing` field (also see the <<query-dsl-ids-query,`ids` query>>)
  58. <2> Aggregating on the `_routing` field
  59. <3> Sorting on the `_routing` field
  60. <4> Accessing the `_routing` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)
  61. ==== Searching with custom routing
  62. Custom routing can reduce the impact of searches. Instead of having to fan
  63. out a search request to all the shards in an index, the request can be sent to
  64. just the shard that matches the specific routing value (or values):
  65. [source,js]
  66. ------------------------------
  67. GET my_index/_search?routing=user1,user2 <1>
  68. {
  69. "query": {
  70. "match": {
  71. "title": "document"
  72. }
  73. }
  74. }
  75. ------------------------------
  76. // AUTOSENSE
  77. <1> This search request will only be executed on the shards associated with the `user1` and `user2` routing values.
  78. ==== Making a routing value required
  79. When using custom routing, it is important to provide the routing value
  80. whenever <<docs-index_,indexing>>, <<docs-get,getting>>,
  81. <<docs-delete,deleting>>, or <<docs-update,updating>> a document.
  82. Forgetting the routing value can lead to a document being indexed on more than
  83. one shard. As a safeguard, the `_routing` field can be configured to make a
  84. custom `routing` value required for all CRUD operations:
  85. [source,js]
  86. ------------------------------
  87. PUT my_index
  88. {
  89. "mappings": {
  90. "my_type": {
  91. "_routing": {
  92. "required": true <1>
  93. }
  94. }
  95. }
  96. }
  97. PUT my_index/my_type/1 <2>
  98. {
  99. "text": "No routing value provided"
  100. }
  101. ------------------------------
  102. // AUTOSENSE
  103. <1> Routing is required for `my_type` documents.
  104. <2> This index request throws a `routing_missing_exception`.
  105. ==== Unique IDs with custom routing
  106. When indexing documents specifying a custom `_routing`, the uniqueness of the
  107. `_id` is not guaranteed across all of the shards in the index. In fact,
  108. documents with the same `_id` might end up on different shards if indexed with
  109. different `_routing` values.
  110. It is up to the user to ensure that IDs are unique across the index.