2019년 4월 4일 목요일

Elasticsearch - 한번에 데이터를 index로 추가하기 (Bulk Index)

이번에는 한 번에 문서를 Elasticsearch로 2개 이상을 등록하는 방법인 Bulk Index API 예제를 작성한다.

ID를 지정하는 경우

bulk.json
{ "index" : {"_id" : "1" } }
{ "field1": "노코드1", "field2": "노코드2", "field3": "노코드3" }
{ "index" : {"_id" : "2" } }
{ "field1": "노코드1", "field2": "노코드2", "field3": "노코드3" }

위와 같은 파일을 저장한 후 콘솔화면에서 아래의 명령어를 실행한다.
curl -H "Content-Type: application/json" -X POST http://localhost:9200/{index 이름}/{type 이름}/_bulk?pretty --data-binary @bulk.json

ID를 지정하지 않는 경우

자동으로 ID가 생성된다.
bulk.json
{ "index" : {} }
{ "field1": "노코드1", "field2": "노코드2", "field3": "노코드3" }
{ "index" : {} }
{ "field1": "노코드1", "field2": "노코드2", "field3": "노코드3" }

위와 같은 파일을 저장한 후 콘솔화면에서 아래의 명령어를 실행한다.
curl -H "Content-Type: application/json" -X POST http://localhost:9200/{index 이름}/{type 이름}/_bulk?pretty --data-binary @bulk.json

index / type / id를 json 내에 작성하는 경우

bulk.json
{ "index" : { "_index" : "index 이름", "_type" : "type 이름", "_id" : "1" } }
{ "field1": "노코드1", "field2": "노코드2", "field3": "노코드3" }

위와 같은 파일을 저장한 후 콘솔화면에서 아래의 명령어를 실행한다.
curl -H "Content-Type: application/json" -X POST http://localhost:9200/{index 이름}/{type 이름}/_bulk?pretty --data-binary @bulk.json

댓글 없음:

댓글 쓰기