본문 바로가기
ElasticSearch & OpenSearch

[OpenSearch] index template (인덱스 템플릿)

by yonikim 2023. 4. 6.
728x90

인덱스 템플릿은 주로 설정이 동일한 복수의 인덱스를 만들 때 사용한다.

 

로그 인덱스를 만든다고 가정해 보자. 한개의 인덱스에 모든 로그 데이터를 넣어주면 어떻게 될까?

초반에 데이터가 적을때는 괜찮겠지만 결국 나중에는 폭-팔-⭐️ 할 것이라는걸 모두 알고 있기 때문에, 로그 인덱스의 경우 대부분 일별 혹은 월별로 인덱스를 생성해줄 것이다. 

 

curation-log-20230405, curation-log-20230406, curation-log-20230407, ...

 

로그의 경우 같은 사전을 사용하고 같은 tokenizer 및 analyzer 를 사용하며 색인하는 필드 역시 동일할 것이다. 

그렇다면 일마다 동일한 settings 와 mappings 를 넣어 로그 인덱스를 생성해줘야 할까? 

 

다행히 OpenSearch 역시 ElasticSearch 와 마찬가지로 인덱스 템플릿을 제공해주기 때문에 이를 사용하여 인덱스를 보다 효율적으로 관리할 수 있다. 

패턴을 정의하여 해당 패턴의 이름으로 인덱스가 생성되는 경우 같은 settings, mappings, 사전을 사용하도록 설정해줄 수 있기 때문이다.

 

ElasticSearch 와 OpenSearch 의 인덱스 템플릿 생성하는 방법이 달라서 삽질한건 안비밀 

 


 

▷ 생성

PUT _index_template/template-curation-log
{
  "index_patterns": ["curation-log-*"],
  "template": {
    "settings": {
      "index": {
        "number_of_shards": "1",
        "number_of_replicas": "0",
        "refresh_interval": "1s"
      }
    },
    "mappings": {
      "properties": {
        "userInfo": {
          "type": "nested",
          "properties": {
            "deviceType": {
              "type": "keyword"
            },
            "userId": {
              "type": "long"
            },
            "profileId": {
              "type": "long"
            },
            "userType": {
              "type": "keyword"
            },
            "operatorId": {
              "type": "long"
            }
          }
        },
        "createdDate": {
          "format": "yyyy-MM-dd HH:mm:ss:SSS||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_second",
          "type": "date"
        },
        "ip": {
          "type": "ip"
        },
        "query": {
          "type": "nested",
          "properties": {
            "q": {
              "type": "keyword"
            },
            "size": {
              "type": "integer"
            },
            "sortBy": {
              "type": "keyword"
            },
            "page": {
              "type": "integer"
            }
          }
        },
        "userAgent": {
          "type": "text",
          "fields": {
            "keyword": {
              "ignore_above": 256,
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

 

▷ 생성된 템플릿 확인

GET _index_template/template-curation-log

 


 

인덱스를 생성한 후 조회해보면 설정한 템플릿 대로 settings, mappings, 사전 이 설정되어 있는걸 확인할 수 있다.

DELETE curation-log-test
PUT curation-log-test
GET curation-log-test
728x90