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

2020-07-29  本文已影响0人  java梦想口服液

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

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

版本选择

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

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
Elasticsearch 升级 7.x 版本后,我感觉掉坑里了 Elasticsearch 升级 7.x 版本后,我感觉掉坑里了

遇到的问题

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

Elasticsearch 升级 7.x 版本后,我感觉掉坑里了
spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200
Elasticsearch 升级 7.x 版本后,我感觉掉坑里了 Elasticsearch 升级 7.x 版本后,我感觉掉坑里了
// 使用ElasticsearchTemplate进行复杂查询
return elasticsearchTemplate.query(searchQuery, response -> {
    LOGGER.info("DSL:{}",searchQuery.getQuery().toString());
    return convertProductRelatedInfo(response);
});
// 使用ElasticsearchRestTemplate进行复杂查询
SearchHits<EsProduct> searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);
return convertProductRelatedInfo(searchHits);
//改进前
private EsProductRelatedInfo convertProductRelatedInfo(SearchResponse response) {
    //省略方法体代码...
}
//改进后
private EsProductRelatedInfo convertProductRelatedInfo(SearchHits<EsProduct> 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]
Elasticsearch 升级 7.x 版本后,我感觉掉坑里了 Elasticsearch 升级 7.x 版本后,我感觉掉坑里了
// ElasticsearchRepository实现复杂搜索
return productRepository.search(searchQuery)
// ElasticsearchRestTemplate实现复杂搜索
SearchHits<EsProduct> searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);
if(searchHits.getTotalHits()<=0){
    return new PageImpl<>(null,pageable,0);
}
List<EsProduct> 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

上一篇下一篇

猜你喜欢

热点阅读