elasticsearch bulk (batch) operation

    In es, we may have such a requirement, that is, sometimes we need to insert or update or delete data into es in batches. If the operation of one data is very slow, then the bulk api of es can come in handy.

 

delete delete operation, only need to write a json to
create create operation, if the document to be created already exists, then the creation fails
index create or replace operation, if the document to be created does not exist, execute the create operation, if it already exists, execute the replacement Operation
update Update operation Perform document update

#Requirements:
    1. Use create to create documents numbered 21, 22, and 23
    2. Use create to create documents numbered 22 again, which will fail because the document numbered 22 already exists
    3. Use index to create documents numbered 24 and
    25. 4. Use index to replace document numbered 25.
    5. Modify the data of document numbered 21.
    6. Delete document numbered 23.

curl -XPOST "http://192.168.99.1:9200/_bulk" -d'
{"create":{"_index":"productindex","_type":"product","_id":21}}
{"name":"21 name","price":21}
{"create":{"_index":"productindex","_type":"product","_id":22}}
{"name":"22 name","price":22}
{"create":{"_index":"productindex","_type":"product","_id":23}}
{"name":"23 name","price":23}
{"create":{"_index":"productindex","_type":"product","_id":22}}
{"name":"The document with id 22 already exists, the creation failed","price":22}
{"index":{"_index":"productindex","_type":"product","_id":24}}
{"name":"Document does not exist, was created","price":24}
{"index":{"_index":"productindex","_type":"product","_id":25}}
{"name":"21 name","price":25}
{"index":{"_index":"productindex","_type":"product","_id":25}}
{"name":"Because the document number 25 already exists, the replacement operation is performed, and the value of the price field is gone"}
{"update":{"_index":"productindex","_type":"product","_id":21}}
{"doc":{"name":"Modify the data of the document numbered 21, the value of the price field is still there"}}
{"delete":{"_index":"productindex","_type":"product","_id":23}}
'

  Execution results, part.
  
 

    After the batch execution is completed, es will return the execution result of each command. If one command reports an error, it will not affect the execution of the rest of the commands.

    Under the batch execution API, each json string needs to occupy one line, and the json string cannot be formatted, otherwise it cannot be executed.

    It is not recommended that the request body of the bulk request be too large, as it will affect performance. It is recommended not to exceed several tens of megabytes. If the index queue is not enough, you need to adjust the value of threadpool.index.queue_size   .

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326990079&siteId=291194637