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版本,不然都无法根本解决问题.以下是请大佬解决版本不合的配置,可以在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>