elasticsearch aggregations_Elasticsearch 升级 7.x 版本后,我感觉掉坑里了!

最近想把我的mall项目升级下,支持SpringBoot 2.3.0 版本。升级过程中发现需要升级Elasticsearch到7.x版本,学习过我的mall项目的朋友应该知道, 我用的Elasticsearch是6.x版本,升级到7.x以后ElasticsearchTemplate都不让用了。本文记录了Elasticsearch从6.x升级到7.x所遇到的一些问题,给大家排排坑!

版本选择

既然我们要升级到Elasticsearch7.x版本,首先要选择合适的版本。如何选择合适的版本,这里有个小技巧分享给大家。

  • 首先我们可以在pom.xml中修改SpringBoot依赖的版本为2.3.0
<parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>2.3.0.RELEASEversion>
    <relativePath/> 
parent>
  • 然后在项目的External Libraries中搜索elasticsearch,可以发现elasticsearch-7.6.2.jar这个依赖;
f195e59bf0fe81b9a77e7c1dd79b75d2.png
  • 然后打开其中的MANIFEST.MF文件,通过jar包中的X-Compile-Elasticsearch-Version属性,我们可以找到兼容的Elasticsearch版本号为7.6.2
68362ddb797a103ff24dad521c2b2f65.png
  • 之前还有试过两个版本6.2.2版本和7.4.0版本,发现与SpringBoot 2.3.0 都有兼容性问题,所以选择合适的版本很重要!

  • 还有一点值得注意的是,如果你使用了中文分词器(IK Analysis),也要选择对应的版本7.6.2,对于使用Kibana和Logstash也是如此。

遇到的问题

选择好了合适的Elasticsearch版本后,接下来我们来讲讲升级版本遇到的问题了!

  • application.yml中,原来我们用来配置Elasticsearch访问路径和集群名称的配置已经不建议使用了;
caf0eca49d3f8cdeb05b9bf01b19c942.png
  • 取而代之的是直接配置Elasticsearch的rest访问地址;
spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200
  • 其实最大的问题还是ElasticsearchTemplate已经过时了,不建议使用了,之前复杂的数据操作用到了它;
55d207ba5692a6690d7e7d4c3dcbbc46.png
  • 推荐使用的是ElasticsearchRestTemplate,这大概就是修改application.yml中那两个配置的原因了,修改为使用ElasticsearchRestTemplate后,我们可以发现原来ElasticsearchTemplate的query()方法已经没有了;
56affa17491d8ed405be67d9d407ea6f.png
  • 可以使用ElasticsearchRestTemplate的search()方法来代替,原来的复杂查询将有以下改进;
// 使用ElasticsearchTemplate进行复杂查询
return elasticsearchTemplate.query(searchQuery, response -> {
    LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
    return convertProductRelatedInfo(response);
});
// 使用ElasticsearchRestTemplate进行复杂查询
SearchHits searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);return convertProductRelatedInfo(searchHits);
  • 我们转换聚合结果对象的方法convertProductRelatedInfo也改进下,只是改变了方法参数类型而已;
//改进前
private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) {
    //省略方法体代码...
}
//改进后
private EsProductRelatedInfo convertProductRelatedInfo(SearchHits response) {
    //省略方法体代码...
}
  • 如果你觉得这样就行了,那你调用下接口就会发现,报了个类型转换异常;
2020-07-21 14:40:48.154 ERROR 11616 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is java.lang.ClassCastException: org.elasticsearch.search.aggregations.bucket.nested.ParsedNested cannot be cast to org.elasticsearch.search.aggregations.bucket.nested.InternalNested] with root cause

java.lang.ClassCastException: org.elasticsearch.search.aggregations.bucket.nested.ParsedNested cannot be cast to org.elasticsearch.search.aggregations.bucket.nested.InternalNested
 at com.macro.mall.tiny.service.impl.EsProductServiceImpl.convertProductRelatedInfo(EsProductServiceImpl.java:254) ~[classes/:na]
 at com.macro.mall.tiny.service.impl.EsProductServiceImpl.searchRelatedInfo(EsProductServiceImpl.java:229) ~[classes/:na]
 at com.macro.mall.tiny.controller.EsProductController.searchRelatedInfo(EsProductController.java:104) ~[classes/:na]
  • 我们对该问题进行修复,主要就是原来的Terms对象都被改为了ParsedTerms相关对象,比如说StringTerms被改为了ParsedStringTerms对象,具体对比如下;
321920096fb8a73486dd9aac19fce008.png
  • 我们还发现原来使用的ElasticsearchRepository的search()方法也过时了,不建议使用了,我们以前用它做了一些复杂查询;
825dfed13bf76f0ca4939edf4acf7083.png
  • 我们可以改用ElasticsearchRestTemplate的search()方法来实现,具体实现对比如下;
// ElasticsearchRepository实现复杂搜索
return productRepository.search(searchQuery)
// ElasticsearchRestTemplate实现复杂搜索
SearchHits searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);if(searchHits.getTotalHits()<=0){return new PageImpl<>(null,pageable,0);
}
List searchProductList = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());return new PageImpl<>(searchProductList,pageable,searchHits.getTotalHits());

总结

Elasticsearch从6.x升级到7.x改动还真不是一般的大,ElasticsearchTemplate不建议使用了,改为使用ElasticsearchRestTemplate,ElasticsearchRepository实现复杂查询的方法也不建议使用了。从此我们简单的数据操作可以使用ElasticsearchRepository,而复杂的数据操作只能使用ElasticsearchRestTemplate了。

项目源码地址

https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-elasticsearch

推荐阅读

  • 肝了两天IntelliJ IDEA 2020,解锁11种新姿势, 真香!!!
  • 在大公司做程序员 vs 在小公司做程序员
  • 微服务聚合Swagger文档,这波操作是真的香!
  • 给Swagger换了个新皮肤,瞬间高大上了!
  • SpringBoot 2.3.0 新特性一览,快来跟我实践一波!
  • 给Swagger升级了新版本,没想到居然有这么多坑!
  • 微服务权限终极解决方案,Spring Cloud Gateway + Oauth2 实现统一认证和鉴权!
  • 听说你的JWT库用起来特别扭,推荐这款贼好用的!
  • 一个不容错过的Spring Cloud实战项目!
  • 我的Github开源项目,从0到20000 Star!
 

e78b704580378330226ff944f7951443.png

欢迎关注,点个在看

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值