티스토리 뷰

인덱스 템플릿

  • 인덱스 템플릿 생성: PUT _index_template/{템플릿명}
  • 적용될 인덱스 패턴 지정: index_patterns: "{인덱스명}-*"

_reindex API 에서 스크립트 사용

  • painless 스크립트를 사용해서 인덱스명 지정
  • ctx._index = ctx._substring(...)

_reindex API에서 ingest pipeline 사용

  • remove 프로세서를 이용하여 불필요한 필드를 삭제하는 ingest pipeline 생성
  • 재색인 시 생성한 ingest pipeline 적용하여 필드 삭제

https://www.elastic.co/guide/en/elasticsearch/reference/7.10/index.html

 

Elasticsearch Guide [7.10] | Elastic

 

www.elastic.co

 

아래를 따라하기위해서는 이전 글을 참고하셔야합니다.

2022.11.08 - [ELK 스택] - Elasticsearch - 인덱스 재색인 : 필드 매핑 개선

 

Elasticsearch - 인덱스 재색인 : 필드 매핑 개선

2022.11.08 - [ELK 스택] - Filebeat - Logstash - Elasticsearch 예제 Filebeat - Logstash - Elasticsearch 예제 Filebeat 설치 및 실행 filebeat.yml 에서 파일 입력 설정 파일을 다시 읽어들이기 위한 data 플래그 삭제 Filebeat =>

jhyunnn.tistory.com

 

# 인덱스 템플릿 생성
PUT _index_template/mylogs
{
  "index_patterns": [
    "mylogs*"
  ],
  "priority": 10,
  "template": {
    #mappings 를 이런식으로 넣습니다.
    "mappings": {
      "properties": {
        "@timestamp": {
        },
        ...
      }
    },
    "verb": {
      "type": "keyword"
    }
  }
}

# 인덱스 템플릿 조회
GET _index_template/mylogs

# 인덱스 템플릿 삭제
DELETE _index_template/mylogs

여기서 다시 reindex 해보면 다이나믹 매핑으로 생성이되지만,

템플릿을 지정한 방식대로 생성이 되는걸 확인 할 수 있습니다.

POST _reindex
{
  "source": {
    "index": "apachelog-2020.08.08"
  },
  "dest": {
    "index": "mylogs"
  }
}

GET mylogs/_mapping

위 방식으로하면 하나하나 다 따로따로 해줘야하지만 이거를 여러개 가져오기 위해서는

source에서는 apachelog-*가 가능하지만 dest 에서는 * 하면 불가능합니다. 그래서 스크립트를 작성해줘야합니다.

 

# dest 에서 * 하면 에러가 발생합니다.
# 아래는 잘못된 예제!!

POST _reindex
{
  "source": {
    "index": "apachelog-*"
  },
  "dest": {
    "index": "mylogs-*"
  }
}

# 스크립트를 이용해서 하는 방법
# painless 스크립트 사용

POST _reindex
{
  "source": {
    "index": "apachelog-*"
  },
  "dest": {
    "index": "mylogs-*"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = 'mylogs-' + (ctx._index.substring('apachelog-'.length(), ctx._index.length()))"
  }
}

# 인덱스에 접근할때는 ctx. 로 접근해야하고
# 이름 부분을 수정해줍니다.

# 예제
# 2020.08.08 만 가져와서 한번 넣어보겠습니다.

POST _reindex
{
  "source": {
    "index": "apachelog-2020.08.08"
  },
  "dest": {
    "index": "mylogs-*"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = 'mylogs-' + (ctx._index.substring('apachelog-'.length(), ctx._index.length()))"
  }
}

GET _cat/indices//mylogs*


## clientip, ecs, input, tags 를 지우는거 해보겠습니다 필드 지우기

PUT _ingest/pipeline/mylogs_delete_fileds
{
  "processors": [
    {
      "remove": {
        "field": ["clientip", "ecs", "input", "tags"]
      }
    }
  ]
}

GET _ingest/pipeline/mylogs_delete_fileds

DELETE _ingest/pipeline/mylogs_delete_fileds



## pipeline 적용해서 생성

POST _reindex
{
  "source": {
    "index": "apachelog-*"
  },
  "dest": {
    "index": "mylogs",
    "pipeline": "mylogs_delete_fileds"
  },
  "script": {
    "lang": "painless",
    "source": "ctx._index = 'mylogs-' + (ctx._index.substring('apachelog-'.length(), ctx._index.length()))"
  }
}


#필드들이 삭제된걸 확인이 가능합니다.
GET mylogs-*/_search

#갯수 확인 가능
GET mylogs-*/_count 

#mylogs로 시작하는 인덱스 검색
GET _cat/indices/mylogs* 

# mapping 확인
GET mylogs_2020.08.09/_mapping

 

 

참고

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
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
글 보관함