Elasticsearch template 사용 방법
Elasticsearch 인덱스 생성시 mapping을 하게 되는데, 이때 template 을 이용하게 되면 굉장히 편리하게 mapping을 할 수 가 있다.
템플릿 생성하기
httpd-access-log 라는 이름의 템플릿을 생성한다.
이때 인덱스 이름이 httpd-access-* 의 패턴이라면, 해당 템플릿이 적용된다.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27$ 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 설정을 한다.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30$ 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"
}
}
}
}
}
여러개의 패턴 설정하기
여러개의 패턴을 배열로 지정한다.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35$ 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"
}
}
}
}
}