目录
- 一、pom文件依赖
- 二、批量操作文档 代码示例
- 2.1、批量创建文档 代码示例
- 2.2、批量删除文档 代码示例
一、pom文件依赖
-
引入相关依赖
<!-- elasticsearch 依赖 --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.8.0</version> </dependency> <!-- elasticsearch 的客户端 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.8.0</version> </dependency> <!-- elasticsearch 依赖 2.x 的 log4j --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.9</version> </dependency> <!-- junit 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
二、批量操作文档 代码示例
2.1、批量创建文档 代码示例
-
批量创建文档代码示例
package com.xz.esdemo.day3; 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; /** * @description: 批量创建文档 * @author: xz */ public class EsDocBatchCreate { public static void main(String[] args) throws Exception{ // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //创建批量新增请求对象 BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(new IndexRequest().index("user").id("01").source(XContentType.JSON, "name", "zhangsan","age",20,"sex","男")); bulkRequest.add(new IndexRequest().index("user").id("02").source(XContentType.JSON, "name", "lisi","age",25,"sex","女")); bulkRequest.add(new IndexRequest().index("user").id("03").source(XContentType.JSON, "name", "wangwu","age",30,"sex","女")); bulkRequest.add(new IndexRequest().index("user").id("04").source(XContentType.JSON, "name", "wangwu1","age",18,"sex","男")); bulkRequest.add(new IndexRequest().index("user").id("05").source(XContentType.JSON, "name", "wangwu2","age",23,"sex","女")); // 客户端发送请求,获取响应对象 BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT); //打印结果信息 System.out.println("消耗时间took:" + response.getTook()); System.out.println("结果信息items:" + response.getItems()); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息,如下图表示批量创建文档成功。
-
通过postman工具查看批量创建的文档信息,如下图所示:
2.2、批量删除文档 代码示例
-
批量删除文档代码示例
package com.xz.esdemo.day3; 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; /** * @description: 批量删除文档 * @author: xz */ public class EsDocBatchDelete { public static void main(String[] args) throws Exception{ // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); //创建批量删除请求对象 BulkRequest bulkRequest = new BulkRequest(); bulkRequest.add(new DeleteRequest().index("user").id("01")); bulkRequest.add(new DeleteRequest().index("user").id("02")); bulkRequest.add(new DeleteRequest().index("user").id("03")); bulkRequest.add(new DeleteRequest().index("user").id("04")); bulkRequest.add(new DeleteRequest().index("user").id("05")); // 客户端发送请求,获取响应对象 BulkResponse response = client.bulk(bulkRequest, RequestOptions.DEFAULT); //打印结果信息 System.out.println("消耗时间took:" + response.getTook()); System.out.println("结果信息items:" + response.getItems()); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息,如下图表示批量删除文档成功。
-
通过postman工具查看批量删除的文档信息,如下图所示: