multi-get.asciidoc 7.0 KB

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