create-index.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. See <<indices-aliases>>.
  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. [[create-index-wait-for-active-shards]]
  126. ===== Wait for active shards
  127. By default, index creation will only return a response to the client when the primary copies of
  128. each shard have been started, or the request times out. The index creation response will indicate
  129. what happened:
  130. [source,console-result]
  131. --------------------------------------------------
  132. {
  133. "acknowledged": true,
  134. "shards_acknowledged": true,
  135. "index": "test"
  136. }
  137. --------------------------------------------------
  138. `acknowledged` indicates whether the index was successfully created in the cluster, while
  139. `shards_acknowledged` indicates whether the requisite number of shard copies were started for
  140. each shard in the index before timing out. Note that it is still possible for either
  141. `acknowledged` or `shards_acknowledged` to be `false`, but the index creation was successful.
  142. These values simply indicate whether the operation completed before the timeout. If
  143. `acknowledged` is `false`, then we timed out before the cluster state was updated with the
  144. newly created index, but it probably will be created sometime soon. If `shards_acknowledged`
  145. is `false`, then we timed out before the requisite number of shards were started (by default
  146. just the primaries), even if the cluster state was successfully updated to reflect the newly
  147. created index (i.e. `acknowledged=true`).
  148. We can change the default of only waiting for the primary shards to start through the index
  149. setting `index.write.wait_for_active_shards` (note that changing this setting will also affect
  150. the `wait_for_active_shards` value on all subsequent write operations):
  151. [source,console]
  152. --------------------------------------------------
  153. PUT /test
  154. {
  155. "settings": {
  156. "index.write.wait_for_active_shards": "2"
  157. }
  158. }
  159. --------------------------------------------------
  160. // TEST[skip:requires two nodes]
  161. or through the request parameter `wait_for_active_shards`:
  162. [source,console]
  163. --------------------------------------------------
  164. PUT /test?wait_for_active_shards=2
  165. --------------------------------------------------
  166. // TEST[skip:requires two nodes]
  167. A detailed explanation of `wait_for_active_shards` and its possible values can be found
  168. <<index-wait-for-active-shards,here>>.