2013-08-28 19:24:34 -04:00
|
|
|
[[indices-create-index]]
|
2019-08-26 08:31:31 -04:00
|
|
|
=== Create index API
|
|
|
|
++++
|
|
|
|
<titleabbrev>Create index</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-08-26 08:31:31 -04:00
|
|
|
Creates a new index.
|
2018-05-25 10:21:09 -04:00
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2018-05-25 10:21:09 -04:00
|
|
|
--------------------------------------------------
|
2019-08-26 08:31:31 -04:00
|
|
|
PUT /twitter
|
2018-05-25 10:21:09 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
|
2019-08-26 08:31:31 -04:00
|
|
|
[[indices-create-api-request]]
|
|
|
|
==== {api-request-title}
|
|
|
|
|
|
|
|
`PUT /<index>`
|
|
|
|
|
|
|
|
[[indices-create-api-desc]]
|
|
|
|
==== {api-description-title}
|
|
|
|
You can use the create index API to add a new index to an {es} cluster. When
|
|
|
|
creating an index, you can specify the following:
|
|
|
|
|
|
|
|
* Settings for the index
|
|
|
|
* Mappings for fields in the index
|
|
|
|
* Index aliases
|
|
|
|
|
|
|
|
|
|
|
|
[[indices-create-api-path-params]]
|
|
|
|
==== {api-path-parms-title}
|
|
|
|
|
|
|
|
`<index>`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, string) Name of the index you wish to create.
|
|
|
|
|
2019-09-23 09:18:01 -04:00
|
|
|
// tag::index-name-reqs[]
|
2019-08-26 08:31:31 -04:00
|
|
|
Index names must meet the following criteria:
|
2018-05-25 10:21:09 -04:00
|
|
|
|
|
|
|
- Lowercase only
|
|
|
|
- Cannot include `\`, `/`, `*`, `?`, `"`, `<`, `>`, `|`, ` ` (space character), `,`, `#`
|
|
|
|
- Indices prior to 7.0 could contain a colon (`:`), but that's been deprecated and won't be supported in 7.0+
|
|
|
|
- Cannot start with `-`, `_`, `+`
|
2018-06-01 12:15:24 -04:00
|
|
|
- Cannot be `.` or `..`
|
2018-05-25 10:21:09 -04:00
|
|
|
- Cannot be longer than 255 bytes (note it is bytes, so multi-byte characters will count towards the 255 limit faster)
|
2019-09-23 09:18:01 -04:00
|
|
|
// end::index-name-reqs[]
|
2019-08-26 08:31:31 -04:00
|
|
|
--
|
|
|
|
|
|
|
|
|
|
|
|
[[indices-create-api-query-params]]
|
|
|
|
==== {api-query-parms-title}
|
2018-05-25 10:21:09 -04:00
|
|
|
|
2019-08-26 08:31:31 -04:00
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=include-type-name]
|
|
|
|
|
2019-08-29 12:56:10 -04:00
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=wait_for_active_shards]
|
2019-08-26 08:31:31 -04:00
|
|
|
|
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=timeoutparms]
|
|
|
|
|
|
|
|
|
|
|
|
[[indices-create-api-request-body]]
|
|
|
|
==== {api-request-body-title}
|
|
|
|
|
|
|
|
`aliases`::
|
|
|
|
(Optional, <<indices-aliases,alias object>>) Index aliases which include the
|
|
|
|
index. See <<indices-aliases>>.
|
|
|
|
|
2019-09-04 11:47:32 -04:00
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=mappings]
|
2019-08-26 08:31:31 -04:00
|
|
|
|
2019-09-04 11:47:32 -04:00
|
|
|
include::{docdir}/rest-api/common-parms.asciidoc[tag=settings]
|
2019-08-26 08:31:31 -04:00
|
|
|
|
|
|
|
[[indices-create-api-example]]
|
|
|
|
==== {api-examples-title}
|
2014-01-27 11:09:46 -05:00
|
|
|
|
|
|
|
[[create-index-settings]]
|
2019-08-26 08:31:31 -04:00
|
|
|
===== Index settings
|
2014-01-27 11:09:46 -05:00
|
|
|
|
|
|
|
Each index created can have specific settings
|
2018-05-25 10:21:09 -04:00
|
|
|
associated with it, defined in the body:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2019-08-26 08:31:31 -04:00
|
|
|
PUT /twitter
|
2016-08-16 06:20:07 -04:00
|
|
|
{
|
2016-04-29 12:07:23 -04:00
|
|
|
"settings" : {
|
|
|
|
"index" : {
|
2016-05-09 21:17:36 -04:00
|
|
|
"number_of_shards" : 3, <1>
|
2016-04-29 12:07:23 -04:00
|
|
|
"number_of_replicas" : 2 <2>
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 06:20:07 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2019-09-06 11:31:13 -04:00
|
|
|
|
2018-05-20 14:59:28 -04:00
|
|
|
<1> Default for `number_of_shards` is 1
|
2014-05-06 10:10:06 -04:00
|
|
|
<2> Default for `number_of_replicas` is 1 (ie one replica for each primary shard)
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
or more simplified
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2019-08-26 08:31:31 -04:00
|
|
|
PUT /twitter
|
2016-08-16 06:20:07 -04:00
|
|
|
{
|
2013-08-28 19:24:34 -04:00
|
|
|
"settings" : {
|
|
|
|
"number_of_shards" : 3,
|
|
|
|
"number_of_replicas" : 2
|
|
|
|
}
|
2016-08-16 06:20:07 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2014-02-19 11:49:38 -05:00
|
|
|
[NOTE]
|
|
|
|
You do not have to explicitly specify `index` section inside the
|
2014-01-27 11:09:46 -05:00
|
|
|
`settings` section.
|
|
|
|
|
|
|
|
For more information regarding all the different index level settings
|
|
|
|
that can be set when creating an index, please check the
|
|
|
|
<<index-modules,index modules>> section.
|
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[mappings]]
|
2019-08-26 08:31:31 -04:00
|
|
|
===== Mappings
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-01-14 16:08:01 -05:00
|
|
|
The create index API allows for providing a mapping definition:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2019-08-26 08:31:31 -04:00
|
|
|
PUT /test
|
2016-08-16 06:20:07 -04:00
|
|
|
{
|
2013-08-28 19:24:34 -04:00
|
|
|
"settings" : {
|
|
|
|
"number_of_shards" : 1
|
|
|
|
},
|
|
|
|
"mappings" : {
|
2019-01-14 16:08:01 -05:00
|
|
|
"properties" : {
|
|
|
|
"field1" : { "type" : "text" }
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-16 06:20:07 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2014-01-28 04:47:40 -05:00
|
|
|
|
2019-01-14 16:08:01 -05:00
|
|
|
NOTE: Before 7.0.0, the 'mappings' definition used to include a type name. Although specifying
|
|
|
|
types in requests is now deprecated, a type can still be provided if the request parameter
|
|
|
|
include_type_name is set. For more details, please see <<removal-of-types>>.
|
|
|
|
|
2014-01-28 04:47:40 -05:00
|
|
|
[[create-index-aliases]]
|
2019-08-26 08:31:31 -04:00
|
|
|
===== Aliases
|
2014-01-28 04:47:40 -05:00
|
|
|
|
|
|
|
The create index API allows also to provide a set of <<indices-aliases,aliases>>:
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2014-01-28 04:47:40 -05:00
|
|
|
--------------------------------------------------
|
2019-08-26 08:31:31 -04:00
|
|
|
PUT /test
|
2016-08-16 06:20:07 -04:00
|
|
|
{
|
2014-01-28 04:47:40 -05:00
|
|
|
"aliases" : {
|
|
|
|
"alias_1" : {},
|
|
|
|
"alias_2" : {
|
|
|
|
"filter" : {
|
|
|
|
"term" : {"user" : "kimchy" }
|
|
|
|
},
|
|
|
|
"routing" : "kimchy"
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 06:20:07 -04:00
|
|
|
}
|
2014-08-11 05:55:43 -04:00
|
|
|
--------------------------------------------------
|
2016-08-02 09:15:01 -04:00
|
|
|
|
|
|
|
[[create-index-wait-for-active-shards]]
|
2019-08-26 08:31:31 -04:00
|
|
|
===== Wait For active shards
|
2016-08-02 09:15:01 -04:00
|
|
|
|
2017-07-05 06:30:19 -04:00
|
|
|
By default, index creation will only return a response to the client when the primary copies of
|
2016-08-02 09:15:01 -04:00
|
|
|
each shard have been started, or the request times out. The index creation response will indicate
|
|
|
|
what happened:
|
|
|
|
|
2019-09-06 09:22:08 -04:00
|
|
|
[source,console-result]
|
2016-08-02 09:15:01 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"acknowledged": true,
|
2017-06-09 13:47:47 -04:00
|
|
|
"shards_acknowledged": true,
|
|
|
|
"index": "test"
|
2016-08-02 09:15:01 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2017-07-05 06:30:19 -04:00
|
|
|
`acknowledged` indicates whether the index was successfully created in the cluster, while
|
2017-01-31 06:52:39 -05:00
|
|
|
`shards_acknowledged` indicates whether the requisite number of shard copies were started for
|
2017-07-05 06:30:19 -04:00
|
|
|
each shard in the index before timing out. Note that it is still possible for either
|
|
|
|
`acknowledged` or `shards_acknowledged` to be `false`, but the index creation was successful.
|
|
|
|
These values simply indicate whether the operation completed before the timeout. If
|
2016-08-02 09:15:01 -04:00
|
|
|
`acknowledged` is `false`, then we timed out before the cluster state was updated with the
|
2017-07-05 06:30:19 -04:00
|
|
|
newly created index, but it probably will be created sometime soon. If `shards_acknowledged`
|
|
|
|
is `false`, then we timed out before the requisite number of shards were started (by default
|
|
|
|
just the primaries), even if the cluster state was successfully updated to reflect the newly
|
2016-08-02 09:15:01 -04:00
|
|
|
created index (i.e. `acknowledged=true`).
|
|
|
|
|
|
|
|
We can change the default of only waiting for the primary shards to start through the index
|
|
|
|
setting `index.write.wait_for_active_shards` (note that changing this setting will also affect
|
|
|
|
the `wait_for_active_shards` value on all subsequent write operations):
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2016-08-02 09:15:01 -04:00
|
|
|
--------------------------------------------------
|
2019-08-26 08:31:31 -04:00
|
|
|
PUT /test
|
2016-08-16 06:20:07 -04:00
|
|
|
{
|
2016-08-02 09:15:01 -04:00
|
|
|
"settings": {
|
|
|
|
"index.write.wait_for_active_shards": "2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
// TEST[skip:requires two nodes]
|
2016-08-02 09:15:01 -04:00
|
|
|
|
|
|
|
or through the request parameter `wait_for_active_shards`:
|
|
|
|
|
2019-09-06 11:31:13 -04:00
|
|
|
[source,console]
|
2016-08-02 09:15:01 -04:00
|
|
|
--------------------------------------------------
|
2019-08-26 08:31:31 -04:00
|
|
|
PUT /test?wait_for_active_shards=2
|
2016-08-02 09:15:01 -04:00
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
// TEST[skip:requires two nodes]
|
2016-08-02 09:15:01 -04:00
|
|
|
|
2017-07-05 06:30:19 -04:00
|
|
|
A detailed explanation of `wait_for_active_shards` and its possible values can be found
|
2016-08-02 09:15:01 -04:00
|
|
|
<<index-wait-for-active-shards,here>>.
|