1.批量插入
/**
* 批量添加索引
* @throws IOException
*/
public void batchAddIndex() {
// 创建es客户端
Client client = elasticClientUtil.getClient();
BulkRequestBuilder bulkRequest = client.prepareBulk();
try {
bulkRequest.add(client.prepareIndex("effect_kepler", "effect_kepler", "0")
.setSource(jsonBuilder()
.field("op_time", "2017-01-01")
.field("bid", 1)
...
.field("add_member_num", 10)
.endObject())
.endObject())
);
} catch (IOException e) {
e.printStackTrace();
}
try {
bulkRequest.add(client.prepareIndex("effect_kepler", "effect_kepler", "1")
.setSource(jsonBuilder()
.field("op_time", "2017-01-01")
.field("bid", 1)
...
.field("add_member_num", 10)
.endObject())
);
} catch (IOException e) {
e.printStackTrace();
}
...
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
System.out.println("failures..............:" + bulkResponse.buildFailureMessage());
}
}
2.批量删除
/**
* 批量删除索引 * @throws IOException
*/
public void batchDeleteIndex() {
logger.info("=================批量删除数据开始=====================");
// 创建es客户端
Client client = elasticClientUtil.getClient();
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareDelete("effect_ex", "effect_ex", "0"));
bulkRequest.add(client.prepareDelete("effect_ex", "effect_ex", "1"));
...
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
System.out.println("failures.............:" + bulkResponse.buildFailureMessage());
}
logger.info("=================批量删除数据结束=====================");
}
3.批量修改
public void batchUpdateIndex() throws IOException {
logger.info("=================批量更新数据开始=====================");
// 创建es客户端
Client client = elasticClientUtil.getClient();
BulkRequestBuilder bulkRequest = client.prepareBulk();
bulkRequest.add(client.prepareUpdate("effect_ex", "effect_ex", "1").setDoc(jsonBuilder()
.startObject()
.field("op_time", "2017-01-02")
.field("bid", 1)
...
.field("add_member_num", 10)
.endObject()));
bulkRequest.add(client.prepareUpdate("effect_ex", "effect_ex", "2").setDoc(jsonBuilder()
.field("op_time", "2017-01-02")
.field("bid", 1)
...
.field("add_member_num", 10)
.endObject()));
BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
System.out.println("failures..............:"+bulkResponse.buildFailureMessage()
);
}
logger.info("=================批量更新数据结束=====================");
}