create-index.asciidoc 6.1 KB

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