Obsidian/Recognition/Programing/ELK/ElasticSearch 쿼리.md

9.0 KiB

템플릿 생성


PUT _template/replay_log_3_fusion
{
  "order" : 1,
  "version" : 60001,
  "index_patterns": [
    "replay_log_3_fusion-*"
  ],
  "settings": { 
    "index": {
      "lifecycle": {
        "name": "index-save-90days-policy",
        "rollover_alias": "replay_log_3_fusion"
      }
    },
    "max_result_window" : 500000,
    "number_of_shards": "1",
    "number_of_replicas": "0"
  },
  "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",
                  "ignore_above": 1 
                }
              }
            },
            "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
                }
              }
        }
      }
  }
}

인덱스 생성

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
                }
              }
        }
      }
  }
}


PUT rolling-test-000001
{
  "aliases": {
    "rolling-write-index": { "is_write_index": true } 
  }
}

인덱스, 템플릿 삭제

// 인덱스 삭제
DELETE replay_log_7_ana-2024-02-27

// 템플릿 삭제
DELETE _template/replay_log_3_fusion-2024-02-27_re

인덱스 복사


POST _reindex
{
 "source": {
  "index": "replay_log_7_ana-2024-02-27"
 },
 "dest": {
  "index": "replay_log_7_ana-2024-02-27_re"
 }
}


SQL -> DLS 변환


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 "
}

조회


// ?filter_path=hits.hits._source.message

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값
  

Alias 추가/삭제

// Alias 추가
POST /_aliases?pretty
{
  "actions": [
    {
      "add": {
        "index": "replay_log_9_ptz-2024-03-12",
        "alias": "replay_log",
        "is_write_index": true
      }
    },
    {
      "add": {
        "index": "replay_log_9_ptz-2024-03-13",
        "alias": "replay_log",
        "is_write_index": true
      }
    }
  ]
}

// Alias 삭제
POST /_aliases?pretty
{
  "actions": [
  	{
      "remove": {
        "index": "replay_log_9_ptz-2024-03-12",
        "alias": "replay_log"
      }
    },
    {
      "remove": {
        "index": "replay_log_9_ptz-2024-03-13",
        "alias": "replay_log"
      }
    }
  ]
}



-----------------------------------------------------------------------------

POST *_log_*/_search?filter_path=hits.hits._source.message
{
  "size" : 10000,
  "track_total_hits": true,
  "query" : {
      "bool" : {
        "must" : [
          {
            "bool" : {
              "must" : [
                {
                  "terms" : {
                    "header.msg_type.keyword" : [
                      3
                    ]
                  }
                },
                {
                  "range" : {
                    "header.recv_time" : {
                      "from" : "20240704110500.000",
                      "to" : "20240704113000.000"
                    }
                  }
                }
              ]
            }
          },
          {
            "query_string": {
              "query" : "*HL8010*"
            }
          }
        ]
      }
    },
  "sort" : [{"header.recv_time.keyword" : {"order" : "ASC"}}]
}

총 데이터 건수 가져오기


GET [인덱스명]/_count

인덱스에 데이터 넣기


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"}


인덱스별 용량


GET /_cat/indices/replay*?v&s=index

// S : (Optional, string) 응답을 정렬하는 데 사용되는 열 이름 또는 열 별칭의 쉼표로 구분된 목록입니다.
// V : (Optional, boolean) true인 경우 응답에 열 제목이 포함됩니다. 기본값은 false입니다.

// json 포멧으로
_cat/indices?format=json
_cat/indices?format=json&pretty


// 

ES서버 디스크 용량


GET _cat/allocation?v

// 디스크 full, 리텐션 관련 : https://qkqhxla1.tistory.com/1071

// Optimize 
curl -XPOST 'http://localhost:9200/twitter/_optimize'

// 클러스터 정보 조회
http://10.200.31.130:9200/_cluster/settings?include_defaults&flat_settings
(max_shards_per_node)

// alias 정보 조회
GET _alias/replay_log*

GET /_cat/aliases/?v=true&s=index


// 인덱스 Policy 정보 조회
GET interface_log*/_ilm/explain

// 특정 Policy 상세정보
GET _ilm/policy/index-save-90days-policy

// Policy 일괄 적용
PUT /replay_log*/_settings
{
  "index": {
    "lifecycle": {
      "name": "index-save-90days-policy",
      "rollover_alias": "replay_log"
    }
  }
}

// 템플릿 정보 확인
GET _cat/templates?v&s=name

// 특정 템플릿 상세 정보 
GET _template/replay_log_9_ptz
 

// template 삭제
DELETE _template/travel-log-template

// 샤드 리스트 확인 
GET /_cat/shards?h=index,shard,prirep,state,unassigned.reason