2.X/1. Getting Started

1-10-08. Dynamic Mapping

drscg 2017. 9. 30. 17:05

When Elasticsearch encounters a previously unknown field in a document, it uses dynamic mappingto determine the datatype for the field and automatically adds the new field to the type mapping.

Elasticsearch가 document에서 이전에 알려지지 않은 field를 만난 경우, field의 데이터 type을 결정하고, 자동으로 type mapping에 새로운 field를 추가하기 위해, dynamic mapping을 사용한다.

Sometimes this is the desired behavior and sometimes it isn’t. Perhaps you don’t know what fields will be added to your documents later, but you want them to be indexed automatically. Perhaps you just want to ignore them. Or—especially if you are using Elasticsearch as a primary data store—perhaps you want unknown fields to throw an exception to alert you to the problem.

가끔은 이것이 바라던 대로 동작하지만, 때로는 그렇지 않다. 아마도, 나중에는 document에 무슨 field가 추가될지는 알 수 없을 것이다. 하지만 그것이 자동으로 색인되기를 원할 것이다. 그것이 무시되거나, 특히 Elasticsearch를 기본 데이터 저장소로 사용하고 있는 경우에는, 문제를 알려주기 위해, 알려지지 않은 field라는 예외(exception)를 던지기를 바랄 것이다.

Fortunately, you can control this behavior with the dynamic setting, which accepts the following options:

다행히도, 아래 옵션을 가진, dynamic 설정을 통해, 이런 동작을 제어할 수 있다.

true

Add new fields dynamically—the default

새로운 field를 동적으로 추가한다. default

false

Ignore new fields

새로운 field를 무시한다.

strict

Throw an exception if an unknown field is encountered

알려지지 않은 field를 만나면, 예외를 던진다.

The dynamic setting may be applied to the root object or to any field of type object. You could set dynamic to strict by default, but enable it just for a specific inner object:

dynamic 설정은 root 오브젝트나 type이 object 인 field에 적용될 수 있다. 기본적으로 dynamic 에 strict 를 설정할 수 있다. 그러나, 특정 inner 오브젝트에게만 활성화 할 수 있다.

PUT /my_index
{
    "mappings": {
        "my_type": {
            "dynamic":      "strict", 
            "properties": {
                "title":  { "type": "string"},
                "stash":  {
                    "type":     "object",
                    "dynamic":  true 
                }
            }
        }
    }
}

my_type 오브젝트는 알려지지 않은 field를 만나면 예외를 던진다.

stash 오브젝트는 새로운 field를 동적으로 생성한다.

With this mapping, you can add new searchable fields into the stash object:

이 mapping으로, stash 오브젝트에, 새로운 검색 가능한 filed를 추가할 수 있다.

PUT /my_index/my_type/1
{
    "title":   "This doc adds a new field",
    "stash": { "new_field": "Success!" }
}

But trying to do the same at the top level will fail:

그러나 최상위 단계에서 동일하게 하면, 실패할 것이다.

PUT /my_index/my_type/1
{
    "title":     "This throws a StrictDynamicMappingException",
    "new_field": "Fail!"
}
Note

Setting dynamic to false doesn’t alter the contents of the _source field at all. The _source will still contain the whole JSON document that you indexed. However, any unknown fields will not be added to the mapping and will not be searchable.

dynamic 을 false 로 설정해도, _source field의 내용은 전혀 변경되지 않는다. _source 는 여전히 색인된, 전체 JSON document를 가지고 있을 것이다. 그러나, 어떤 알려지지 않은 field도, mapping에 추가되지 않을 것이고, 검색할 수 없을 것이다.


'2.X > 1. Getting Started' 카테고리의 다른 글

1-10-06. Types and Mappings  (0) 2017.09.30
1-10-07. The Root Object  (0) 2017.09.30
1-10-09. Customizing Dynamic Mapping  (0) 2017.09.30
1-10-10. Default Mapping  (0) 2017.09.30
1-10-11. Reindexing Your Data  (0) 2017.09.30