[Elasticsearch] Dynamic Templates 간단 정리

Elastic/Elasticsearch 2020. 9. 10. 12:18

Elasticsearch Reference 를 그대로 의역한 수준 이라고 보시면 될것 같습니다.

 

[참고문서]

www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html

www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html

 

match_mapping_type)
JSON 파서를 통해서 data type 을 detection 합니다.
long, double 의 경우 integer 와 float 에 대한 정확한 detection 이 어렵기 때문에 항상 long 과 double 로 detection 하게 됩니다.

detection type 은 아래와 같습니다.
- boolean
- date
- double
- long
- object
- string

[Code Snippet from Elastic]
- long type 으로 matching 된 것을 모두 integer 로 설정 하는 것과
- string type 으로 matching 된 것을 모두 text 와 fields 설정을 이용해서 keyword 로 설정 하는 것입니다.

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type":  "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    ]
  }
}


match and unmatch)
이 설정은 field명에 대한 패턴 매칭을 이용해서 data type 을 detection 합니다.
unmatch 패턴은 제외 시키기 위한 설정 이라고 생각 하면 됩니다.

[Code Snippet from Elastic]
- data type 이 string 으로 매칭 된 경우 필드명을 통해 최종 설정을 하는 것입니다.
- 필드명을 통해서 data type 을 설정 하는 것입니다.

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "longs_as_strings": {
          "match_mapping_type": "string",
          "match":   "long_*",
          "unmatch": "*_text",
          "mapping": {
            "type": "long"
          }
        }
      }
    ]
  }
}


match_pattern)
이 설정은 위에서 "match":   "long_*" 과 같은 wildcard 패턴을 java 정규식으로 사용하기 위한 설정입니다.

[Code Snippet from Elastic]

  "match_pattern": "regex",
  "match": "^profit_\d+$"


path_match and path_unmatch)
이 설정은 nested 나 object field 에 대한 match, unmatch 와 동일한 설정 입니다.

% Nested field type
The nested type is a specialised version of the object data type 
that allows arrays of objects to be indexed in a way 
that they can be queried independently of each other.

{name} and {dynamic_type}
이 설정은 field명을 {name} 으로 사용하고 matching 된 data type 을 {dynamic_type} 으로 사용하는 설정입니다.

[Code Snippet from Elastic]
- analyzer 로 english analyer 를 사용 하겠다는 의미 입니다. {name} == "english"
- {dynamic_type} == long 으로 매칭이 되었고 doc_values 를 사용하지 않겠다는 의미 입니다.

PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "named_analyzers": {
          "match_mapping_type": "string",
          "match": "*",
          "mapping": {
            "type": "text",
            "analyzer": "{name}"
          }
        }
      },
      {
        "no_doc_values": {
          "match_mapping_type":"*",
          "mapping": {
            "type": "{dynamic_type}",
            "doc_values": false
          }
        }
      }
    ]
  }
}

PUT my-index-000001/_doc/1
{
  "english": "Some English text", 
  "count":   5 
}

지금까지 보셨다면 위 설정은 Index 에 직접 설정 한다는 것을 아실 수 있습니다.

하지만 Elasticsearch 에서는 별도 Index Template 이라는 것을 제공 하고 있습니다.

 

[참고문서]

www.elastic.co/guide/en/elasticsearch/reference/7.9/index-templates.html

www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-templates-v1.html

www.elastic.co/guide/en/elasticsearch/reference/7.9/indices-component-template.html

 

이 index template 설정은 최신 버전으로 넘어 오면서 두 가지 타입이 존재 합니다.

- _template 에 설정

PUT /_template/<index-template>

 

- _component_template 에 설정
PUT /_component_template/<component-template>

 

이 설정이 dynamic template 과의 차이는 index 에 직접 하느냐 cluster 에 하느냐의 차이 입니다.

더불어 dynamic template 은 mapping 에 대한 내용이지만 index template 은 setting, mapping 등 모두 포함 하고 있습니다.

중요한 내용이니 꼭 인지 하시기 바랍니다.
더불어 주의 하셔야 하는 점은 이런 템플릿 설정은 인덱스가 생성 되는 시점에 적용이 되는 것이기 때문에,

이미 생성된 인덱스에는 영향을 끼치지 못한다는 것도 알아 두셔야 합니다.

 

[Code Example]

- 아래 template 과 dynamic mapping 을 함께 설정한 예제 입니다.

- 기존 예제만 함쳐 놓은 것이기 때문에 구성에 맞게 고쳐서 사용 하시면 됩니다.

PUT /_template/template_1
{
  "index_patterns" : ["te*"],
  "order" : 0,
  "settings" : {
    "number_of_shards" : 1
  },
  "mappings" : {
    "_source" : { "enabled" : false },
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type":  "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    ]
  }
}

 

: