get.asciidoc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. [[java-rest-high-document-get]]
  2. === Get API
  3. [[java-rest-high-document-get-request]]
  4. ==== Get Request
  5. A `GetRequest` requires the following arguments:
  6. ["source","java",subs="attributes,callouts,macros"]
  7. --------------------------------------------------
  8. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request]
  9. --------------------------------------------------
  10. <1> Index
  11. <2> Type
  12. <3> Document id
  13. [[java-rest-high-document-get-request-optional-arguments]]
  14. ==== Optional arguments
  15. The following arguments can optionally be provided:
  16. ["source","java",subs="attributes,callouts,macros"]
  17. --------------------------------------------------
  18. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-no-source]
  19. --------------------------------------------------
  20. <1> Disable source retrieval, enabled by default
  21. ["source","java",subs="attributes,callouts,macros"]
  22. --------------------------------------------------
  23. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-source-include]
  24. --------------------------------------------------
  25. <1> Configure source inclusion for specific fields
  26. ["source","java",subs="attributes,callouts,macros"]
  27. --------------------------------------------------
  28. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-source-exclude]
  29. --------------------------------------------------
  30. <1> Configure source exclusion for specific fields
  31. ["source","java",subs="attributes,callouts,macros"]
  32. --------------------------------------------------
  33. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-stored]
  34. --------------------------------------------------
  35. <1> Configure retrieval for specific stored fields (requires fields to be
  36. stored separately in the mappings)
  37. <2> Retrieve the `message` stored field (requires the field to be stored
  38. separately in the mappings)
  39. ["source","java",subs="attributes,callouts,macros"]
  40. --------------------------------------------------
  41. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-routing]
  42. --------------------------------------------------
  43. <1> Routing value
  44. ["source","java",subs="attributes,callouts,macros"]
  45. --------------------------------------------------
  46. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-preference]
  47. --------------------------------------------------
  48. <1> Preference value
  49. ["source","java",subs="attributes,callouts,macros"]
  50. --------------------------------------------------
  51. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-realtime]
  52. --------------------------------------------------
  53. <1> Set realtime flag to `false` (`true` by default)
  54. ["source","java",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-refresh]
  57. --------------------------------------------------
  58. <1> Perform a refresh before retrieving the document (`false` by default)
  59. ["source","java",subs="attributes,callouts,macros"]
  60. --------------------------------------------------
  61. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-version]
  62. --------------------------------------------------
  63. <1> Version
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-request-version-type]
  67. --------------------------------------------------
  68. <1> Version type
  69. [[java-rest-high-document-get-sync]]
  70. ==== Synchronous Execution
  71. ["source","java",subs="attributes,callouts,macros"]
  72. --------------------------------------------------
  73. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-execute]
  74. --------------------------------------------------
  75. [[java-rest-high-document-get-async]]
  76. ==== Asynchronous Execution
  77. The asynchronous execution of a get request requires both the `GetRequest`
  78. instance and an `ActionListener` instance to be passed to the asynchronous
  79. method:
  80. ["source","java",subs="attributes,callouts,macros"]
  81. --------------------------------------------------
  82. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-execute-async]
  83. --------------------------------------------------
  84. <1> The `GetRequest` to execute and the `ActionListener` to use when
  85. the execution completes
  86. The asynchronous method does not block and returns immediately. Once it is
  87. completed the `ActionListener` is called back using the `onResponse` method
  88. if the execution successfully completed or using the `onFailure` method if
  89. it failed.
  90. A typical listener for `GetResponse` looks like:
  91. ["source","java",subs="attributes,callouts,macros"]
  92. --------------------------------------------------
  93. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-execute-listener]
  94. --------------------------------------------------
  95. <1> Called when the execution is successfully completed. The response is
  96. provided as an argument.
  97. <2> Called in case of failure. The raised exception is provided as an argument.
  98. [[java-rest-high-document-get-response]]
  99. ==== Get Response
  100. The returned `GetResponse` allows to retrieve the requested document along with
  101. its metadata and eventually stored fields.
  102. ["source","java",subs="attributes,callouts,macros"]
  103. --------------------------------------------------
  104. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-response]
  105. --------------------------------------------------
  106. <1> Retrieve the document as a `String`
  107. <2> Retrieve the document as a `Map<String, Object>`
  108. <3> Retrieve the document as a `byte[]`
  109. <4> Handle the scenario where the document was not found. Note that although
  110. the returned response has `404` status code, a valid `GetResponse` is
  111. returned rather than an exception thrown. Such response does not hold any
  112. source document and its `isExists` method returns `false`.
  113. When a get request is performed against an index that does not exist, the
  114. response has `404` status code, an `ElasticsearchException` gets thrown
  115. which needs to be handled as follows:
  116. ["source","java",subs="attributes,callouts,macros"]
  117. --------------------------------------------------
  118. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-indexnotfound]
  119. --------------------------------------------------
  120. <1> Handle the exception thrown because the index does not exist
  121. In case a specific document version has been requested, and the existing
  122. document has a different version number, a version conflict is raised:
  123. ["source","java",subs="attributes,callouts,macros"]
  124. --------------------------------------------------
  125. include-tagged::{doc-tests}/CRUDDocumentationIT.java[get-conflict]
  126. --------------------------------------------------
  127. <1> The raised exception indicates that a version conflict error was returned