Elasticsearch(九)| bulk(批处理)的命令行
批处理命令bulk可以在单个请求中一次执行多个索引或者删除更新操作,使用这种方式可以极大的提升索引性能。
官方地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html
测试数据
准备好测试数据,保存到bulk.json文件中:
{"create":{"_index":"test_record"}}
{"key":"中华人民共和国国籍","date":"2021-11-15T13:12:00","counts" : 4}
{"create":{"_index":"test_record"}}
{"key":"中华人民共和国国徽","date":"2021-11-16T13:12:00","counts" : 14}
{"delete":{"_index":"test_record","_id" : "pzK5HIQB8xpN1XtdHm7Z" }}
{"update":{"_index":"test","_id":"qjK5HIQB8xpN1Xtdy25q"}}
{"doc":{"date":"2021-12-15T14:12:00"}}
这里一共4个命令,先是创建了国籍和国徽,删除了国歌,把国旗的时间进行了修改。
Bulk批处理
使用下面命令可以执行json文件:
curl --cacert http_ca.crt -u elastic -XPOST https://localhost:9200/_bulk?pretty -H 'Content-Type: application/json' --data-binary @bulk.json
bulk会按顺序执行命令,并且如果其中有一个命令错误,依然是执行完毕,并返回每个命令对应的执行结果:
{ "took" : 3933, "errors" : false, "items" : [ { "create" : { "_index" : "test_record", "_id" : "vNoNN4QBHJgRqN7h6HeV", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 21, "_primary_term" : 4, "status" : 201 } }, { "create" : { "_index" : "test_record", "_id" : "vdoNN4QBHJgRqN7h6HeV", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 22, "_primary_term" : 4, "status" : 201 } }, { "delete" : { "_index" : "test_record", "_id" : "pzK5HIQB8xpN1XtdHm7Z", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 23, "_primary_term" : 4, "status" : 200 } }, { "update" : { "_index" : "test_record", "_id" : "qjK5HIQB8xpN1Xtdy25q", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 27, "_primary_term" : 4, "status" : 200 } } ] }
Replace替换
有时候在update的时候可能需要整个替换,在bulk中可以用下面的命令格式:
{"update":{"_index":"search_record","_id":"vNoNN4QBHJgRqN7h6HeV"}}
{"script":{"source":"ctx._source=params","params":{"key" : "中华人民共和国国歌","date" : "2021-10-15T13:12:00","counts" : 1},"lang":"painless"}}
这条命令就是把新加的国籍整个替换成国歌的内容
0