[elasticsearch] _reindex API 사용하기

HI·2024년 10월 15일

_reindex API

간단하게 타겟 index를 사용해 새로운 색인으로 만들 수 있다.

POST /_reindex
{
  "source": {
    "index": "your_source_index"
  },
  "dest": {
    "index": "your_target_index"
  }
}
  • source : 색인할 타겟 index 정보
  • dest : 신규 색인 index 정보

다른 클러스터의 색인을 reindex 하기

remote 로 다른 클러스터의 색인도 reindex 가능하다.
먼저 elasticsearch.yml에 remote할 클러스터의 정보를 입력한다.

#elasticsearch.yml
reindex.remote.whitelist: "host:9200"

그리고 reindex를 다음과 같이 진행한다.

POST _reindex?wait_for_completion=false
{
  "source": {
    "remote": {
      "host": "http://host:9200",
      "username": "elastic",  
      "password": "my-password"
    },
    "index": "test1",
    "size": 1000,
    "query": {
      "match_all": {
        
      }
    }
  },
  "dest": {
    "index": "test2"
  },
  "max_docs": 5000 
 ,"requests_per_second": 100 
}
  • wait_for_completion=false
    대규모 reindex 작업인 경우 time_out을 초과할 수 있어 _reindex API를 뒤에서 실행하게 한다.
  • size: 한번에 처리하는 문서 수. 기본값은 100이라고 함.
  • max_docs : 최대 처리할 문서 수
  • requests_per_second : 초당 처리할 요청 수

참고로 리프레시를 비활성화 해놓으면 색인을 보다 빠르게 할 수 있다.
PUT /my-index/_settings
{"index": {"refresh_interval": "-1" // 리프레시를 비활성화}}
PUT /my-index/_settings
{"index": { "refresh_interval": "1s" // 기본값으로 되돌리기}}
! 단, 비활성화 동안은 색인된 문서가 보이지 않는다.

실행중인 _reindex 작업 확인

GET _tasks?actions=*reindex&wait_for_completion=false&detailed

_reindex 작업 취소하기

  • 실행중인 모든 작업 취소하기

    POST _tasks/_cancel
  • 특정 작업 취소하기

    reindex작업을 확인하면 task_id 알 수 있다.
    POST _tasks/'task_id입력'/_cancel

참고) https://www.elastic.co/kr/blog/3-best-practices-for-using-and-troubleshooting-the-reindex-api

profile
https://github.com/gaeunban

0개의 댓글