2013-08-28 19:24:34 -04:00
|
|
|
[[indices-create-index]]
|
|
|
|
== Create Index
|
|
|
|
|
2014-01-06 15:58:46 -05:00
|
|
|
The create index API allows to instantiate an index. Elasticsearch
|
2013-08-28 19:24:34 -04:00
|
|
|
provides support for multiple indices, including executing operations
|
2014-01-27 11:09:46 -05:00
|
|
|
across several indices.
|
|
|
|
|
|
|
|
[float]
|
|
|
|
[[create-index-settings]]
|
|
|
|
=== Index Settings
|
|
|
|
|
|
|
|
Each index created can have specific settings
|
2013-08-28 19:24:34 -04:00
|
|
|
associated with it.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
PUT twitter
|
|
|
|
{
|
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
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
// CONSOLE
|
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
|
|
|
|
|
|
|
The above second curl example shows how an index called `twitter` can be
|
|
|
|
created with specific settings for it using http://www.yaml.org[YAML].
|
|
|
|
In this case, creating an index with 3 shards, each with 2 replicas. The
|
|
|
|
index settings can also be defined with http://www.json.org[JSON]:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
PUT twitter
|
|
|
|
{
|
2013-08-28 19:24:34 -04:00
|
|
|
"settings" : {
|
|
|
|
"index" : {
|
|
|
|
"number_of_shards" : 3,
|
|
|
|
"number_of_replicas" : 2
|
|
|
|
}
|
|
|
|
}
|
2016-08-16 06:20:07 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
// CONSOLE
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
or more simplified
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
PUT twitter
|
|
|
|
{
|
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
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
// CONSOLE
|
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-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[float]
|
2013-09-25 12:17:40 -04:00
|
|
|
[[mappings]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== Mappings
|
|
|
|
|
2017-07-05 06:30:19 -04:00
|
|
|
The create index API allows to provide a type mapping:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
PUT test
|
|
|
|
{
|
2013-08-28 19:24:34 -04:00
|
|
|
"settings" : {
|
|
|
|
"number_of_shards" : 1
|
|
|
|
},
|
|
|
|
"mappings" : {
|
|
|
|
"type1" : {
|
|
|
|
"properties" : {
|
2016-03-18 12:01:27 -04:00
|
|
|
"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
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
// CONSOLE
|
2014-01-28 04:47:40 -05:00
|
|
|
|
|
|
|
[float]
|
|
|
|
[[create-index-aliases]]
|
|
|
|
=== Aliases
|
|
|
|
|
|
|
|
The create index API allows also to provide a set of <<indices-aliases,aliases>>:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
PUT test
|
|
|
|
{
|
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-16 06:20:07 -04:00
|
|
|
// CONSOLE
|
2016-08-02 09:15:01 -04:00
|
|
|
|
|
|
|
[float]
|
|
|
|
[[create-index-wait-for-active-shards]]
|
|
|
|
=== Wait For Active Shards
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"acknowledged": true,
|
2017-06-09 13:47:47 -04:00
|
|
|
"shards_acknowledged": true,
|
|
|
|
"index": "test"
|
2016-08-02 09:15:01 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
// TESTRESPONSE
|
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):
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
PUT test
|
|
|
|
{
|
2016-08-02 09:15:01 -04:00
|
|
|
"settings": {
|
|
|
|
"index.write.wait_for_active_shards": "2"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TEST[skip:requires two nodes]
|
2016-08-02 09:15:01 -04:00
|
|
|
|
|
|
|
or through the request parameter `wait_for_active_shards`:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-08-16 06:20:07 -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
|
|
|
// CONSOLE
|
|
|
|
// 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>>.
|
2018-04-11 09:54:16 -04:00
|
|
|
|
|
|
|
[float]
|
|
|
|
=== Skipping types
|
|
|
|
|
|
|
|
Types are scheduled to be fully removed in Elasticsearch 8.0 and will not appear
|
|
|
|
in requests or responses anymore. You can opt in for this future behaviour by
|
|
|
|
setting `include_type_name=false` and putting mappings directly under `mappings`
|
|
|
|
in the index creation call.
|
|
|
|
|
|
|
|
Here is an example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
PUT test?include_type_name=false
|
|
|
|
{
|
|
|
|
"mappings": {
|
|
|
|
"properties": {
|
|
|
|
"foo": {
|
|
|
|
"type": "keyword"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|