Mapping

이상민·2021년 5월 11일
0
post-thumbnail

defines structure of docs and how values are indexes

--> similar to schema in RDB

  1. explicit mapping : defined by developer

  2. 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
  1. date without time
  2. date with time
  3. 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
profile
편하게 읽기 좋은 단위의 포스트를 추구하는 개발자입니다

0개의 댓글