create-index.asciidoc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. (Required, 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. - Names starting with `.` are deprecated, except for <<index-hidden,hidden indices>> and internal indices managed by plugins
  36. // end::index-name-reqs[]
  37. --
  38. [[indices-create-api-query-params]]
  39. ==== {api-query-parms-title}
  40. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
  41. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
  42. [[indices-create-api-request-body]]
  43. ==== {api-request-body-title}
  44. `aliases`::
  45. (Optional, <<indices-aliases,alias object>>) Index aliases which include the
  46. index. See <<indices-aliases>>.
  47. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=mappings]
  48. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=settings]
  49. [[indices-create-api-example]]
  50. ==== {api-examples-title}
  51. [[create-index-settings]]
  52. ===== Index settings
  53. Each index created can have specific settings
  54. associated with it, defined in the body:
  55. [source,console]
  56. --------------------------------------------------
  57. PUT /twitter
  58. {
  59. "settings" : {
  60. "index" : {
  61. "number_of_shards" : 3, <1>
  62. "number_of_replicas" : 2 <2>
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  67. <1> Default for `number_of_shards` is 1
  68. <2> Default for `number_of_replicas` is 1 (ie one replica for each primary shard)
  69. or more simplified
  70. [source,console]
  71. --------------------------------------------------
  72. PUT /twitter
  73. {
  74. "settings" : {
  75. "number_of_shards" : 3,
  76. "number_of_replicas" : 2
  77. }
  78. }
  79. --------------------------------------------------
  80. [NOTE]
  81. You do not have to explicitly specify `index` section inside the
  82. `settings` section.
  83. For more information regarding all the different index level settings
  84. that can be set when creating an index, please check the
  85. <<index-modules,index modules>> section.
  86. [[mappings]]
  87. ===== Mappings
  88. The create index API allows for providing a mapping definition:
  89. [source,console]
  90. --------------------------------------------------
  91. PUT /test
  92. {
  93. "settings" : {
  94. "number_of_shards" : 1
  95. },
  96. "mappings" : {
  97. "properties" : {
  98. "field1" : { "type" : "text" }
  99. }
  100. }
  101. }
  102. --------------------------------------------------
  103. [[create-index-aliases]]
  104. ===== Aliases
  105. The create index API allows also to provide a set of <<indices-aliases,aliases>>:
  106. [source,console]
  107. --------------------------------------------------
  108. PUT /test
  109. {
  110. "aliases" : {
  111. "alias_1" : {},
  112. "alias_2" : {
  113. "filter" : {
  114. "term" : {"user" : "kimchy" }
  115. },
  116. "routing" : "kimchy"
  117. }
  118. }
  119. }
  120. --------------------------------------------------
  121. [[create-index-wait-for-active-shards]]
  122. ===== Wait For active shards
  123. By default, index creation will only return a response to the client when the primary copies of
  124. each shard have been started, or the request times out. The index creation response will indicate
  125. what happened:
  126. [source,console-result]
  127. --------------------------------------------------
  128. {
  129. "acknowledged": true,
  130. "shards_acknowledged": true,
  131. "index": "test"
  132. }
  133. --------------------------------------------------
  134. `acknowledged` indicates whether the index was successfully created in the cluster, while
  135. `shards_acknowledged` indicates whether the requisite number of shard copies were started for
  136. each shard in the index before timing out. Note that it is still possible for either
  137. `acknowledged` or `shards_acknowledged` to be `false`, but the index creation was successful.
  138. These values simply indicate whether the operation completed before the timeout. If
  139. `acknowledged` is `false`, then we timed out before the cluster state was updated with the
  140. newly created index, but it probably will be created sometime soon. If `shards_acknowledged`
  141. is `false`, then we timed out before the requisite number of shards were started (by default
  142. just the primaries), even if the cluster state was successfully updated to reflect the newly
  143. created index (i.e. `acknowledged=true`).
  144. We can change the default of only waiting for the primary shards to start through the index
  145. setting `index.write.wait_for_active_shards` (note that changing this setting will also affect
  146. the `wait_for_active_shards` value on all subsequent write operations):
  147. [source,console]
  148. --------------------------------------------------
  149. PUT /test
  150. {
  151. "settings": {
  152. "index.write.wait_for_active_shards": "2"
  153. }
  154. }
  155. --------------------------------------------------
  156. // TEST[skip:requires two nodes]
  157. or through the request parameter `wait_for_active_shards`:
  158. [source,console]
  159. --------------------------------------------------
  160. PUT /test?wait_for_active_shards=2
  161. --------------------------------------------------
  162. // TEST[skip:requires two nodes]
  163. A detailed explanation of `wait_for_active_shards` and its possible values can be found
  164. <<index-wait-for-active-shards,here>>.