_index.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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:
  16. ["source","java",subs="attributes,callouts,macros"]
  17. --------------------------------------------------
  18. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-map]
  19. --------------------------------------------------
  20. <1> Document source provided as a `Map` which gets automatically converted
  21. to JSON format
  22. ["source","java",subs="attributes,callouts,macros"]
  23. --------------------------------------------------
  24. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-xcontent]
  25. --------------------------------------------------
  26. <1> Document source provided as an `XContentBuilder` object, the Elasticsearch
  27. built-in helpers to generate JSON content
  28. ["source","java",subs="attributes,callouts,macros"]
  29. --------------------------------------------------
  30. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-shortcut]
  31. --------------------------------------------------
  32. <1> Document source provided as `Object` key-pairs, which gets converted to
  33. JSON format
  34. ==== Optional arguments
  35. The following arguments can optionally be provided:
  36. ["source","java",subs="attributes,callouts,macros"]
  37. --------------------------------------------------
  38. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-routing]
  39. --------------------------------------------------
  40. <1> Routing value
  41. ["source","java",subs="attributes,callouts,macros"]
  42. --------------------------------------------------
  43. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-parent]
  44. --------------------------------------------------
  45. <1> Parent value
  46. ["source","java",subs="attributes,callouts,macros"]
  47. --------------------------------------------------
  48. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-timeout]
  49. --------------------------------------------------
  50. <1> Timeout to wait for primary shard to become available as a `TimeValue`
  51. <2> Timeout to wait for primary shard to become available as a `String`
  52. ["source","java",subs="attributes,callouts,macros"]
  53. --------------------------------------------------
  54. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-refresh]
  55. --------------------------------------------------
  56. <1> Refresh policy as a `WriteRequest.RefreshPolicy` instance
  57. <2> Refresh policy as a `String`
  58. ["source","java",subs="attributes,callouts,macros"]
  59. --------------------------------------------------
  60. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-version]
  61. --------------------------------------------------
  62. <1> Version
  63. ["source","java",subs="attributes,callouts,macros"]
  64. --------------------------------------------------
  65. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-version-type]
  66. --------------------------------------------------
  67. <1> Version type
  68. ["source","java",subs="attributes,callouts,macros"]
  69. --------------------------------------------------
  70. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-op-type]
  71. --------------------------------------------------
  72. <1> Operation type provided as an `DocWriteRequest.OpType` value
  73. <2> Operation type provided as a `String`: can be `create` or `update` (default)
  74. ["source","java",subs="attributes,callouts,macros"]
  75. --------------------------------------------------
  76. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-request-pipeline]
  77. --------------------------------------------------
  78. <1> The name of the ingest pipeline to be executed before indexing the document
  79. [[java-rest-high-document-index-sync]]
  80. ==== Synchronous Execution
  81. ["source","java",subs="attributes,callouts,macros"]
  82. --------------------------------------------------
  83. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-execute]
  84. --------------------------------------------------
  85. [[java-rest-high-document-index-async]]
  86. ==== Asynchronous Execution
  87. ["source","java",subs="attributes,callouts,macros"]
  88. --------------------------------------------------
  89. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-execute-async]
  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-index-response]]
  95. ==== Index Response
  96. The returned `IndexResponse` allows to retrieve information about the executed
  97. operation as follows:
  98. ["source","java",subs="attributes,callouts,macros"]
  99. --------------------------------------------------
  100. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-response]
  101. --------------------------------------------------
  102. <1> Handle (if needed) the case where the document was created for the first
  103. time
  104. <2> Handle (if needed) the case where the document was rewriten as it was
  105. already existing
  106. <3> Handle the situation where number of successful shards is less than
  107. total shards
  108. <4> Handle the potential failures
  109. If there is a version conflict, an `ElasticsearchException` will
  110. be thrown:
  111. ["source","java",subs="attributes,callouts,macros"]
  112. --------------------------------------------------
  113. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-conflict]
  114. --------------------------------------------------
  115. <1> The raised exception indicates that a version conflict error was returned.
  116. Same will happen in case `opType` was set to `create` and a document with
  117. same index, type and id already existed:
  118. ["source","java",subs="attributes,callouts,macros"]
  119. --------------------------------------------------
  120. include-tagged::{doc-tests}/CRUDDocumentationIT.java[index-optype]
  121. --------------------------------------------------
  122. <1> The raised exception indicates that a version conflict error was returned.