본문 바로가기

elasticsearch

[elasticsearch] 엘라스틱서치 리인덱싱(reindexing) - 같은서버, 다른서버

반응형

 

엘라스틱서치에서는 인덱스의 이름을 바꾸거나, 속성을 바꾸려면 리인덱싱(reindexing)이라는 방법을 통해서 인덱스를 관리해야 한다. 이러한 리인덱싱 작업은 엘라스틱서치를 사용할때 꽤나 자주 발생한다. 

리인덱싱은 같은 엘라스틱서치 내에서도 할 수 있지만, 다른 엘라스틱 서치에 있는 데이터를 가져와서 리인덱싱할 수도 있다. 이번 포스팅에서는 그 두가지 방법을 소개한다.

 

같은 elasticsearch 내에서 reindexing

Kibana Dev Tools에서

POST _reindex?wait_for_completion=true
{
  "source": {
    "index": "machine_log"
  },
  "dest": {
    "index": "another_machine_log"
  }
}

 

curl 명령어

curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
  "source": {
    "index": "machine_log"
  },
  "dest": {
    "index": "another_machine_log"
  }
}
'

source 부분에 원래 인덱스명을, dest 부분에 새롭게 생성한 인덱스명을 입력한다. 꼭 미리 새로 생성할 인덱스를 생성하고, 리인덱싱 작업을 진행하자. 위 예제는 machine_log라는 인덱스의 내용을 anoter_machine_log로 복사하는 내용이다.

 

 

다른 elasticsearch에서 reindexing

Kibana Dev Tools에서

POST _reindex?wait_for_completion=true
{
  "source": {
    "remote": {
      "host": "http://otherhost:9200",
      "username": "user",
      "password": "pass"
    },
    "index": "machine_log",
    "query": {
      "match": {
        "test": "data"
      }
    }
  },
  "dest": {
    "index": "another_machine_log"
  }
}

 

curl 명령어

curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
  "source": {
    "remote": {
      "host": "http://otherhost:9200",
      "username": "user",
      "password": "pass"
    },
    "index": "machine_log",
    "query": {
      "match": {
        "test": "data"
      }
    }
  },
  "dest": {
    "index": "another_machine_log"
  }
}
'

source 부분에 원래 인덱스명을, dest 부분에 새롭게 생성한 인덱스명을 입력한다. source 부분에 데이터를 가져올 remote elasticsearch 주소와 포트를 host에 명기한다. 필요하다면 username, password도 설정해야 한다. 위 예제에서는 otherhost에 있는 machine_log를 로컬에 있는 another_machine_log로 리인덱싱하는 작업이다. 꼭 미리 새로 생성할 인덱스를 생성하고, 리인덱싱 작업을 진행하자.

 

반응형