multi-get.asciidoc 5.2 KB

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