Sep 8, 2018

Elasticsearch Index settings and Mappings: How to create Index mapping and settings (Elasticsearch and Kibana Devtool)

For illustration purpose we generally index document and elasticsearch(ELS) does settings and mappings creation for us. But in production environment we generally first create Index's settings and mapping. In ELS mapping has significance however in ELS 7 mapping will be irrelevant as Type will be decommissioned.
In this post we will see how to create Index settings and mapping in strict mode before creating very first document. Settings node primarily specify number of shards & number of replica. Mapping specify data types of document and analyzers details.

What is strict mode mapping ?
By default elasticsearch allows incremental change in document with new fields other than fields specified initially while setting up mapping node. i.e: "dynamic" : "false". Elasticsearch figure out data type dynamically and add it in mapping node.
In order to restrict specific set of fields in doc specified in mapping, we have to use
"dynamic" : "strict". Below PUT command creates mapping with dynamic = strict (highlighted in yellow)
ELS has two values for dynamic field.
dynamic : false - indexing field will be ignored
dynamic : strict  - indexing field will throw error

Create Index mapping and settings : Below PUT command creates index customers with settings and mapping details. By default ELS setup number of shards = 5, we have overwritten it to 2.
In mapping nodes we have to specify list of fields and analyzer details(fields where we want analysis to be carried out). name is of type text and we instruct to use standard analyzer for it.
Note:- When we have to use custom analyzer for analysis, custom analyzer is declared under settings node.

PUT /customers
{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 1
  },
  "mappings": {
    "online" :{
      "dynamic": "strict",
      "properties":{
        "gender" :{
          "type": "text",
          "analyzer" : "standard"
        },
        "age":{
            "type": "integer"
        },
        "total_spent":{
           "type": "float"
        },
        "is_new":{
           "type" : "boolean"
        },
        "name":{
           "type": "text",
           "analyzer" : "standard"
        },
        "address": {
          "type" : "text",
          "fields":{
            "keyword":{
              "type" : "keyword",
              "ignore_above":  256
            }
          }
        }
      }
    }
  }
}
Show response

Index customers document of type Online: Index document successfully as it followed mapping specs (all document fields are specified in mapping)
PUT /customers/online/176
{  
        "gender" : "male",
        "age":25,
        "total_spent":1298.69,
        "is_new":false,
        "name": "Nikhil",
        "address" : "Windsor prime, Bangalore"
}
Show response

Index customers document with additional fields : Here we will add locale field in document and expect elastic search will not index document successfully, it should throw error - strict_dynamic_mapping_exception. Remember "dynamic" : "strict" will enforce indexing to follow mapping specs.
PUT /customers/online/176
{  
        "gender" : "male",
        "age":25,
        "total_spent":1298.69,
        "is_new":false,
        "name": "Nikhil",
        "address" : "Windsor prime, Bangalore",
        "locale" : "en-US"
}
Show response

How to add restriction on existing index type ? Using _mapping with PUT command we can change dynamic value of a given type.
PUT /customers/_mapping/online
{
    "dynamic": "strict"
}
Show response

Location: Bengaluru, Karnataka, India