## 템플릿 생성 ```Json PUT _template/replay_log_template { "index_patterns": [ "replay_log_*" // 적용할 인덱스 패턴 ], "settings": { "max_result_window" : 500000 // 50만개까지 한꺼번에 조회 가능하도록 설정 }, "mappings": { "properties": { "@timestamp": { "type": "date" }, "header": { "properties": { "log_type": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 1 // 1 자리 이외의 데이터는 검색에서 무시 } } }, "msg_type": { "type": "text", "fields": { "keyword": { "type": "keyword", } } }, "recv_time": { "type": "text", "fields": { "keyword": { "type": "keyword", "eager_global_ordinals": true // 집계에 많이 사용되는 필드를 캐시 } } }, "server_se": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 1 } } } } }, "message": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 1 } } } } } } ``` ## 인덱스 생성 ```json PUT replay_log_7_ana-2024-02-27_re { "settings": { "max_result_window" : 500000 }, "mappings": { "properties": { "@timestamp": { "type": "date" }, "header": { "properties": { "log_type": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 1 } } }, "msg_type": { "type": "text", "fields": { "keyword": { "type": "keyword" } } }, "recv_time": { "type": "text", "fields": { "keyword": { "type": "keyword", "eager_global_ordinals": true } } }, "server_se": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 1 } } } } }, "message": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 1 } } } } } } ``` ## 인덱스, 템플릿 삭제 ```json // 인덱스 삭제 DELETE replay_log_7_ana-2024-02-27 // 템플릿 삭제 DELETE _template/replay_log_3_fusion-2024-02-27_re ``` ## 인덱스 복사 ```json POST _reindex { "source": { "index": "replay_log_7_ana-2024-02-27" }, "dest": { "index": "replay_log_7_ana-2024-02-27_re" } } ``` ## SQL -> DLS 변환 ```json GET _sql/translate { "query" : "select message from \"replay_log_10_route-2024-03-12\" where header.msg_type in ('3', '7') " } GET _sql/translate { "query": "SELECT header.recv_time, message FROM \"*-fusion\" WHERE header.recv_time >= '20240111120000.000' AND header.recv_time <= '20240111163000.000' and header.msg_type like '3' order by header.recv_time" } GET _sql/translate { "query" : "select max(header.recv_time) as max_time, min(header.recv_time) as min_time from \"replay_log_10_route-2024-03-12\" where header.msg_type in ('3', '7')" } GET _sql/translate { "query" : "select max(header.recv_time) as max_time, min(header.recv_time) as min_time from \"replay_log_10_route-*\" " } GET _sql/translate { "query" : "select SUBSTRING(header.recv_time, 9,2) as hour, count(*) as cnt from \"replay_log_10_route-2024-03-12\" where SUBSTRING(header.recv_time, 1,8) = '20240502' group by SUBSTRING(header.recv_time, 9,2) " } GET _sql/translate { "query" : "select count(SUBSTRING(header.recv_time.keyword, 9,2)) as cnt from \"replay_log_10_route-2024-03-12\" where SUBSTRING(header.recv_time.keyword, 1,8) = '20240502' group by header.msg_type.keyword " } ``` ## 조회 ```json POST replay_log_*_*/_search { "size" : 1000, "track_total_hits": true, // 하위 조건에 해당하는 전체 데이터 건수 표시 (size 필드 상관 없음) "query" : { "bool" : { "filter" : [ { "terms" : { "header.msg_type.keyword" : [ "3", "7", "8" ], "boost" : 1.0 } }, { "range" : { "header.recv_time" : { "from" : "20240226163800.000", "to" : "20240226173859.000" } } } ] } }, "_source": ["@timestamp", "header", "message"], "sort" : [{"header.recv_time.keyword" : {"order" : "ASC"}}], "search_after" : ["0"] // 여러번 나눠서 요청하는 경우 마지막 solt값 ``` ## 총 데이터 건수 가져오기 ```json GET [인덱스명]/_count ``` ## 인덱스에 데이터 넣기 ```json POST test_index/_bulk {"index":{"_id":1}} {"message":"The quick brown fox"} {"index":{"_id":2}} {"message":"The quick brown fox jumps over the lazy dog"} {"index":{"_id":3}} {"message":"The quick brown fox jumps over the quick dog"} {"index":{"_id":4}} {"message":"Brown fox brown dog"} {"index":{"_id":5}} {"message":"Lazy jumping dog"} ``` ## 인덱스별 용량 ```json GET /_cat/indices/st*?v&s=index // S : (Optional, string) 응답을 정렬하는 데 사용되는 열 이름 또는 열 별칭의 쉼표로 구분된 목록입니다. // V : (Optional, boolean) true인 경우 응답에 열 제목이 포함됩니다. 기본값은 false입니다. // json 포멧으로 _cat/indices?format=json _cat/indices?format=json&pretty // ``` ## ES서버 디스크 용량 ```json GET _cat/allocation?v // 디스크 full, 리텐션 관련 : https://qkqhxla1.tistory.com/1071 ``` ```json // Optimize curl -XPOST 'http://localhost:9200/twitter/_optimize' ```