[Recipes] Mappings of Elasticsearch Documents - Viewing, Adding, Updating

Problem: 

Need to view existing mappings for an index. Need to add mappings to existing mappings. Update mappings for existing fields.

Solution Summary: 

We can use _mapping endpoint to see current mappings. 

Prerequisites: 

Working elasticsearch cluster and Kibana.

Solution Steps: 

Initial Test Data

You may create an index employee with a document if you are not following along:

PUT /employee/_doc/1?pretty
{
  "name": "Heartin",
  "salary": 10000000
}

 

Viewing current mappings

GET employee/_doc/_mapping

Response:

{
  "employee": {
    "mappings": {
      "_doc": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "salary": {
            "type": "long"
          }
        }
      }
    }
  }
}

 

Adding New Mappings

PUT employee/_doc/_mapping
{
  "properties": {
    "certifications": {
      "type": "integer"
    }
  }
}

 

Updating Existing Mappings

Try changing the type for Salary to "double" from "float".

PUT employee/_doc/_mapping
{
  "properties": {
    "salary": {
      "type": "double"
    }
  }
}

 

Error Response:

...

     {
        "type": "illegal_argument_exception",
        "reason": "mapper [salary] cannot be changed from type [long] to [double]"
      }

...

Note: You cannot change existing mappings. May delete index and reindex. However, you can add additional mappings to existing fields (E.g. adding a keyword type to a text field).

 

TODO

  1. Create an index with a text field. Add a keyword type to that text field.

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