Details on index creation

Automatic index creation

If you specify an index that does not already exists in the Elasticsearch server, it will be created automatically by the server. In that case you have no control over the parameters of the index and the document field mapping. There are several defaults that will be used for index creation, for details check the Elasticsearch documentation, here: Create index The type mapping of document fields for new indices is determined by the 'dynamic mapping' rules of Elasticsearch. This feature is described in the documentation, for example here: Dynamic Mapping

The dynamic mapping may not automatically recognise the desired data type for a field, for example the Elasticsearch geo_point type is not automatically detected, when you send a string field formatted as geo_point string. In that case the geo_point will appear as normal text string in the Elasticsearch index, and may limit your ability to query the index.

To take full control about the indices your application is using, it is recomended to manually create them before usage

Manual index creation

If you manually create the index before usage, you can specify all necessary parameters of the index. So you do not have to rely on the automatic index creation of the Elasticsearch server.

Most likely you want to set these parameters
  • The number of shards for the index
  • The number of replicas for the index
  • The type mapping of the index

The following example shows a curl command to create an index named 'test' with 2 shards and one replica. The schema for the index contains one field named 'locationName' of type text and one field named 'location' of type geo_point'.


curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "number_of_shards" : 2,
        "number_of_replicas" : 1
    },
    "mappings" : {
        "_doc" : {
            "properties" : {
                "locationName" : { "type" : "text" },
                "location" : { "type" : "geo_point" }
            }
        }
    }
}'