SpringBoot实战电商我的项目mall(35k+star)地址:https://github.com/macrozheng/mall
摘要
最近想把我的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.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --></parent>
- 而后在我的项目的
External Libraries
中搜寻elasticsearch
,能够发现elasticsearch-7.6.2.jar
这个依赖;
- 而后关上其中的
MANIFEST.MF
文件,通过jar包中的X-Compile-Elasticsearch-Version
属性,咱们能够找到兼容的Elasticsearch版本号为7.6.2
;
- 之前还有试过两个版本
6.2.2
版本和7.4.0
版本,发现与SpringBoot 2.3.0 都有兼容性问题,所以抉择适合的版本很重要! - 还有一点值得注意的是,如果你应用了中文分词器(IK Analysis),也要抉择对应的版本
7.6.2
,对于应用Kibana和Logstash也是如此。
遇到的问题
抉择好了适合的Elasticsearch版本后,接下来咱们来讲讲降级版本遇到的问题了!
- 在
application.yml
中,原来咱们用来配置Elasticsearch拜访门路和集群名称的配置曾经不倡议应用了;
- 取而代之的是间接配置Elasticsearch的rest拜访地址;
spring: elasticsearch: rest: uris: http://localhost:9200
- 其实最大的问题还是ElasticsearchTemplate曾经过期了,不倡议应用了,之前简单的数据操作用到了它;
- 举荐应用的是ElasticsearchRestTemplate,这大略就是批改
application.yml
中那两个配置的起因了,批改为应用ElasticsearchRestTemplate后,咱们能够发现原来ElasticsearchTemplate的query()
办法曾经没有了;
- 能够应用ElasticsearchRestTemplate的
search()
办法来代替,原来的简单查问将有以下改良;
// 应用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);
- 咱们转换聚合后果对象的办法
convertProductRelatedInfo
也改良下,只是扭转了办法参数类型而已;
//改良前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 causejava.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对象,具体比照如下;
- 咱们还发现原来应用的ElasticsearchRepository的
search()
办法也过期了,不倡议应用了,咱们以前用它做了一些简单查问;
- 咱们能够改用ElasticsearchRestTemplate的
search()
办法来实现,具体实现比照如下;
// 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...
> 本文 GitHub [https://github.com/macrozheng/mall-learning](https://github.com/macrozheng/mall-learning) 曾经收录,欢送大家Star!