文章目录
- 版本信息记录
- 使用Java API配置ES客户端实现集成
- pom.xml添加相关的Dependency
- 使用javaConfig构建RestHighLevelClient对象
- 给ES中存储文档数据,当然也可以更新文档数据
- search API:搜索 address 中包含mill的所有人的年龄分布以及平均年龄
- search API:按照年龄聚合,并且请求这些年龄段的这些人的平均薪资
版本信息记录
本博客记录SpringBoot集成ES相关组件的版本信息如下
名称 | 版本号 |
---|---|
ES服务端 | 7.14.0 |
SpringBoot | 2.6.13 |
elasticsearch-rest-high-level-client | 7.15.2 |
温馨提示:部分配置可能因版本而存在差异,比如Java Client API当前版本就和最新版本存在巨大差异。因此具体解决方案请参照官方文档 《Elasticsearch-client》
使用Java API配置ES客户端实现集成
官方文档入口
pom.xml添加相关的Dependency
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
使用javaConfig构建RestHighLevelClient对象
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(esHost, port, schema)));
return client;
}
官方提供了如下集群模式
下的多节点配置示例:
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
在此也可以将RequestOptions
进行配置
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// builder.addHeader("Authorization", "Bearer " + TOKEN);
// builder.setHttpAsyncResponseConsumerFactory(
// new HttpAsyncResponseConsumerFactory
// .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
完成的JavaConfig配置如下图所示:
给ES中存储文档数据,当然也可以更新文档数据
首先使用如下代码片段给ES的指定Index中存储文档
@Test
void testIndexRequest() throws IOException {
// 即将存储到ES中的文档
Product product = new Product("bk-1", "City bike", 123.0);
// 指定Index
IndexRequest indexReq = new IndexRequest("product");
// 给该文档设置id,默认也会自动生成
indexReq.id(product.getSkuId());
// 将product实例序列化
indexReq.source(JSON.toJSONString(product), XContentType.JSON);
// 进行存储操作
IndexResponse response = restHighLevelClient.index(indexReq, ElasticSearchConfig.COMMON_OPTIONS);
System.out.println("response = " + response);
}
如图所示,存储文档成功;
如果想更新该文档中的数据也可以使用如上代码片段,如图所示
search API:搜索 address 中包含mill的所有人的年龄分布以及平均年龄
@Test
void testESSearch() throws IOException {
// 构建一个查询请求,指定的Index=bank
SearchRequest searchReq = new SearchRequest("bank");
// SearchSourceBuilder就是DSL;即检索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// query match_all
searchSourceBuilder.query(QueryBuilders.termQuery("address","mill"));
// 构建age分布的聚合
TermsAggregationBuilder ageAgg = AggregationBuilders
.terms("ageAgg").field("age").size(10);
searchSourceBuilder.aggregation(ageAgg);
// 构建age的平均聚合
AvgAggregationBuilder ageAvg = AggregationBuilders
.avg("ageAvg").field("age");
searchSourceBuilder.aggregation(ageAvg);
// 将检索条件封装给查询请求
searchReq.source(searchSourceBuilder);
SearchResponse result = restHighLevelClient.search(searchReq, ElasticSearchConfig.COMMON_OPTIONS);
System.out.println("result = " + result);
}
search API:按照年龄聚合,并且请求这些年龄段的这些人的平均薪资
@Test
void testAgeAvg() throws IOException {
SearchRequest searchReq = new SearchRequest("bank");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery())
.aggregation(AggregationBuilders.terms("ageAgg").field("age").size(30)
.subAggregation(AggregationBuilders.avg("ageGroupBalanceAvg").field("balance")));
searchReq.source(sourceBuilder);
SearchResponse result = restHighLevelClient.search(searchReq, ElasticSearchConfig.COMMON_OPTIONS);
System.out.println("result = " + result);
}