create-index.asciidoc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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=doc-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. `mappings`::
  47. +
  48. --
  49. (Optional, <<mapping,mapping object>>) Mapping for fields in the index. If
  50. specified, this mapping can include:
  51. * Field names
  52. * <<mapping-types,Field datatypes>>
  53. * <<mapping-params,Mapping parameters>>
  54. See <<mapping>>.
  55. --
  56. `settings`::
  57. (Optional, <<index-modules-settings,index setting object>>) Configuration
  58. options for the index. See <<index-modules-settings>>.
  59. [[indices-create-api-example]]
  60. ==== {api-examples-title}
  61. [[create-index-settings]]
  62. ===== Index settings
  63. Each index created can have specific settings
  64. associated with it, defined in the body:
  65. [source,js]
  66. --------------------------------------------------
  67. PUT /twitter
  68. {
  69. "settings" : {
  70. "index" : {
  71. "number_of_shards" : 3, <1>
  72. "number_of_replicas" : 2 <2>
  73. }
  74. }
  75. }
  76. --------------------------------------------------
  77. // CONSOLE
  78. <1> Default for `number_of_shards` is 1
  79. <2> Default for `number_of_replicas` is 1 (ie one replica for each primary shard)
  80. or more simplified
  81. [source,js]
  82. --------------------------------------------------
  83. PUT /twitter
  84. {
  85. "settings" : {
  86. "number_of_shards" : 3,
  87. "number_of_replicas" : 2
  88. }
  89. }
  90. --------------------------------------------------
  91. // CONSOLE
  92. [NOTE]
  93. You do not have to explicitly specify `index` section inside the
  94. `settings` section.
  95. For more information regarding all the different index level settings
  96. that can be set when creating an index, please check the
  97. <<index-modules,index modules>> section.
  98. [[mappings]]
  99. ===== Mappings
  100. The create index API allows for providing a mapping definition:
  101. [source,js]
  102. --------------------------------------------------
  103. PUT /test
  104. {
  105. "settings" : {
  106. "number_of_shards" : 1
  107. },
  108. "mappings" : {
  109. "properties" : {
  110. "field1" : { "type" : "text" }
  111. }
  112. }
  113. }
  114. --------------------------------------------------
  115. // CONSOLE
  116. NOTE: Before 7.0.0, the 'mappings' definition used to include a type name. Although specifying
  117. types in requests is now deprecated, a type can still be provided if the request parameter
  118. include_type_name is set. For more details, please see <<removal-of-types>>.
  119. [[create-index-aliases]]
  120. ===== Aliases
  121. The create index API allows also to provide a set of <<indices-aliases,aliases>>:
  122. [source,js]
  123. --------------------------------------------------
  124. PUT /test
  125. {
  126. "aliases" : {
  127. "alias_1" : {},
  128. "alias_2" : {
  129. "filter" : {
  130. "term" : {"user" : "kimchy" }
  131. },
  132. "routing" : "kimchy"
  133. }
  134. }
  135. }
  136. --------------------------------------------------
  137. // CONSOLE
  138. [[create-index-wait-for-active-shards]]
  139. ===== Wait For active shards
  140. By default, index creation will only return a response to the client when the primary copies of
  141. each shard have been started, or the request times out. The index creation response will indicate
  142. what happened:
  143. [source,js]
  144. --------------------------------------------------
  145. {
  146. "acknowledged": true,
  147. "shards_acknowledged": true,
  148. "index": "test"
  149. }
  150. --------------------------------------------------
  151. // TESTRESPONSE
  152. `acknowledged` indicates whether the index was successfully created in the cluster, while
  153. `shards_acknowledged` indicates whether the requisite number of shard copies were started for
  154. each shard in the index before timing out. Note that it is still possible for either
  155. `acknowledged` or `shards_acknowledged` to be `false`, but the index creation was successful.
  156. These values simply indicate whether the operation completed before the timeout. If
  157. `acknowledged` is `false`, then we timed out before the cluster state was updated with the
  158. newly created index, but it probably will be created sometime soon. If `shards_acknowledged`
  159. is `false`, then we timed out before the requisite number of shards were started (by default
  160. just the primaries), even if the cluster state was successfully updated to reflect the newly
  161. created index (i.e. `acknowledged=true`).
  162. We can change the default of only waiting for the primary shards to start through the index
  163. setting `index.write.wait_for_active_shards` (note that changing this setting will also affect
  164. the `wait_for_active_shards` value on all subsequent write operations):
  165. [source,js]
  166. --------------------------------------------------
  167. PUT /test
  168. {
  169. "settings": {
  170. "index.write.wait_for_active_shards": "2"
  171. }
  172. }
  173. --------------------------------------------------
  174. // CONSOLE
  175. // TEST[skip:requires two nodes]
  176. or through the request parameter `wait_for_active_shards`:
  177. [source,js]
  178. --------------------------------------------------
  179. PUT /test?wait_for_active_shards=2
  180. --------------------------------------------------
  181. // CONSOLE
  182. // TEST[skip:requires two nodes]
  183. A detailed explanation of `wait_for_active_shards` and its possible values can be found
  184. <<index-wait-for-active-shards,here>>.