create-index.asciidoc 6.2 KB

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