[Recipes] Understanding Dynamic Mappings in Elasticsearch

Problem: 

Demonstrate the use of different values for dynamic mappings: true, false, strict.

Solution Summary: 

With dynamic mappings enabled (true), elasticsearch will automatically assign types to our fields. Dynamic mapping is enabled by default. 

Setting the dynamic parameter to false ignorea new fields. 

When dynamic mapping is set to strict, it will throw an exception if an unknown field is encountered.

Dynamic mapping can be set, both at the document and at the object level.

Prerequisites: 

Working Elasticsearch cluster and Kibana.

Delete employee_mappingdemo if already exist:

DELETE /employee_mappingdemo

Solution Steps: 

Initial Document

PUT /employee_mappingdemo/_doc/1?pretty
{
  "name": {
    "first":"Heartin"
  }
}

 

Check the current mapping:

GET employee_mappingdemo/_mapping

Response (Current Mapping):

{
  "employee_mappingdemo": {
    "mappings": {
      "_doc": {
        "properties": {
          "name": {
            "properties": {
              "first": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

 

Adding a document with new field (dynamic is true)

PUT /employee_mappingdemo/_doc/2?pretty
{
  "name": {
    "first":"Heartin",
    "last": "Kanikathottu"
  },
  "phone": "9999999999"
}

Note: If you verify the mapping, you see that mapping for field "last" and "phone" are added.

 

Disabling Dynamic Mapping (dynamic = false)

Disable dynamic mapping as:

PUT /employee_mappingdemo/_mapping/_doc
{
  "dynamic": "false"
}

Verify mapping to see dynamic is set to false.

 

Now execute below PUT request with new "middle" field and "address" field:

PUT /employee_mappingdemo/_doc/3?pretty
{
  "name": {
    "first":"Heartin",
    "middle": "Jacob",
    "last": "Kanikathottu"
  },
  "address": "Bangalore, India"
}

Note:

  1. There will be no error.

  2. No mapping is created for "middle" field and "address" field. Verify the mappings.

  3. Data for "middle" field and "address" field are still part of the document. Verify using a GET request.

  4. However, we will not be able to use them in queries (e.g. match query). 

 

Strict Dynamic Mapping (dynamic = strict)

Set strict dynamic mapping as:

PUT /employee_mappingdemo/_mapping/_doc
{
  "dynamic": "strict"
}

Verify mapping to see dynamic is set to strict.

 

Now execute below PUT request with new "middle" field and "address" field:

PUT /employee_mappingdemo/_doc/4?pretty
{
  "name": {
    "first":"Heartin",
    "middle": "Jacob",
    "last": "Kanikathottu"
  },
  "address": "Bangalore, India"
}

Note:

  1. There will be no error: strict_dynamic_mapping_exception: mapping set to strict, dynamic introduction of [middle] within [name] is not allowed.

  2. No mapping is created for "middle" field and "address" field. Verify the mappings.

  3. Document is also not created. Verify using a GET request.

 

Overriding mapping for Nested Objects

Set document level mapping to false and nested object level mapping to true:

PUT /employee_mappingdemo/_mapping/_doc
{
  "dynamic": "false",
  "properties": {
    "name": {
      "dynamic": "true",
      "properties": {
      }
    }
  }
}

 

Now execute below PUT request with new "middle" field and "address" field:

PUT /employee_mappingdemo/_doc/5?pretty
{
  "name": {
    "first":"Heartin",
    "middle": "Jacob",
    "last": "Kanikathottu"
  },
  "address": "Bangalore, India"
}

Note:

  1. There will be no errors.

  2. Mapping will be created for "middle" field, but NOT for "address field". Verify the mappings.

  3. Data for "middle" field and "address" field are part of the document. Verify using a GET request.

  4. However, we will not be able to use "address" in queries, but only "middle" (e.g. match query). 

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream