关于java:Elasticsearch-升级-7x-版本后我感觉掉坑里了

12次阅读

共计 3931 个字符,预计需要花费 10 分钟才能阅读完成。

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 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 对象,具体比照如下;

  • 咱们还发现原来应用的 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!

正文完
 0