脚本批量操作
{"delete":{"_index":"person","_id":"2"}}
{"create":{"_index":"person","_id":"5"}}
{"name":"田七","age":22,"address":"北京海淀区"}
{"update":{"_index":"person","_id":"3"}}
{"doc":{"name":"王五3号"}}
javaApi批量操作
/**
* 批量操作
*/
@Test
void bulkDoc() throws IOException {
//创建bulkRequest,整合所有操作
BulkRequest bulkRequest=new BulkRequest();
/**
* 删除5号记录
* 添加8号记录
* 修改4号记录,名称为赵六4号
*/
//添加相应操作
//删除5号记录
DeleteRequest deleteRequest=new DeleteRequest("person","5");
bulkRequest.add(deleteRequest);
//添加8号记录
Map mapAdd=new HashMap<>();
mapAdd.put("name","阿西吧");
mapAdd.put("age",38);
mapAdd.put("address","北京天安门");
IndexRequest indexRequest=new IndexRequest("person").id("8").source(mapAdd);
bulkRequest.add(indexRequest);
//修改4号记录
Map mapUpdate=new HashMap<>();
mapUpdate.put("name","赵六4号");
//mapUpdate.put("age",38);
//mapUpdate.put("address","北京天安门");
UpdateRequest updateRequest=new UpdateRequest("person","4").doc(mapUpdate);
bulkRequest.add(updateRequest);
//执行批量操作
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(bulk.status());
}