目录
- 一、pom文件依赖
- 二、创建实体对象类
- 三、文档操作代码示例
- 3.1、创建文档代码示例
- 3.2、修改文档代码示例
- 3.3、查询文档代码示例
- 3.4、删除文档代码示例
一、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>
二、创建实体对象类
-
创建实体对象
package com.xz.esdemo.day2; import lombok.Data; @Data public class User { private String name; private Integer age; private String sex; }
三、文档操作代码示例
3.1、创建文档代码示例
-
创建文档代码示例
package com.xz.esdemo.day2; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.http.HttpHost; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; 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 EsDocCreate { public static void main(String[] args) throws Exception{ // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 新增文档 - 请求对象 IndexRequest indexRequest = new IndexRequest(); // 设置索引及唯一性标识 indexRequest.index("user").id("01"); // 创建数据对象 User user = new User(); user.setName("zhangsan"); user.setAge(20); user.setSex("男"); ObjectMapper objectMapper = new ObjectMapper(); String productJson = objectMapper.writeValueAsString(user); // 添加文档数据,数据格式为 JSON 格式 indexRequest.source(productJson, XContentType.JSON); // 客户端发送请求,获取响应对象 IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); System.out.println("_index:" + response.getIndex()); System.out.println("_id:" + response.getId()); System.out.println("_result:" + response.getResult()); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息,如下图表示创建文档成功。
-
通过postman工具查看创建的文档信息,如下图所示:
3.2、修改文档代码示例
-
修改文档代码示例
package com.xz.esdemo.day2; import org.apache.http.HttpHost; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; 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 EsDocUpdate { public static void main(String[] args) throws Exception{ // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 新增文档 - 请求对象 UpdateRequest updateRequest = new UpdateRequest(); // 设置索引及唯一性标识 updateRequest.index("user").id("01"); // 设置请求体,对数据进行修改 updateRequest.doc(XContentType.JSON, "sex", "女"); // 客户端发送请求,获取响应对象 UpdateResponse response = client.update(updateRequest, RequestOptions.DEFAULT); System.out.println("_index:" + response.getIndex()); System.out.println("_id:" + response.getId()); System.out.println("_result:" + response.getResult()); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息,如下图表示创建文档成功。
-
通过postman工具查看创建的文档信息,如下图所示:
3.3、查询文档代码示例
-
查询文档代码示例
package com.xz.esdemo.day2; import org.apache.http.HttpHost; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; /** * @description: 查询文档 * @author: xz */ public class EsDocGet { public static void main(String[] args) throws Exception{ // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 新增文档 - 请求对象 GetRequest getRequest = new GetRequest(); // 设置索引及唯一性标识 getRequest.index("user").id("01"); // 客户端发送请求,获取响应对象 GetResponse response = client.get(getRequest, RequestOptions.DEFAULT); System.out.println("_index:" + response.getIndex()); System.out.println("_type:" + response.getType()); System.out.println("_id:" + response.getId()); System.out.println("source:" + response.getSourceAsString()); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息,如下图表示创建文档成功。
3.4、删除文档代码示例
-
删除文档代码示例
package com.xz.esdemo.day2; import org.apache.http.HttpHost; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; /** * @description: 删除文档 * @author: xz */ public class EsDocDelete { public static void main(String[] args) throws Exception{ // 创建 es 客户端对象 RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")) ); // 新增文档 - 请求对象 DeleteRequest deleteRequest = new DeleteRequest(); // 设置索引及唯一性标识 deleteRequest.index("user").id("01"); // 客户端发送请求,获取响应对象 DeleteResponse response = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(response.toString()); // 关闭 es 客户端连接 client.close(); } }
-
执行代码,查看控制台信息,如下图表示创建文档成功。
-
通过postman工具查看创建的文档信息,如下图所示表示删除文档成功: