Mapping


defines structure of docs and how values are indexes
--> similar to schema in RDB

  • explicit mapping : defined by developer

  • dynamic mapping : auto generate field mapping
  • combining two are possible
  • 1. Explicit Mapping

  • field mappings can be created when creating an index or afterwards
  • PUT /reviews
    {
        "mappings": {
            "properties": {
                "rating": {"type": "float"},
                "content": {"type": "text"},
                "product_id": {"type": "integer"},
                "author": {
                    "properties": {
                        "first_name": {"type": "text"},
                        "last_name": {"type": "text"},
                        "email": {"type": "keyword"}
                    }
                }
            }
        }
    }

    2. Retrieving Mapping

  • for entire index
  • GET /reviews/_mapping
  • for specific field
  • GET /reviews/_mapping/field/content
    
    // for field in object
    GET /reviews/_mapping/field/author.email

    3. Using dot notation in field names

    PUT /reviews
    {
        "mappings": {
            "properties": {
                "rating": {"type": "float"},
                "content": {"type": "text"},
                "product_id": {"type": "integer"},
                "author.first_name": {"type": "text"}
                "author.last_name": {"type": "text"}
                "author.email": {"type": "keyboard"}
            }
        }
    }
  • dot notation can be used insted of nesting for better viewability
  • 4. Adding Mappings to Existing Indexes

    // add time stamp to reviews index
    PUT /reviews/_mapping
    {
        "properties": {
            "created_at": {"type": "date"}
        }
    }

    Date data type

  • specifying date
  • formatted string
  • milliseconds since the epoch (long)
  • seconds since the epoch (int)
  • custom date format
  • epoch = 1/1/1970

    default behavior of date fields

  • Accepted formats
  • date without time
  • date with time
  • milliseconds since epoch (long)
  • UTC assumed
  • Date must be formatted according to ISO 8601 spec
  • how date fields are stored

  • stored internally as milliseconds since epoch
  • PUT /reviews/_doc/3
    {
        "created_at": "2015-04-15T13:07:41Z"
    }
  • how missing fields are handled
  • all fields are optional
  • unlike RDB where NULL allow should be explicitly set
  • integrity checks should be done at application level