java.lang.NoSuchMethodError: org.elasticsearch.search.SearchHits.getTotalHits().value错误
构建String Boot整合Elasticsearch7.x版本时报错

原因是版本不匹配…

版本对比
Spring Boot2.3.0以下不能够支持到ES7.x,个别调用相同时出现错误:
ES6.x版本调用:

long total = response.getHits().getTotalHits();

ES6.x版本源码如下:

public static final SearchHit[] EMPTY = new SearchHit[0];
    private SearchHit[] hits;
    public long totalHits;
    private float maxScore;
    ...
    public long getTotalHits() {
        return this.totalHits;
    }

ES7.x版本

long total = response.getHits().getTotalHits().value;

ES7.x源码:

public static final SearchHit[] EMPTY = new SearchHit[0];

    private final SearchHit[] hits;
    private final TotalHits totalHits;
    private final float maxScore;
    @Nullable
    private final SortField[] sortFields;
    @Nullable
    private final String collapseField;
    @Nullable
    private final Object[] collapseValues;
...
@Nullable
    public TotalHits getTotalHits() {
        return totalHits;
    }
public final class TotalHits {
/** How the {@link TotalHits#value} should be interpreted. */
  public enum Relation {
    /**
     * The total hit count is equal to {@link TotalHits#value}.
     */
    EQUAL_TO,
    /**
     * The total hit count is greater than or equal to {@link TotalHits#value}.
     */
    GREATER_THAN_OR_EQUAL_TO
  }

  /**
   * The value of the total hit count. Must be interpreted in the context of
   * {@link #relation}.
   */
	public final long value;

Spring官方解答

在这里插入图片描述

无论你在使用Spring Boot与Spring Data Elasticsearch进行整合,或者使用Spring Boot和Elasticsearch-rest-high-level-client整合都最好使用Spring Boot 2.3.x版本,不然都无法根本解决问题.

另外我的elasticsearch是7.X.0版本,Springboot就应该使用2.3.X版本
但是项目中还引入了jestClient, Springboot2.3.X不支持通过配置的方式自动注入jestClient,那只能退而求其次,通过手动的方式注入

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐