create-index.asciidoc 7.4 KB

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