How to do it...

The HTTP method to create an index is PUT (but POST also works); the REST URL contains the index name:

http://<server>/<index_name>

To create an index, we must perform the following steps:

  1. From the command line, we can execute a PUT call:
PUT /myindex
{
"settings": {
"index": {
"number_of_shards": 2,
"number_of_replicas": 1
}
}
}
  1. The result returned by Elasticsearch should be like the following:
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "myindex"
}
  1. If the index already exists, a 400 error will be returned:
{
"error": {
"root_cause": [
{
"type": "resource_already_exists_exception",
"reason": "index [myindex/xaXAnnwcTUiTePcKGWJw3Q] already exists",
"index_uuid": "xaXAnnwcTUiTePcKGWJw3Q",
"index": "myindex"
}
],
"type": "resource_already_exists_exception",
"reason": "index [myindex/xaXAnnwcTUiTePcKGWJw3Q] already exists",
"index_uuid": "xaXAnnwcTUiTePcKGWJw3Q",
"index": "myindex"
},
"status": 400
}