create-index.asciidoc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. [[indices-create-index]]
  2. === Create index API
  3. ++++
  4. <titleabbrev>Create index</titleabbrev>
  5. ++++
  6. Creates a new index.
  7. [source,js]
  8. --------------------------------------------------
  9. PUT /twitter
  10. --------------------------------------------------
  11. // CONSOLE
  12. [[indices-create-api-request]]
  13. ==== {api-request-title}
  14. `PUT /<index>`
  15. [[indices-create-api-desc]]
  16. ==== {api-description-title}
  17. You can use the create index API to add a new index to an {es} cluster. When
  18. creating an index, you can specify the following:
  19. * Settings for the index
  20. * Mappings for fields in the index
  21. * Index aliases
  22. [[indices-create-api-path-params]]
  23. ==== {api-path-parms-title}
  24. `<index>`::
  25. +
  26. --
  27. (Optional, string) Name of the index you wish to create.
  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. --
  36. [[indices-create-api-query-params]]
  37. ==== {api-query-parms-title}
  38. include::{docdir}/rest-api/common-parms.asciidoc[tag=include-type-name]
  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,js]
  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. // CONSOLE
  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,js]
  71. --------------------------------------------------
  72. PUT /twitter
  73. {
  74. "settings" : {
  75. "number_of_shards" : 3,
  76. "number_of_replicas" : 2
  77. }
  78. }
  79. --------------------------------------------------
  80. // CONSOLE
  81. [NOTE]
  82. You do not have to explicitly specify `index` section inside the
  83. `settings` section.
  84. For more information regarding all the different index level settings
  85. that can be set when creating an index, please check the
  86. <<index-modules,index modules>> section.
  87. [[mappings]]
  88. ===== Mappings
  89. The create index API allows for providing a mapping definition:
  90. [source,js]
  91. --------------------------------------------------
  92. PUT /test
  93. {
  94. "settings" : {
  95. "number_of_shards" : 1
  96. },
  97. "mappings" : {
  98. "properties" : {
  99. "field1" : { "type" : "text" }
  100. }
  101. }
  102. }
  103. --------------------------------------------------
  104. // CONSOLE
  105. NOTE: Before 7.0.0, the 'mappings' definition used to include a type name. Although specifying
  106. types in requests is now deprecated, a type can still be provided if the request parameter
  107. include_type_name is set. For more details, please see <<removal-of-types>>.
  108. [[create-index-aliases]]
  109. ===== Aliases
  110. The create index API allows also to provide a set of <<indices-aliases,aliases>>:
  111. [source,js]
  112. --------------------------------------------------
  113. PUT /test
  114. {
  115. "aliases" : {
  116. "alias_1" : {},
  117. "alias_2" : {
  118. "filter" : {
  119. "term" : {"user" : "kimchy" }
  120. },
  121. "routing" : "kimchy"
  122. }
  123. }
  124. }
  125. --------------------------------------------------
  126. // CONSOLE
  127. [[create-index-wait-for-active-shards]]
  128. ===== Wait For active shards
  129. By default, index creation will only return a response to the client when the primary copies of
  130. each shard have been started, or the request times out. The index creation response will indicate
  131. what happened:
  132. [source,console-result]
  133. --------------------------------------------------
  134. {
  135. "acknowledged": true,
  136. "shards_acknowledged": true,
  137. "index": "test"
  138. }
  139. --------------------------------------------------
  140. `acknowledged` indicates whether the index was successfully created in the cluster, while
  141. `shards_acknowledged` indicates whether the requisite number of shard copies were started for
  142. each shard in the index before timing out. Note that it is still possible for either
  143. `acknowledged` or `shards_acknowledged` to be `false`, but the index creation was successful.
  144. These values simply indicate whether the operation completed before the timeout. If
  145. `acknowledged` is `false`, then we timed out before the cluster state was updated with the
  146. newly created index, but it probably will be created sometime soon. If `shards_acknowledged`
  147. is `false`, then we timed out before the requisite number of shards were started (by default
  148. just the primaries), even if the cluster state was successfully updated to reflect the newly
  149. created index (i.e. `acknowledged=true`).
  150. We can change the default of only waiting for the primary shards to start through the index
  151. setting `index.write.wait_for_active_shards` (note that changing this setting will also affect
  152. the `wait_for_active_shards` value on all subsequent write operations):
  153. [source,js]
  154. --------------------------------------------------
  155. PUT /test
  156. {
  157. "settings": {
  158. "index.write.wait_for_active_shards": "2"
  159. }
  160. }
  161. --------------------------------------------------
  162. // CONSOLE
  163. // TEST[skip:requires two nodes]
  164. or through the request parameter `wait_for_active_shards`:
  165. [source,js]
  166. --------------------------------------------------
  167. PUT /test?wait_for_active_shards=2
  168. --------------------------------------------------
  169. // CONSOLE
  170. // TEST[skip:requires two nodes]
  171. A detailed explanation of `wait_for_active_shards` and its possible values can be found
  172. <<index-wait-for-active-shards,here>>.