multi-get.asciidoc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. --
  2. :api: multi-get
  3. :request: MultiGetRequest
  4. :response: MultiGetResponse
  5. --
  6. [id="{upid}-{api}"]
  7. === Multi-Get API
  8. The `multiGet` API executes multiple <<java-rest-high-document-get,`get`>>
  9. requests in a single http request in parallel.
  10. [id="{upid}-{api}-request"]
  11. ==== Multi-Get Request
  12. A +{request}+ is built empty and you add `MultiGetRequest.Item`s to configure
  13. what to fetch:
  14. ["source","java",subs="attributes,callouts,macros"]
  15. --------------------------------------------------
  16. include-tagged::{doc-tests-file}[{api}-request]
  17. --------------------------------------------------
  18. <1> Index
  19. <2> Document id
  20. <3> Add another item to fetch
  21. ==== Optional arguments
  22. `multiGet` supports the same optional arguments that the
  23. <<java-rest-high-document-get-request-optional-arguments,`get` API>> supports.
  24. You can set most of these on the `Item`:
  25. ["source","java",subs="attributes,callouts,macros"]
  26. --------------------------------------------------
  27. include-tagged::{doc-tests-file}[{api}-request-no-source]
  28. --------------------------------------------------
  29. <1> Disable source retrieval, enabled by default
  30. ["source","java",subs="attributes,callouts,macros"]
  31. --------------------------------------------------
  32. include-tagged::{doc-tests-file}[{api}-request-source-include]
  33. --------------------------------------------------
  34. <1> Configure source inclusion for specific fields
  35. ["source","java",subs="attributes,callouts,macros"]
  36. --------------------------------------------------
  37. include-tagged::{doc-tests-file}[{api}-request-source-exclude]
  38. --------------------------------------------------
  39. <1> Configure source exclusion for specific fields
  40. ["source","java",subs="attributes,callouts,macros"]
  41. --------------------------------------------------
  42. include-tagged::{doc-tests-file}[{api}-request-stored]
  43. --------------------------------------------------
  44. <1> Configure retrieval for specific stored fields (requires fields to be
  45. stored separately in the mappings)
  46. <2> Retrieve the `foo` stored field (requires the field to be stored
  47. separately in the mappings)
  48. ["source","java",subs="attributes,callouts,macros"]
  49. --------------------------------------------------
  50. include-tagged::{doc-tests-file}[{api}-request-item-extras]
  51. --------------------------------------------------
  52. <1> Routing value
  53. <2> Version
  54. <3> Version type
  55. {ref}/search-request-preference.html[`preference`],
  56. {ref}/docs-get.html#realtime[`realtime`]
  57. and
  58. {ref}/docs-get.html#get-refresh[`refresh`] can be set on the main request but
  59. not on any items:
  60. ["source","java",subs="attributes,callouts,macros"]
  61. --------------------------------------------------
  62. include-tagged::{doc-tests-file}[{api}-request-top-level-extras]
  63. --------------------------------------------------
  64. <1> Preference value
  65. <2> Set realtime flag to `false` (`true` by default)
  66. <3> Perform a refresh before retrieving the document (`false` by default)
  67. include::../execution.asciidoc[]
  68. [id="{upid}-{api}-response"]
  69. ==== Multi Get Response
  70. The returned +{response}+ contains a list of `MultiGetItemResponse`s in
  71. `getResponses` in the same order that they were requested.
  72. `MultiGetItemResponse` contains *either* a
  73. <<java-rest-high-document-get-response, `GetResponse`>> if the get succeeded
  74. or a `MultiGetResponse.Failure` if it failed. A success looks just like a
  75. normal `GetResponse`.
  76. ["source","java",subs="attributes,callouts,macros"]
  77. --------------------------------------------------
  78. include-tagged::{doc-tests-file}[{api}-response]
  79. --------------------------------------------------
  80. <1> `getFailure` returns null because there isn't a failure.
  81. <2> `getResponse` returns the `GetResponse`.
  82. <3> Retrieve the document as a `String`
  83. <4> Retrieve the document as a `Map<String, Object>`
  84. <5> Retrieve the document as a `byte[]`
  85. <6> Handle the scenario where the document was not found. Note that although
  86. the returned response has `404` status code, a valid `GetResponse` is
  87. returned rather than an exception thrown. Such response does not hold any
  88. source document and its `isExists` method returns `false`.
  89. When one of the subrequests as performed against an index that does not exist
  90. `getFailure` will contain an exception:
  91. ["source","java",subs="attributes,callouts,macros"]
  92. --------------------------------------------------
  93. include-tagged::{doc-tests-file}[{api}-indexnotfound]
  94. --------------------------------------------------
  95. <1> `getResponse` is null.
  96. <2> `getFailure` isn't and contains an `Exception`.
  97. <3> That `Exception` is actually an `ElasticsearchException`
  98. <4> and it has a status of `NOT_FOUND`. It'd have been an HTTP 404 if this
  99. wasn't a multi get.
  100. <5> `getMessage` explains the actual cause, `no such index`.
  101. In case a specific document version has been requested, and the existing
  102. document has a different version number, a version conflict is raised:
  103. ["source","java",subs="attributes,callouts,macros"]
  104. --------------------------------------------------
  105. include-tagged::{doc-tests-file}[{api}-conflict]
  106. --------------------------------------------------
  107. <1> `getResponse` is null.
  108. <2> `getFailure` isn't and contains an `Exception`.
  109. <3> That `Exception` is actually an `ElasticsearchException`
  110. <4> and it has a status of `CONFLICT`. It'd have been an HTTP 409 if this
  111. wasn't a multi get.
  112. <5> `getMessage` explains the actual cause, `