Elasticsearch bulkを使って複数ドキュメント登録


Elasticsearchで一つ一つドキュメントを追加していくのが面倒だったため、jsonファイルを使って複数ドキュメントを追加していきます。

JSONファイル

sample.json
{"index":{}}
{"id": 1, "name": "じゃがりこ"}
{"index":{}}
{"id": 2, "name": "マイクポップコーン"}
{"index":{}}
{"id": 3, "name": "チョコあ〜んぱん"}
{"index":{}}
{"id": 4, "name": "茎わかめ"}

一番下に一行空けないとエラーになってしまう。。

実行コマンド

# テンプレート
$ curl -H 'Content-Type: application/x-ndjson' -XPOST http://{domain}/{index}/_bulk?pretty --data-binary @{json file}.json

$ curl -H 'Content-Type: application/x-ndjson' -XPOST http://0.0.0.0:9200/snacks/_bulk?pretty --data-binary @sample.json