Elasticsearchのバッチ処理(bulk API)を記す。
公式ドキュメント Batch Processing を参考にした。 www.elastic.co
検証環境: Elasticsearch 6.0.0-rc1
Elasticsearchは、ドキュメントのインデックス付け・更新・削除が可能であることに加え、_bulk APIを使用してバッチで処理する機能もある。
bulk APIの簡単な例として、以下の例では2つのドキュメント(ID 1にはJohn Doe、ID 2にはJane Doe)をインデックスする。
bash-3.2$ curl -XPOST 'localhost:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' > {"index":{"_id":"1"}} > {"name": "John Doe" } > {"index":{"_id":"2"}} > {"name": "Jane Doe" } > ' { "took" : 4, "errors" : false, "items" : [ { "index" : { "_index" : "customer", "_type" : "doc", "_id" : "1", "_version" : 8, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 7, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "customer", "_type" : "doc", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 3, "_primary_term" : 1, "status" : 201 } } ] } bash-3.2$ curl -XGET 'localhost:9200/customer/doc/1?pretty&pretty' { "_index" : "customer", "_type" : "doc", "_id" : "1", "_version" : 8, "found" : true, "_source" : { "name" : "John Doe" } } bash-3.2$ curl -XGET 'localhost:9200/customer/doc/2?pretty&pretty' { "_index" : "customer", "_type" : "doc", "_id" : "2", "_version" : 1, "found" : true, "_source" : { "name" : "Jane Doe" } }
次の例では、最初のドキュメント(IDが1)を更新して2番目のドキュメント(IDが2)を削除する。
bash-3.2$ curl -XPOST 'localhost:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' > {"update":{"_id":"1"}} > {"doc": { "name": "John Doe becomes Jane Doe" } } > {"delete":{"_id":"2"}} > ' { "took" : 4, "errors" : false, "items" : [ { "update" : { "_index" : "customer", "_type" : "doc", "_id" : "1", "_version" : 9, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 8, "_primary_term" : 1, "status" : 200 } }, { "delete" : { "_index" : "customer", "_type" : "doc", "_id" : "2", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 4, "_primary_term" : 1, "status" : 200 } } ] } bash-3.2$ curl -XGET 'localhost:9200/customer/doc/1?pretty&pretty' { "_index" : "customer", "_type" : "doc", "_id" : "1", "_version" : 9, "found" : true, "_source" : { "name" : "John Doe becomes Jane Doe" } } bash-3.2$ curl -XGET 'localhost:9200/customer/doc/2?pretty&pretty' { "_index" : "customer", "_type" : "doc", "_id" : "2", "found" : false }
bulkAPIは、何らかの理由でのどこかのアクションが失敗した場合もその後の残りのアクションを処理し続ける。buikAPIは特定のアクションが失敗したかどうかを確認できるように、各アクションのステータスが(送信されたのと同じ順序で)提供される。