:::tips 与ElasticSearch实现交互的所有API都封装RestHighLevelClient类中,必须先引入依赖,然后完成这个对象的初始化,建立与ElasticSearch的连接 :::
引入依赖
:::tips 在项目中引入RestHighLevelClient的依赖 :::
<!-- RestHighLevelClient依赖 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
初始化连接
:::tips 因为需要频繁使用RestHighLevelClient连接,所以我们将它注册到Spring容器中,需要用到时直接注入即可 :::
@Configuration
public class XxxConfig{
//将RestHighLevelClient对象注册到Spring容器中
@Bean
public RestHighLevelClient restHighLevelClient(){
return new RestHighLevelClient(RestClient.builder(HttpHost.create("http://IP地址:端口号")));
}
}