create-index.asciidoc 7.2 KB

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