IT/elasticsearch

Elasticsearch index template 만들기

할일없는라이프 2022. 9. 14. 14:58

elasticsearch 에서는 R-DBMS의 테이블의 스키마/아키텍쳐와 같은 것이  elastic의 인덱스(테이블)에는 존재하지 않습니다. 이를 비/반정형 데이터라 부르며 정형 데이터와 달리 넣는 그대로  네이과 내용이 들어간다(elastic은 반정형 데이터 입니다)

그렇다면 elastic에서 데이터의 구조가 없이 들어간다면 나중에 통계나 서치를 하는 부분의 문제가 생길수 있습니다. 
그렇기 때문에 보통은 index를 생성할때 mapping이나 setting을 할수 있으나 이도 인덱스가 생성될때 마다 정의를 하기에는 어려운 일입니다.
그래서 elastic에는   index_template 라는 기능이 있으며 , template 사용시 특정인덱스의  생성시 특정 컬럼(이름)의 특정 데이터 형테를 지정할수 있습니다. 

template종류 

  1. _index_template
    기본적인 템플릿(흔의 elastic의 template는 index_template를 말함)으로 settings, mappings, aliases를 정의 할 수 있습니다. 
  2. _component_template
    구성요소 템플릿 , 기본개념은 index_template와 같으나, component_template 여러개를 합하여 한개의 index_template를 선언할수 있습니다. 

    예 > A, B의 구성요소(component_template)를 가진 C템플릿(index_template을 정의 할 수 있다

 

template 생성 방법

1. _component_template

PUT _component_template/template01
{
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    },
    "mappings": {
      "_source": {
        "enabled": true
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "create_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }
    }
  }
  
}

위에 component template이용시 프라이머리샤드를 1개 만들고 해당레플리카를 1세트 만들어라  

    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 1
    },

그리고 데이터의 ,host_name은 키워드 형식으로 , create_at은 데이터 형식으로 받아라 라는 설정이 들어가 있습니다. 

      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "create_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }

위에 처럼 component_template를 선언후 해당 인덱스를 이용해 index_template를 선얼 할 수 있습니다. 

2. index_template

PUT _index_template/index_tempate01
{
  "index_patterns": ["test*"],
  "template": {
    "mappings" : {
      "_source": {
        "enabled": true
      },
      "properties": {
        "name": {
          "type": "keyword"
        },
        "titel": {
          "type": "text"
        },
        "age": {
          "type": "long"
        }
      }
    },
    "aliases" : {
      "my_test": { }
    }
  },
  "priority": 500,
  "composed_of": ["template01"]
}

index_template는 인덱스 패턴을 생성한다 해당 index_template는 test* 라는 이름으로 인덱스가 생성시 적용된다 

"index_patterns": ["test*"],

composed_of를 이용해서 기존에 정의된 component_template "template01"를 구성요소로 적용할수 있다 

 "composed_of": ["template01"]

 

이후 test001 이라는 index를 생성후 mapping 정보를 확인해 보면 아래 와 같이 생성이 되는것을 확인 할수 있다 

{
  "test001": {
    "mappings": {
      "properties": {
        "age": {
          "type": "long"
        },
        "create_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        },
        "host_name": {
          "type": "keyword"
        },
        "name": {
          "type": "keyword"
        },
        "titel": {
          "type": "text"
        },
        "title": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}