文章目录
批量新增
- 批量新增
- 批量删除
批量操作, 主要是用BulkRequest , 在这个request中, 批量添加新增文档的请求.
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.util.Arrays;
/**
* 类名称:ESTest_Client
* 类描述: 创建索引
*
* @author:
* 创建时间:2022/1/11 21:05
*/
public class ESTest_Doc_Insert_Batch {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = null;
// 创建ES客户端
try {
esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
);
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan", "age", 30, "sex", "男"));
request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi", "age", 20, "sex", "女"));
request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu", "age", 50, "sex", "男"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println("批量创建文档操作花费毫秒数:" + response.getTook());
System.out.println("批量创建文档操作结果:" + Arrays.asList(response.getItems()).toString());
} finally {
if (esClient != null) {
// 关闭ES客户端
esClient.close();
}
}
}
}
执行 后控制台打印如下 查询其中一个文档的数据, 也能查询到, 代表新增成功.
在BulkRequest中封装多个DeleteRequest , 进行批量删除操作.
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.util.Arrays;
/**
* 类名称:ESTest_Client
* 类描述: 批量删除文档
*
* @author:
* 创建时间:2022/1/11 21:05
*/
public class ESTest_Doc_Delete_Batch {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = null;
// 创建ES客户端
try {
esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
);
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index("user").id("1001"));
request.add(new DeleteRequest().index("user").id("1002"));
request.add(new DeleteRequest().index("user").id("1003"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println("批量删除文档操作花费毫秒数:" + response.getTook());
System.out.println("批量删除文档操作结果:" + Arrays.asList(response.getItems()).toString());
} finally {
if (esClient != null) {
// 关闭ES客户端
esClient.close();
}
}
}
}
执行结果如下
批量删除文档操作花费毫秒数:18ms 批量删除文档操作结果:[org.elasticsearch.action.bulk.BulkItemResponse@6aaceffd, org.elasticsearch.action.bulk.BulkItemResponse@c86b9e3, org.elasticsearch.action.bulk.BulkItemResponse@10aa41f2]
进行查询也无法查询到了, 说明删除成功.