2.X/1. Getting Started

1-10-09. Customizing Dynamic Mapping

drscg 2017. 9. 30. 17:01

If you know that you are going to be adding new fields on the fly, you probably want to leave dynamic mapping enabled. At times, though, the dynamic mapping "rules" can be a bit blunt. Fortunately, there are settings that you can use to customize these rules to better suit your data.

새로운 field를 추가하려는 것을, 지금 알고 있다면, dynamic mapping을 비활성화하고 싶을 것이다. 때때로, 동적 mapping "규칙" 은 조금 무딜 수 있다. 다행히도, 데이터에 더 적합한 규칙을 사용자 정의할 수 있다.

date_detectionedit

When Elasticsearch encounters a new string field, it checks to see if the string contains a recognizable date, like 2014-01-01. If it looks like a date, the field is added as type date. Otherwise, it is added as type string.

Elasticsearch가 새로운 문자열 field를 만났을 때, 문자열이 2014-01-01 처럼 인식할 수 있는 날짜가 포함되어 있는지를 확인한다. 날짜처럼 보인다면, 그 field는 date type으로 추가된다. 그렇지 않으면, stringtype으로 추가된다.

Sometimes this behavior can lead to problems. Imagine that you index a document like this:

가끔, 이런 동작은 문제를 일으킨다. 아래와 같은 document를 색인 한다고 가정해 보자.

{ "note": "2014-01-01" }

Assuming that this is the first time that the note field has been seen, it will be added as a date field. But what if the next document looks like this:

note field가 나타난 것이 처음이라고 가정하면, date field로 추가될 것이다. 그런데 다음 document가 다음과 같다면,

{ "note": "Logged out" }

This clearly isn’t a date, but it is too late. The field is already a date field and so this "malformed date" will cause an exception to be thrown.

이것은 분명히 날짜가 아니다. 하지만, 이미 늦었다. filed는 이미 date field이고, "잘못된 날짜(malformed date)" 라고 예외를 던질 것이다.

Date detection can be turned off by setting date_detection to false on the root object:

date detection은 root 오브젝트에서 date_detection 을 false 로 설정하여, 비활성화할 수 있다.

PUT /my_index
{
    "mappings": {
        "my_type": {
            "date_detection": false
        }
    }
}

With this mapping in place, a string will always be a string. If you need a date field, you have to add it manually.

이 mapping이 있으면, 문자열은 항상 string 이 된다. 만약 date field가 필요하면, 수동으로 추가해야 한다.

Note

Elasticsearch’s idea of which strings look like dates can be altered with the dynamic_date_formats setting.

날짜처럼 보이는 문자열은 dynamic_date_formats 설정으로 변경할 수 있다.

dynamic_templatesedit

With dynamic_templates, you can take complete control over the mapping that is generated for newly detected fields. You can even apply a different mapping depending on the field name or datatype.

dynamic_templates 으로, 새로 발견되는 field에 의해 생성된, mapping을 완벽하게 제어할 수 있다. 심지어, field 이름이나 데이터 type에 따라, 다른 mapping을 적용할 수 있다.

Each template has a name, which you can use to describe what the template does, a mapping to specify the mapping that should be applied, and at least one parameter (such as match) to define which fields the template should apply to.

각 template은 template의 동작을 설명하는 데 사용할 수 있는 이름, 적용할 mapping을 지정하기 위한 mapping, template을 적용할 field를 정의하기 위해, 최소 하나 이상의 매개변수(match 같은)를 가지고 있다.

Templates are checked in order; the first template that matches is applied. For instance, we could specify two templates for string fields:

template은 순서대로 확인된다. 일치하는 첫 번째 template이 적용된다. 예를 들어, string field에 대해, 두 개의 template을 지정할 수 있다.

  • es: Field names ending in _es should use the spanish analyzer.

    es: field 이름이 _es 로 끝나면, spanish analyzer를 사용.

  • en: All others should use the english analyzer.

    en: 다른 모든 field는 english analyzer를 사용.

We put the es template first, because it is more specific than the catchall en template, which matches all string fields:

es template이, 모든 string field에 일치하는 범용 en template보다 더 명확하기 때문에, 처음에 두었다.

PUT /my_index
{
    "mappings": {
        "my_type": {
            "dynamic_templates": [
                { "es": {
                      "match":              "*_es", 
                      "match_mapping_type": "string",
                      "mapping": {
                          "type":           "string",
                          "analyzer":       "spanish"
                      }
                }},
                { "en": {
                      "match":              "*", 
                      "match_mapping_type": "string",
                      "mapping": {
                          "type":           "string",
                          "analyzer":       "english"
                      }
                }}
            ]
}}}

이름이 _es 로 끝나는 string field에 일치

모든 다른 string field에 일치

The match_mapping_type allows you to apply the template only to fields of the specified type, as detected by the standard dynamic mapping rules, (for example string or long).

match_mapping_type 은 string, long 같은, 표준 동적 mapping 규칙에 의해 발견되는, 지정된 type의 field에게만 template이 적용되도록 한다.

The match parameter matches just the field name, and the path_match parameter matches the full path to a field in an object, so the pattern address.*.name would match a field like this:

match 매개변수는 field 이름으로만 일치하고, path_match 매개변수는 오브젝트에 있는 field의 전체 경로(full path)와 일치한다. 때문에, 아래 예처럼 address.*.name 과 같은 pattern은, 이처럼 특정 field에 일치한다.

{
    "address": {
        "city": {
            "name": "New York"
        }
    }
}

The unmatch and path_unmatch patterns can be used to exclude fields that would otherwise match.

unmath 와 path_unmatch pattern은 일치하지 않는 field를 제외하는데 사용될 수 있다.

More configuration options can be found in the dynamic mapping documentation.

더 많은 설정 옵션은 dynamic mapping documentation에서 확인할 수 있다.


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

1-10-07. The Root Object  (0) 2017.09.30
1-10-08. 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
1-10-12. Index Aliases and Zero Downtime  (0) 2017.09.30