[elasticsearch] index template 생성하기 및 종류

HI·2024년 10월 24일

elasticsearch에서는 template을 사용하면 편하게 색인을 생성할 수 있는데 이 종류도 두가지가 있다.

1. Legacy Index Templates

  • mappingssettings 을 구성할 수 있다.
  • 생성 쿼리
PUT _template/my_template
{
  "index_patterns": ["my_index-*"],
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "date"
      }
    }
  },
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

2. Index Templates (버전 7.x 이상)

  • mappings, settings, aliases 등의 섹션을 포함할 수 있다.
  • Legacy Index Templates 보다 관리하기 용이하다.
  • 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"
        }
      }
    }
  }
}

0개의 댓글