create_index.asciidoc 6.6 KB

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