java.lang.NoSuchMethodError: org.elasticsearch.search.SearchHits.getTotalHits().value

本文解决SpringBoot整合Elasticsearch7.x版本时出现的java.lang.NoSuchMethodError错误。主要原因是SpringBoot版本与Elasticsearch版本不匹配。文中提供了一个在SpringBoot2.2.x版本下配置ES7.x的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.lang.NoSuchMethodError: org.elasticsearch.search.SearchHits.getTotalHits().value错误

构建String Boot整合Elasticsearch7.x版本时报错

原因是版本不匹配…

  1. 版本对比

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;
  1. Spring官方解答

在这里插入图片描述

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

    以下是请大佬解决版本不合的配置,可以在Spring Boot 2.2.x版本正常使用,打包后也不存在低版本ES

	<parent>
    	<!-- Spring Boot 环境-->
    </parent>
	<properties>
        <elasticsearch.version>7.8.0</elasticsearch.version>
    </properties>

    <dependencies>
        <!-- Java High Level REST Client -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.8.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>elasticsearch-rest-client</artifactId>
                    <groupId>org.elasticsearch.client</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.8.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.8.0</version>
            <scope>compile</scope>
        </dependency> 
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.56</version>
        </dependency>
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.25</version>
        </dependency>
    </dependencies>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值