index.asciidoc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. [[java-rest-high-document-index]]
  2. === Index API
  3. [[java-rest-high-document-index-request]]
  4. ==== Index Request
  5. An `IndexRequest` requires the following arguments:
  6. ["source","java",subs="attributes,callouts,macros"]
  7. --------------------------------------------------
  8. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-string]
  9. --------------------------------------------------
  10. <1> Index
  11. <2> Type
  12. <3> Document id
  13. <4> Document source provided as a `String`
  14. ==== Providing the document source
  15. The document source can be provided in different ways in addition to the
  16. `String` example shown above:
  17. ["source","java",subs="attributes,callouts,macros"]
  18. --------------------------------------------------
  19. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-map]
  20. --------------------------------------------------
  21. <1> Document source provided as a `Map` which gets automatically converted
  22. to JSON format
  23. ["source","java",subs="attributes,callouts,macros"]
  24. --------------------------------------------------
  25. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-xcontent]
  26. --------------------------------------------------
  27. <1> Document source provided as an `XContentBuilder` object, the Elasticsearch
  28. built-in helpers to generate JSON content
  29. ["source","java",subs="attributes,callouts,macros"]
  30. --------------------------------------------------
  31. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-shortcut]
  32. --------------------------------------------------
  33. <1> Document source provided as `Object` key-pairs, which gets converted to
  34. JSON format
  35. ==== Optional arguments
  36. The following arguments can optionally be provided:
  37. ["source","java",subs="attributes,callouts,macros"]
  38. --------------------------------------------------
  39. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-routing]
  40. --------------------------------------------------
  41. <1> Routing value
  42. ["source","java",subs="attributes,callouts,macros"]
  43. --------------------------------------------------
  44. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-parent]
  45. --------------------------------------------------
  46. <1> Parent value
  47. ["source","java",subs="attributes,callouts,macros"]
  48. --------------------------------------------------
  49. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-timeout]
  50. --------------------------------------------------
  51. <1> Timeout to wait for primary shard to become available as a `TimeValue`
  52. <2> Timeout to wait for primary shard to become available as a `String`
  53. ["source","java",subs="attributes,callouts,macros"]
  54. --------------------------------------------------
  55. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-refresh]
  56. --------------------------------------------------
  57. <1> Refresh policy as a `WriteRequest.RefreshPolicy` instance
  58. <2> Refresh policy as a `String`
  59. ["source","java",subs="attributes,callouts,macros"]
  60. --------------------------------------------------
  61. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-version]
  62. --------------------------------------------------
  63. <1> Version
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-version-type]
  67. --------------------------------------------------
  68. <1> Version type
  69. ["source","java",subs="attributes,callouts,macros"]
  70. --------------------------------------------------
  71. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-op-type]
  72. --------------------------------------------------
  73. <1> Operation type provided as an `DocWriteRequest.OpType` value
  74. <2> Operation type provided as a `String`: can be `create` or `update` (default)
  75. ["source","java",subs="attributes,callouts,macros"]
  76. --------------------------------------------------
  77. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-pipeline]
  78. --------------------------------------------------
  79. <1> The name of the ingest pipeline to be executed before indexing the document
  80. [[java-rest-high-document-index-sync]]
  81. ==== Synchronous Execution
  82. ["source","java",subs="attributes,callouts,macros"]
  83. --------------------------------------------------
  84. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-execute]
  85. --------------------------------------------------
  86. [[java-rest-high-document-index-async]]
  87. ==== Asynchronous Execution
  88. The asynchronous execution of an index request requires both the `IndexRequest`
  89. instance and an `ActionListener` instance to be passed to the asynchronous
  90. method:
  91. ["source","java",subs="attributes,callouts,macros"]
  92. --------------------------------------------------
  93. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-execute-async]
  94. --------------------------------------------------
  95. <1> The `IndexRequest` to execute and the `ActionListener` to use when
  96. the execution completes
  97. The asynchronous method does not block and returns immediately. Once it is
  98. completed the `ActionListener` is called back using the `onResponse` method
  99. if the execution successfully completed or using the `onFailure` method if
  100. it failed.
  101. A typical listener for `IndexResponse` looks like:
  102. ["source","java",subs="attributes,callouts,macros"]
  103. --------------------------------------------------
  104. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-execute-listener]
  105. --------------------------------------------------
  106. <1> Called when the execution is successfully completed. The response is
  107. provided as an argument
  108. <2> Called in case of failure. The raised exception is provided as an argument
  109. [[java-rest-high-document-index-response]]
  110. ==== Index Response
  111. The returned `IndexResponse` allows to retrieve information about the executed
  112. operation as follows:
  113. ["source","java",subs="attributes,callouts,macros"]
  114. --------------------------------------------------
  115. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-response]
  116. --------------------------------------------------
  117. <1> Handle (if needed) the case where the document was created for the first
  118. time
  119. <2> Handle (if needed) the case where the document was rewritten as it was
  120. already existing
  121. <3> Handle the situation where number of successful shards is less than
  122. total shards
  123. <4> Handle the potential failures
  124. If there is a version conflict, an `ElasticsearchException` will
  125. be thrown:
  126. ["source","java",subs="attributes,callouts,macros"]
  127. --------------------------------------------------
  128. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-conflict]
  129. --------------------------------------------------
  130. <1> The raised exception indicates that a version conflict error was returned
  131. Same will happen in case `opType` was set to `create` and a document with
  132. same index, type and id already existed:
  133. ["source","java",subs="attributes,callouts,macros"]
  134. --------------------------------------------------
  135. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-optype]
  136. --------------------------------------------------
  137. <1> The raised exception indicates that a version conflict error was returned