create_index.asciidoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. [[java-rest-high-create-index]]
  2. === Create Index API
  3. [[java-rest-high-create-index-request]]
  4. ==== Create Index Request
  5. A `CreateIndexRequest` requires an `index` argument:
  6. ["source","java",subs="attributes,callouts,macros"]
  7. --------------------------------------------------
  8. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-request]
  9. --------------------------------------------------
  10. <1> The index to create
  11. ==== Index settings
  12. Each index created can have specific settings associated with it.
  13. ["source","java",subs="attributes,callouts,macros"]
  14. --------------------------------------------------
  15. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-request-settings]
  16. --------------------------------------------------
  17. <1> Settings for this index
  18. [[java-rest-high-create-index-request-mappings]]
  19. ==== Index mappings
  20. An index may be created with mappings for its document types
  21. ["source","java",subs="attributes,callouts,macros"]
  22. --------------------------------------------------
  23. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-request-mappings]
  24. --------------------------------------------------
  25. <1> The type to define
  26. <2> The mapping for this type, provided as a JSON string
  27. The mapping source can be provided in different ways in addition to the
  28. `String` example shown above:
  29. ["source","java",subs="attributes,callouts,macros"]
  30. --------------------------------------------------
  31. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-mappings-map]
  32. --------------------------------------------------
  33. <1> Mapping source provided as a `Map` which gets automatically converted
  34. to JSON format
  35. ["source","java",subs="attributes,callouts,macros"]
  36. --------------------------------------------------
  37. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-mappings-xcontent]
  38. --------------------------------------------------
  39. <1> Mapping source provided as an `XContentBuilder` object, the Elasticsearch
  40. built-in helpers to generate JSON content
  41. ["source","java",subs="attributes,callouts,macros"]
  42. --------------------------------------------------
  43. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-mappings-shortcut]
  44. --------------------------------------------------
  45. <1> Mapping source provided as `Object` key-pairs, which gets converted to
  46. JSON format
  47. ==== Index aliases
  48. Aliases can be set at index creation time
  49. ["source","java",subs="attributes,callouts,macros"]
  50. --------------------------------------------------
  51. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-request-aliases]
  52. --------------------------------------------------
  53. <1> The alias to define
  54. ==== Providing the whole source
  55. The whole source including all of its sections (mappings, settings and aliases)
  56. can also be provided:
  57. ["source","java",subs="attributes,callouts,macros"]
  58. --------------------------------------------------
  59. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-whole-source]
  60. --------------------------------------------------
  61. <1> The source provided as a JSON string. It can also be provided as a `Map`
  62. or an `XContentBuilder`.
  63. ==== Optional arguments
  64. The following arguments can optionally be provided:
  65. ["source","java",subs="attributes,callouts,macros"]
  66. --------------------------------------------------
  67. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-request-timeout]
  68. --------------------------------------------------
  69. <1> Timeout to wait for the all the nodes to acknowledge the index creation as a `TimeValue`
  70. <2> Timeout to wait for the all the nodes to acknowledge the index creation as a `String`
  71. ["source","java",subs="attributes,callouts,macros"]
  72. --------------------------------------------------
  73. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-request-masterTimeout]
  74. --------------------------------------------------
  75. <1> Timeout to connect to the master node as a `TimeValue`
  76. <2> Timeout to connect to the master node as a `String`
  77. ["source","java",subs="attributes,callouts,macros"]
  78. --------------------------------------------------
  79. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-request-waitForActiveShards]
  80. --------------------------------------------------
  81. <1> The number of active shard copies to wait for before the create index API returns a
  82. response, as an `int`
  83. <2> The number of active shard copies to wait for before the create index API returns a
  84. response, as an `ActiveShardCount`
  85. [[java-rest-high-create-index-sync]]
  86. ==== Synchronous Execution
  87. ["source","java",subs="attributes,callouts,macros"]
  88. --------------------------------------------------
  89. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-execute]
  90. --------------------------------------------------
  91. [[java-rest-high-create-index-async]]
  92. ==== Asynchronous Execution
  93. The asynchronous execution of a create index request requires both the `CreateIndexRequest`
  94. instance and an `ActionListener` instance to be passed to the asynchronous
  95. method:
  96. ["source","java",subs="attributes,callouts,macros"]
  97. --------------------------------------------------
  98. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-execute-async]
  99. --------------------------------------------------
  100. <1> The `CreateIndexRequest` to execute and the `ActionListener` to use when
  101. the execution completes
  102. The asynchronous method does not block and returns immediately. Once it is
  103. completed the `ActionListener` is called back using the `onResponse` method
  104. if the execution successfully completed or using the `onFailure` method if
  105. it failed.
  106. A typical listener for `CreateIndexResponse` looks like:
  107. ["source","java",subs="attributes,callouts,macros"]
  108. --------------------------------------------------
  109. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-execute-listener]
  110. --------------------------------------------------
  111. <1> Called when the execution is successfully completed. The response is
  112. provided as an argument
  113. <2> Called in case of failure. The raised exception is provided as an argument
  114. [[java-rest-high-create-index-response]]
  115. ==== Create Index Response
  116. The returned `CreateIndexResponse` allows to retrieve information about the executed
  117. operation as follows:
  118. ["source","java",subs="attributes,callouts,macros"]
  119. --------------------------------------------------
  120. include-tagged::{doc-tests}/IndicesClientDocumentationIT.java[create-index-response]
  121. --------------------------------------------------
  122. <1> Indicates whether all of the nodes have acknowledged the request
  123. <2> Indicates whether the requisite number of shard copies were started for each shard in the index before timing out