index.asciidoc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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-timeout]
  45. --------------------------------------------------
  46. <1> Timeout to wait for primary shard to become available as a `TimeValue`
  47. <2> Timeout to wait for primary shard to become available as a `String`
  48. ["source","java",subs="attributes,callouts,macros"]
  49. --------------------------------------------------
  50. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-refresh]
  51. --------------------------------------------------
  52. <1> Refresh policy as a `WriteRequest.RefreshPolicy` instance
  53. <2> Refresh policy as a `String`
  54. ["source","java",subs="attributes,callouts,macros"]
  55. --------------------------------------------------
  56. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-version]
  57. --------------------------------------------------
  58. <1> Version
  59. ["source","java",subs="attributes,callouts,macros"]
  60. --------------------------------------------------
  61. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-version-type]
  62. --------------------------------------------------
  63. <1> Version type
  64. ["source","java",subs="attributes,callouts,macros"]
  65. --------------------------------------------------
  66. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-op-type]
  67. --------------------------------------------------
  68. <1> Operation type provided as an `DocWriteRequest.OpType` value
  69. <2> Operation type provided as a `String`: can be `create` or `update` (default)
  70. ["source","java",subs="attributes,callouts,macros"]
  71. --------------------------------------------------
  72. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-pipeline]
  73. --------------------------------------------------
  74. <1> The name of the ingest pipeline to be executed before indexing the document
  75. [[java-rest-high-document-index-sync]]
  76. ==== Synchronous Execution
  77. ["source","java",subs="attributes,callouts,macros"]
  78. --------------------------------------------------
  79. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-execute]
  80. --------------------------------------------------
  81. [[java-rest-high-document-index-async]]
  82. ==== Asynchronous Execution
  83. The asynchronous execution of an index request requires both the `IndexRequest`
  84. instance and an `ActionListener` instance to be passed to the asynchronous
  85. method:
  86. ["source","java",subs="attributes,callouts,macros"]
  87. --------------------------------------------------
  88. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-execute-async]
  89. --------------------------------------------------
  90. <1> The `IndexRequest` to execute and the `ActionListener` to use when
  91. the execution completes
  92. The asynchronous method does not block and returns immediately. Once it is
  93. completed the `ActionListener` is called back using the `onResponse` method
  94. if the execution successfully completed or using the `onFailure` method if
  95. it failed.
  96. A typical listener for `IndexResponse` looks like:
  97. ["source","java",subs="attributes,callouts,macros"]
  98. --------------------------------------------------
  99. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-execute-listener]
  100. --------------------------------------------------
  101. <1> Called when the execution is successfully completed. The response is
  102. provided as an argument
  103. <2> Called in case of failure. The raised exception is provided as an argument
  104. [[java-rest-high-document-index-response]]
  105. ==== Index Response
  106. The returned `IndexResponse` allows to retrieve information about the executed
  107. operation as follows:
  108. ["source","java",subs="attributes,callouts,macros"]
  109. --------------------------------------------------
  110. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-response]
  111. --------------------------------------------------
  112. <1> Handle (if needed) the case where the document was created for the first
  113. time
  114. <2> Handle (if needed) the case where the document was rewritten as it was
  115. already existing
  116. <3> Handle the situation where number of successful shards is less than
  117. total shards
  118. <4> Handle the potential failures
  119. If there is a version conflict, an `ElasticsearchException` will
  120. be thrown:
  121. ["source","java",subs="attributes,callouts,macros"]
  122. --------------------------------------------------
  123. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-conflict]
  124. --------------------------------------------------
  125. <1> The raised exception indicates that a version conflict error was returned
  126. Same will happen in case `opType` was set to `create` and a document with
  127. same index, type and id already existed:
  128. ["source","java",subs="attributes,callouts,macros"]
  129. --------------------------------------------------
  130. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-optype]
  131. --------------------------------------------------
  132. <1> The raised exception indicates that a version conflict error was returned