LoginSignup
2
1

More than 3 years have passed since last update.

ElasticSearchに複数データを投入

Last updated at Posted at 2019-05-05

複数データを投入する場合はBulkAPIを使う

BulkAPI

APIのEndpoint

POST /_bulk
POST /{index}/_bulk
POST /{index}/{type}/_bulk

API実行

コマンド

$ curl -s -H "Content-Type: application/x-ndjson" -XPOST localhost:9200/_bulk --data-binary "@insert_data.js"

insert_data.jsは投入データを定義したファイル

注意点

-H "Content-Type: application/x-ndjson"でheaderを指定する
-dではなく、--data-binaryを使う
insert_data.jsはndjson形式で記述する

insert_data.jsのサンプル

リファレンスがちょっと分かりづらかったのでうまく行ったサンプルを掲載します。

{"create":{ "_index" : "test_es" , "_id" : "1"}}
{"text":"あああああああ","text_id":291}
{"create":{ "_index" : "test_es" , "_id" : "2"}}
{"text":"えええええええ","text_id":338}
{"create":{ "_index" : "test_es" , "_id" : "3"}}
{"text":"たかたかたかか","text_id":941}

・1行目:実行するaction(create)とmetadata(_index, _id)
・2行目:投入するデータ(text, tesxt_id)
※1行目と2行目のフォーマットで投入するデータ分記述する
※create以外にもindex、delete、updateがあり、それぞれフォーマットが異なる
※createとindexのみ2行目が必要

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1