Elasticsearch template 사용 방법

try.catch·2019년 12월 19일
0

Elasticsearch 인덱스 생성시 mapping을 하게 되는데, 이때 template 을 이용하게 되면 굉장히 편리하게 mapping을 할 수 가 있다.

템플릿 생성하기

httpd-access-log 라는 이름의 템플릿을 생성한다.
이때 인덱스 이름이 httpd-access-* 의 패턴이라면, 해당 템플릿이 적용된다.

$ curl -XPUT localhost:9200/_template/httpd-access-log -d '
{
  "index_patterns": [
    "httpd-access-*"
  ],
  "mappings": {
    "log": { // type name
      "properties": {
        "ip": {
          "type": "text"
        },
        "host": {
          "type": "keyword"
        },
        "uri": {
          "type": "text"
        },
        "datetime": {
          "type": "date"
        },
        "@timestamp": {
          "type": "date"
        }
      }
    }
  }
}

setting 설정하기

인덱스 생성시 setting 설정을 한다.

$ curl -XPUT localhost:9200/_template/httpd-access-log -d '
{
  "index_patterns": [
    "httpd-access-*"
  ],
  "settings": { // setting
    "number_of_shards": 1
  },
  "mappings": {
    "log": { // type name
      "properties": {
        "ip": {
          "type": "text"
        },
        "host": {
          "type": "keyword"
        },
        "uri": {
          "type": "text"
        },
        "datetime": {
          "type": "date"
        },
        "@timestamp": {
          "type": "date"
        }
      }
    }
  }
}

여러개의 패턴 설정하기

여러개의 패턴을 배열로 지정한다.

$ curl -XPUT localhost:9200/_template/httpd-access-log -d '
{
  "index_patterns": [
    "httpd-access1-*",
    "httpd-access2-*",
    "httpd-access3-*",
    "httpd-access4-*",
    "httpd-access5-*",
    "httpd-access6-*",
  ],
  "settings": { // setting
    "number_of_shards": 1
  },
  "mappings": {
    "log": { // type name
      "properties": {
        "ip": {
          "type": "text"
        },
        "host": {
          "type": "keyword"
        },
        "uri": {
          "type": "text"
        },
        "datetime": {
          "type": "date"
        },
        "@timestamp": {
          "type": "date"
        }
      }
    }
  }
}

2개의 댓글

comment-user-thumbnail
2021년 5월 26일

잘복고갑니당

1개의 답글