elasticsearch에서는 template을 사용하면 편하게 색인을 생성할 수 있는데 이 종류도 두가지가 있다.
mappings와 settings 을 구성할 수 있다.PUT _template/my_template
{
"index_patterns": ["my_index-*"],
"mappings": {
"properties": {
"timestamp": {
"type": "date"
}
}
},
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
}
}
mappings, settings, aliases 등의 섹션을 포함할 수 있다.ilm 을 적용해서 색인을 관리하기도 좋다.PUT _index_template/my_index_template
{
"index_patterns": [
"my_index-*"
],
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"dynamic_templates": [],
"properties": {
"timestamp": {
"type": "date"
}
}
}
}
}
dynamic_templates 부분으로 새로운 필드가 추가될 때 자동으로 특정 유형으로 매핑하도록 설정할 수 있다.dynamic_templates 설정 예시PUT _index_template/my_index_template
{
"index_patterns": [
"my_index-*"
],
"template": {
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"dynamic_templates": [
{
"dates": {
"match": "*date*", // 필드 이름에 "date"가 포함된 경우
"match_mapping_type": "string", // 문자열 타입에 대해
"mapping": {
"type": "date"
}
}
},
{
"strings": {
"match_mapping_type": "string", // 문자열 타입
"mapping": {
"type": "text" // 기본 텍스트 필드로 매핑
}
}
}
],
"properties": {
"timestamp": {
"type": "date"
}
}
}
}
}