上QQ阅读APP看书,第一时间看更新
How to do it...
As we are changing the state of the data, the HTTP method is POST and the REST URL is as follows:
http://<server>/<index_name/_bulk
To execute a bulk action, we will perform the following steps via curl (because it's very common to prepare your data on files and send them to Elasticsearch via the command line):
- We need to collect the create/index/delete/update commands in a structure made of bulk JSON lines, composed of a line of action with metadata, and another optional line of data related to the action. Every line must end with a new line \n. A bulk data file should be presented like this:
{ "index":{ "_index":"myindex", "_id":"1" } }
{ "field1" : "value1", "field2" : "value2" }
{ "delete":{ "_index":"myindex", "_id":"2" } }
{ "create":{ "_index":"myindex", "_id":"3" } }
{ "field1" : "value1", "field2" : "value2" }
{ "update":{ "_index":"myindex", "_id":"3" } }
{ "doc":{"field1" : "value1", "field2" : "value2" }}
- This file can be sent with the following POST:
curl -s -XPOST localhost:9200/_bulk --data-binary @bulkdata;
- The result returned by Elasticsearch should collect all the responses of the actions.
You can execute the previous commands in Kibana with the following call:
POST /_bulk
{ "index":{ "_index":"myindex", "_id":"1" } }
{ "field1" : "value1", "field2" : "value2" }
{ "delete":{ "_index":"myindex", "_id":"2" } }
{ "create":{ "_index":"myindex", "_id":"3" } }
{ "field1" : "value1", "field2" : "value2" }
{ "update":{ "_index":"myindex", "_id":"3" } }
{ "doc":{"field1" : "value1", "field2" : "value2" }}