ElaticSearch 学习三(springboot 集成ES 7.6.1)
Posted 谢月
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElaticSearch 学习三(springboot 集成ES 7.6.1)相关的知识,希望对你有一定的参考价值。
一、创建springboot项目
1.此处创建简单的springboot项目,不做过多赘述
我的springboot版本是2.4.5,装的ES是7.6.1的
二、导入elasticsearch依赖
1.pom.xml中添加elasticsearch相关依赖,如下:注意这里依赖的版本与es库版本需要一致
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.6.1</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.6.1</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.6.1</version> </dependency>
三、定义elasticsearch的配置类,设置elasticsearch库信息
创建ElasticSearchClientConfig类,目录结构如下:
配置基本信息代码如下:
package com.elasticsearch.esapi.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchClientConfig
@Bean
public RestHighLevelClient restHighLevelClient()
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1",9200,"http")
//可配置多个作为集群
//new HttpHost("127.0.0.1",9300,"http")
));
return restHighLevelClient;
四、对索引进进行增删查操作
在springboot的测试类中进行测试,代码如下:
package com.elasticsearch.esapi;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class EsApiApplicationTests
// 以下引用bean方式任选其一均可
/*@Autowired
private RestHighLevelClient restHighLevelClient;*/
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
// 创建索引,名为springboot_api
@Test
void createIndex() throws IOException
// 1.创建索引请求
CreateIndexRequest createIndex = new CreateIndexRequest("springboot_api");
// 2.客户端获取indices
IndicesClient indices = client.indices();
// 3.indices执行请求,获取响应
CreateIndexResponse createIndexResponse = indices.create(createIndex, RequestOptions.DEFAULT);
// 4.打印查看响应结果
System.out.println(createIndexResponse);
// 判断索引是否存在
@Test
void IndexExitIs() throws IOException
// 1.创建索引请求
GetIndexRequest getIndex = new GetIndexRequest("springboot_api");
// 2.客户端获取indices
IndicesClient indices = client.indices();
// 3.indices执行请求,获取响应
boolean exitIs = indices.exists(getIndex, RequestOptions.DEFAULT);
// 4.打印查看响应结果
System.out.println("springboot_es索引是否存在:"+exitIs);
// 删除索引
@Test
void deleteIndex() throws IOException
// 1.创建索引请求
DeleteIndexRequest deleteIndex = new DeleteIndexRequest("springboot_api");
// 2.客户端获取indices
IndicesClient indices = client.indices();
// 3.indices执行请求,获取响应
AcknowledgedResponse deleteResponse = indices.delete(deleteIndex, RequestOptions.DEFAULT);
// 4.打印查看响应结果
System.out.println("springboot_es索引是否删除成功:"+deleteResponse.isAcknowledged());
以上是关于ElaticSearch 学习三(springboot 集成ES 7.6.1)的主要内容,如果未能解决你的问题,请参考以下文章