zoukankan      html  css  js  c++  java
  • Elasticsearch 升级 7.x 版本后,我感觉掉坑里了!

    转载自:https://www.jianshu.com/p/60c3b9cf1153

    遇到的问题

    选择好了合适的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了。
  • 相关阅读:
    Emmet 语法
    GitHub常用命令
    ProgressBar.js – 漂亮的响应式 SVG 进度条
    99个漂亮的注册和登录页设计(附PSD)
    android Acitivity之间的几种传值方式(^_^)
    Android 动态生成 EditTest
    Android 小笔记
    winfrom获取用户控件里的控件对象
    MVC+Easeyui dialog的小问题
    bootStrap
  • 原文地址:https://www.cnblogs.com/sheng-se/p/15098792.html
Copyright © 2011-2022 走看看