create-index.asciidoc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. [[indices-create-index]]
  2. === Create index API
  3. ++++
  4. <titleabbrev>Create index</titleabbrev>
  5. ++++
  6. Creates a new index.
  7. [source,console]
  8. --------------------------------------------------
  9. PUT /twitter
  10. --------------------------------------------------
  11. [[indices-create-api-request]]
  12. ==== {api-request-title}
  13. `PUT /<index>`
  14. [[indices-create-api-desc]]
  15. ==== {api-description-title}
  16. You can use the create index API to add a new index to an {es} cluster. When
  17. creating an index, you can specify the following:
  18. * Settings for the index
  19. * Mappings for fields in the index
  20. * Index aliases
  21. [[indices-create-api-path-params]]
  22. ==== {api-path-parms-title}
  23. `<index>`::
  24. +
  25. --
  26. (Optional, string) Name of the index you wish to create.
  27. // tag::index-name-reqs[]
  28. Index names must meet the following criteria:
  29. - Lowercase only
  30. - Cannot include `\`, `/`, `*`, `?`, `"`, `<`, `>`, `|`, ` ` (space character), `,`, `#`
  31. - Indices prior to 7.0 could contain a colon (`:`), but that's been deprecated and won't be supported in 7.0+
  32. - Cannot start with `-`, `_`, `+`
  33. - Cannot be `.` or `..`
  34. - Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit faster)
  35. // end::index-name-reqs[]
  36. --
  37. [[indices-create-api-query-params]]
  38. ==== {api-query-parms-title}
  39. include::{docdir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  40. include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  41. [[indices-create-api-request-body]]
  42. ==== {api-request-body-title}
  43. `aliases`::
  44. (Optional, <<indices-aliases,alias object>>) Index aliases which include the
  45. index. See <<indices-aliases>>.
  46. include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
  47. include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
  48. [[indices-create-api-example]]
  49. ==== {api-examples-title}
  50. [[create-index-settings]]
  51. ===== Index settings
  52. Each index created can have specific settings
  53. associated with it, defined in the body:
  54. [source,console]
  55. --------------------------------------------------
  56. PUT /twitter
  57. {
  58. "settings" : {
  59. "index" : {
  60. "number_of_shards" : 3, <1>
  61. "number_of_replicas" : 2 <2>
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. <1> Default for `number_of_shards` is 1
  67. <2> Default for `number_of_replicas` is 1 (ie one replica for each primary shard)
  68. or more simplified
  69. [source,console]
  70. --------------------------------------------------
  71. PUT /twitter
  72. {
  73. "settings" : {
  74. "number_of_shards" : 3,
  75. "number_of_replicas" : 2
  76. }
  77. }
  78. --------------------------------------------------
  79. [NOTE]
  80. You do not have to explicitly specify `index` section inside the
  81. `settings` section.
  82. For more information regarding all the different index level settings
  83. that can be set when creating an index, please check the
  84. <<index-modules,index modules>> section.
  85. [[mappings]]
  86. ===== Mappings
  87. The create index API allows for providing a mapping definition:
  88. [source,console]
  89. --------------------------------------------------
  90. PUT /test
  91. {
  92. "settings" : {
  93. "number_of_shards" : 1
  94. },
  95. "mappings" : {
  96. "properties" : {
  97. "field1" : { "type" : "text" }
  98. }
  99. }
  100. }
  101. --------------------------------------------------
  102. [[create-index-aliases]]
  103. ===== Aliases
  104. The create index API allows also to provide a set of <<indices-aliases,aliases>>:
  105. [source,console]
  106. --------------------------------------------------
  107. PUT /test
  108. {
  109. "aliases" : {
  110. "alias_1" : {},
  111. "alias_2" : {
  112. "filter" : {
  113. "term" : {"user" : "kimchy" }
  114. },
  115. "routing" : "kimchy"
  116. }
  117. }
  118. }
  119. --------------------------------------------------
  120. [[create-index-wait-for-active-shards]]
  121. ===== Wait For active shards
  122. By default, index creation will only return a response to the client when the primary copies of
  123. each shard have been started, or the request times out. The index creation response will indicate
  124. what happened:
  125. [source,console-result]
  126. --------------------------------------------------
  127. {
  128. "acknowledged": true,
  129. "shards_acknowledged": true,
  130. "index": "test"
  131. }
  132. --------------------------------------------------
  133. `acknowledged` indicates whether the index was successfully created in the cluster, while
  134. `shards_acknowledged` indicates whether the requisite number of shard copies were started for
  135. each shard in the index before timing out. Note that it is still possible for either
  136. `acknowledged` or `shards_acknowledged` to be `false`, but the index creation was successful.
  137. These values simply indicate whether the operation completed before the timeout. If
  138. `acknowledged` is `false`, then we timed out before the cluster state was updated with the
  139. newly created index, but it probably will be created sometime soon. If `shards_acknowledged`
  140. is `false`, then we timed out before the requisite number of shards were started (by default
  141. just the primaries), even if the cluster state was successfully updated to reflect the newly
  142. created index (i.e. `acknowledged=true`).
  143. We can change the default of only waiting for the primary shards to start through the index
  144. setting `index.write.wait_for_active_shards` (note that changing this setting will also affect
  145. the `wait_for_active_shards` value on all subsequent write operations):
  146. [source,console]
  147. --------------------------------------------------
  148. PUT /test
  149. {
  150. "settings": {
  151. "index.write.wait_for_active_shards": "2"
  152. }
  153. }
  154. --------------------------------------------------
  155. // TEST[skip:requires two nodes]
  156. or through the request parameter `wait_for_active_shards`:
  157. [source,console]
  158. --------------------------------------------------
  159. PUT /test?wait_for_active_shards=2
  160. --------------------------------------------------
  161. // TEST[skip:requires two nodes]
  162. A detailed explanation of `wait_for_active_shards` and its possible values can be found
  163. <<index-wait-for-active-shards,here>>.