Elasticsearch-Rest-Client:官方RestClient,封装了ES操作,API层次分明,上手简单。
1. 导入依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.5.0</version>
</dependency>
2. 编写配置
@Configuration
public class ElasticSearchConfig{public static final RequestOptions COMMON_OPTIONS;static{RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();COMMON_OPTIONS = builder.build();}@Beanpublic RestHighLevelClient esRestClient(){RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.100.120",9200,"http")));return client;}
}
3. 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {@AutowiredRestHighLevelClient client;@Testpublic void loadClient(){System.out.println(client);}
}