Introduction to Mappings in Elasticsearch

Mapping defines the types, formats etc. for different fields in an Elasticsearch document. Mapping also defines various ways in which fields / types behave in different situations (e.g. dynamic mapping allows creation of types dynamically). Mpping may also denote how a document’s metadata associated (e.g. _index, _type, _id, and _source ) is treated. 

Mappings are defined for each type and with ES 6.2 there can be only one type per index. Therefore mappings are defined per index.

 

Field Types

Field datatypes can be:

  • a simple type like text, keyword, date, long, double, boolean or ip.

  • a type which supports the hierarchical nature of JSON such as object or nested.

  • or a specialised type like geo_point, geo_shape, or completion.

Example:

PUT index1/_mapping/_doc 
{
  "properties": {
    "name": {
      "type": "text"
    }
  }
}

 

You can create sub fields with different types for the same field:

PUT index1/_mapping/_doc
{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "keyword_typed" : {
          "type" : "keyword"
        }
      }
    }
  }
}

Note

  1. You then refer to the field as name.keyword_typed. Usually it is named as keyword itself, but I have used a different name to denote that it can be any name.

  2. Keyword fields are similar to text, but are not analyzed and hence only searchable by their exact value. It is generally used for structured content such as email addresses, hostnames, status codes, zip codes or tags.

 

Defining Mappings

Dynamic Mapping

With dynamic mapping (enabled by defaut) we do not have to pre-define mappings, Elasticsearch will guess the most appropriate mapping type / types for your fields.

By default, for text fields, elasticsearch assigns "text" type, but also add a sub field called "keyword" of type "keyword".

See examples for dynamic mapping here.

Explicit Mapping

We can define our own mappings either while creating an index, or you can add fields to an existing index with the PUT mapping API

See examples for explicit mapping here.

 

Important Points on Mappings

  1. Existing field mappings cannot be updated in general. Changing the mapping would mean invalidating already indexed documents. Instead, you should create a new index with the correct mappings and reindex your data into that index.

    1. You can add mappings for new fields that does not already have mappings.

    2. You can add sub fields for existing fields with different types.

  2.  The following settings allow you to limit the number of field mappings that can be created manually or dynamically:

    1. index.mapping.total_fields.limit

    2. index.mapping.depth.limit

    3. index.mapping.nested_fields.limit

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