:::tips 与ElasticSearch实现交互的所有API都封装RestHighLevelClient类中,必须先引入依赖,然后完成这个对象的初始化,建立与ElasticSearch的连接 :::

引入依赖

:::tips 在项目中引入RestHighLevelClient的依赖 :::

  1. <!-- RestHighLevelClient依赖 -->
  2. <dependency>
  3. <groupId>org.elasticsearch.client</groupId>
  4. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  5. </dependency>

初始化连接

:::tips 因为需要频繁使用RestHighLevelClient连接,所以我们将它注册到Spring容器中,需要用到时直接注入即可 :::

  1. @Configuration
  2. public class XxxConfig{
  3. //将RestHighLevelClient对象注册到Spring容器中
  4. @Bean
  5. public RestHighLevelClient restHighLevelClient(){
  6. return new RestHighLevelClient(RestClient.builder(HttpHost.create("http://IP地址:端口号")));
  7. }
  8. }