ABOUT ME

Developer를 위한 Reference 블로그 입니다.

Today
Yesterday
Total
  • Index Template
    BackEnd/Elasticsearch API 2025. 3. 16. 09:00
    반응형

      인덱스 템플릿(Index Template)은 새로운 인덱스를 생성할 때 자동으로 적용할 수 있는 매핑과 설정을 정의하는 방법입니다. 이는 여러 인덱스가 공통의 설정과 매핑을 가질 수 있도록 하여, 인덱스 생성 시 효율성을 높이고 일관성을 유지하는데 유용합니다.

     

      인덱스 템플릿은 시간 기반 인덱스와 같은 유사한 구조를 가진 인덱스를 관리하는데 유용합니다. 예를 들어, 로그 데이터나 메트릭 데이터는 주로 시간별로 분리되어 저장되며, 이러한 인덱스들은 동일한 매핑과 설정을 공유하는 경우가 많습니다.

     

    Reserved index patterns

    • logs-*-*
    • metrics-*-*
    • synthetics-*-*
    • profiling-*
    • security_solution-*-*

     

    Index Template

    # 인덱스 템플릿 생성
    PUT /_index_template/logs_template
    {
      "index_patterns": ["log-*"],
      "priority": 1,
      "template": {
        "settings": {
          "number_of_shards": 1,
          "number_of_replicas": 1
        },
        "mappings": {
          "properties": {
            "timestamp": {
              "type": "date"
            },
            "message": {
              "type": "text"
            },
            "status": {
              "type": "keyword"
            }
          }
        },
        "aliases": {
          "logs": {}
        }
      }
    }
    
    # 인덱스 템플릿 조회
    GET /_index_template/logs_template
    
    # 인덱스 템플릿 삭제
    DELETE /_index_template/logs_template
    • index_patterns: 해당 템플릿이 적용될 인덱스의 이름 패턴을 지정합니다. log-* 패턴의 모든 인덱스에 이 템플릿이 자동으로 적용됩니다.
    • priority: 여러 템플릿이 동일한 인덱스 패턴과 일치할 경우, 우선순위가 높은 템플릿의 설정이 적용됩니다.
    • template: 새로운 인덱스가 생성될 때 적용할 설정을 정의합니다.
    • aliases: 새로운 인덱스가 생성될 때 자동으로 logs 라는 이름의 alias를 생성합니다. 이는 여러 인덱스를 하나의 이름으로 통합하여 검색할 수 있게 합니다.

     

    인덱스 생성 & 데이터 색인

    # log-2024-06-18 인덱스 생성
    PUT /log-2024-06-18
    {
      "mappings": {
        "properties": {
          "extra_field": {
            "type": "text"
          }
        }
      }
    }
    
    # 데이터 색인
    POST /log-2024-06-18/_doc/1
    {
      "extra_field": "This is an extra field for additional information.",
      "message": "System started successfully.",
      "status": "info",
      "timestamp": "2024-06-18T10:15:30Z"
    }
    
    POST /log-2024-06-18/_doc/2
    {
      "extra_field": "User login attempt failed due to incorrect password.",
      "message": "Login attempt failed.",
      "status": "error",
      "timestamp": "2024-06-18T11:00:00Z"
    }
    
    # log-2024-06-19 인덱스 생성
    PUT /log-2024-06-19
    {
      "mappings": {
        "properties": {
          "extra_field": {
            "type": "text"
          }
        }
      }
    }
    
    # 데이터 색인
    POST /log-2024-06-19/_doc/1
    {
      "extra_field": "Disk space running low on server.",
      "message": "Warning: Disk space below threshold.",
      "status": "warning",
      "timestamp": "2024-06-19T14:22:10Z"
    }

     

    데이터 검색

    GET /logs/_search
    {
      "query": {
        "match_all": {}
      }
    }

     

    [참고자료]

    반응형

    'BackEnd > Elasticsearch API' 카테고리의 다른 글

    Search  (0) 2025.03.20
    Analyzer  (0) 2025.03.18
    Alias & Reindex  (0) 2025.03.15
    Mapping parameters  (0) 2025.03.14
    Mapping  (0) 2025.03.13

    댓글

Designed by Tistory.