Springboot集成ElasticSearch

tech2024-02-20  73

配置工作

需导入elasticsearch依赖,注意与电脑上安装的elasticsearch版本保持一致 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>

2.编写elasticsearch配置类

/** * ElasticSearch配置类 */ @Configuration public class ElasticSearchClientConfig { public RestHighLevelClient restHighLevelClient(){ RestHighLevelClient restHighLevelClient=new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1",9200,"http"))); return restHighLevelClient; } }

索引API操作测试

/* 高级客户端测试 */ @SpringBootTest class SpringbootElasticsearchApplicationTests { /** * 注入RestHighLevelClient * 须保证restHighLevelClient能跳转到自己编写的elasticsearch配置类 */ @Autowired RestHighLevelClient restHighLevelClient; //测试创建索引 @Test void CreateIndexDemo() throws IOException { //创建索引请求 CreateIndexRequest createIndexRequest = new CreateIndexRequest("wzl_1"); //客户端执行请求IndicesClient,请求后获得响应 CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); System.out.println(createIndexResponse); } //测试获取索引 @Test void GetIndexDemo() throws IOException { //获取索引请求 GetIndexRequest getIndexRequest = new GetIndexRequest("wzl_1"); //客户端执行请求IndicesClient,请求后获得bool值 boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT); //elasticsearch存在该索引则打印true,不存在则打印false System.out.println(exists); } //测试删除索引 @Test void DelIndexDemo() throws IOException { //删除索引请求 DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("wzl_1"); //客户端执行请求IndicesClient,请求后获得bool值 AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); //打印出是否删除成功,成功返回true,失败返回false System.out.println(delete.isAcknowledged()); } }

文档API操作测试

//测试创建文档 @Test void CreateDocDemo() throws IOException { //创建对象 User user = new User("吴木木", 21); //创建请求 IndexRequest indexRequest = new IndexRequest("wzl_1"); indexRequest.id("1"); //设置无法立即执行操作时等待的超时时间 indexRequest.timeout(TimeValue.timeValueSeconds(1)); //将数据转为json放入请求 indexRequest.source(JSON.toJSONString(user), XContentType.JSON); //客户端发送请求 IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); //打印出响应的结果 System.out.println(indexResponse.toString()); //打印出对应命令的返回状态 System.out.println(indexResponse.status()); } //测试获取文档的信息 @Test void getDocDemo() throws IOException { //创建请求 GetRequest getRequest = new GetRequest("wzl_1", "1"); //客户端发送请求 GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); //打印出结果,文档的结果 System.out.println(documentFields.getSourceAsString()); //打印出的内容与Kibana模式一样的 System.out.println(documentFields); } //测试更新文档的信息 @Test void updateDocDemo() throws IOException { //创建请求 UpdateRequest updateRequest = new UpdateRequest("wzl_1", "1"); //设置无法立即执行操作时等待的超时时间 updateRequest.timeout(TimeValue.timeValueSeconds(1)); //创建修改对象 User user = new User("吴", 10); //将数据转为json放入请求 updateRequest.doc(JSON.toJSONString(user), XContentType.JSON); //客户端发送请求 UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT); //打印出响应的状态 System.out.println(updateResponse.status()); } //测试删除文档的信息 @Test void deleteDocDemo() throws IOException { //创建请求 DeleteRequest deleteRequest = new DeleteRequest("wzl_1", "1"); //客户端发送请求 DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT); //打印出响应的状态 System.out.println(delete.status()); } //测批量插入文档信息 @Test void batchInsertDemo() throws IOException { //创建请求 BulkRequest bulkRequest = new BulkRequest(); //设置无法立即执行操作时等待的超时时间 bulkRequest.timeout(TimeValue.timeValueSeconds(10)); //创建List集合用来存储User对象 List<User> userList = new ArrayList<>(); //创建多个对象,并添加到List集合中 userList.add(new User("木木一号", 21)); userList.add(new User("木木二号", 21)); userList.add(new User("木木三号", 21)); userList.add(new User("木木四号", 21)); //批处理请求 for (int i = 0; i < userList.size(); i++) { bulkRequest.add(new IndexRequest("wzl_1") .id("" + (i + 1)) .source(JSON.toJSONString(userList.get(i)), XContentType.JSON)); } //客户端发送请求 BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); //打印状态,状态为是否失败,失败返回true,成功返回true System.out.println(bulk.hasFailures()); } //测批量删除文档信息 @Test void batchDelDemo() throws IOException { //创建请求 BulkRequest bulkRequest = new BulkRequest(); //设置无法立即执行操作时等待的超时时间 bulkRequest.timeout(TimeValue.timeValueSeconds(10)); //批处理请求 for (int i = 1; i < 5; i++) { bulkRequest.add(new DeleteRequest("wzl_1", "" + i)); } //客户端发送请求 BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); //打印状态,状态为是否失败,失败返回true,成功返回true System.out.println(bulk.hasFailures()); } //查询 @Test void searchDemo() throws IOException { //创建请求 SearchRequest searchRequest = new SearchRequest("wzl_1"); //构建搜索条件 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //精确匹配,查询条件 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "21"); searchSourceBuilder.query(termQueryBuilder); //searchSourceBuilder放入请求 searchRequest.source(searchSourceBuilder); //发送请求 SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); //打印 System.out.println(JSON.toJSONString(search.getHits())); //循环遍历打印出查询到的每个数据 for (SearchHit filed: search.getHits().getHits()) { System.out.println(filed.getSourceAsMap()); } }

学习☞:https://www.bilibili.com/video/BV17a4y1x7zq?p=14

最新回复(0)