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

Hello guys, I know this topic has already been opened but I couldn't see any solution. I am using spring boot 2.2.6 with ES 7.6.2, spring data commons 2.2.6.

I defined my repository like this:

public interface CollaboratorRepository extends ElasticsearchRepository<Collaborator, String> {
	
}

In my CollaboratorService, I call collaboratorRepository.findAll().

I get
java.lang.NoSuchMethodError: org.elasticsearch.search.SearchHits.getTotalHits()J
at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.doCount(ElasticsearchRestTemplate.java:628) ~[spring-data-elasticsearch-3.2.6.RELEASE.jar:3.2.6.RELEASE]

Has someone an idea of the error origin ?

Welcome!

When using springboot with elasticsearch, you need to be explicit with some transitive dependencies as SpringBoot declares a version 6.4...

Basically you can put this in your pom.xml:

<properties>
  <elasticsearch.version>7.6.2<elasticsearch.version>
</properties>

See documentation here: https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-customize-dependency-versions

Thank you @dadoonet.

But I am using gradle and in my build.gradle, I declare the dependencies like this:

ext {
	springBootVersion = '2.2.6.RELEASE',
	elasticSearchVersion = '7.6.2'
}

dependencies {
    implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}"
    compile "org.springframework.boot:spring-boot-starter-web:${springBootVersion}"
    compile "org.springframework.data:spring-data-commons:${springBootVersion}"
    compile "org.springframework.data:spring-data-elasticsearch:3.2.6.RELEASE"
    compile "org.elasticsearch.client:elasticsearch-rest-high-level-client:${elasticSearchVersion}"
    compile "org.elasticsearch.client:elasticsearch-rest-client:${elasticSearchVersion}"
	compile "org.elasticsearch:elasticsearch:${elasticSearchVersion}"
    implementation "io.springfox:springfox-swagger2:2.8.0"
    implementation "io.springfox:springfox-swagger-ui:2.8.0"
    implementation "javax.xml.bind:jaxb-api"
    implementation "org.projectlombok:lombok"
    testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
    testCompile "junit:junit:4.12"
}

Is there a way to get the dependency tree in Gradle?

Of course @dadoonet but the dependency tree is very verbose.

Share it on gist.github.com if too big.

The link: https://gist.github.com/ajnfde/6b59657870c5f277ea07de11a639b014

I think that the way you wrote Elasticsearch version is wrong. May be it should be elasticsearchVersion and not elasticSearchVersion.

Look at this:

 org.elasticsearch.client:transport:6.8.7

According to the dependency tree, the transport jar is brought by spring-data-elasticsearch.

So I have updated my build.gradle by asking gradle to not include transport jar when it imports spring-data-elasticsearch. And I have declared clearly org.elasticsearch.client:transport:7.6.2

The issue always appears.

Here the dependencies declarations

dependencies {
    compile "org.elasticsearch:elasticsearch:7.6.2"
    compile "org.elasticsearch.client:transport:7.6.2"
    implementation "org.springframework.boot:spring-boot-starter:2.2.6.RELEASE"
    compile "org.springframework.boot:spring-boot-starter-web:2.2.6.RELEASE"
    compile "org.springframework.data:spring-data-commons:2.2.6.RELEASE"
    compile ("org.springframework.data:spring-data-elasticsearch:3.2.6.RELEASE"){
      exclude group: 'org.elasticsearch.client', module: 'transport'
      exclude group: 'org.elasticsearch.client', module: 'elasticsearch-rest-high-level-client'
    }
    compile "org.elasticsearch.client:elasticsearch-rest-high-level-client:7.6.2"
    compile "org.elasticsearch.client:elasticsearch-rest-client:7.6.2"
    implementation "io.springfox:springfox-swagger2:2.8.0"
    implementation "io.springfox:springfox-swagger-ui:2.8.0"
    implementation "javax.xml.bind:jaxb-api"
    implementation "org.projectlombok:lombok"
    testImplementation "org.springframework.boot:spring-boot-starter-test:2.2.6.RELEASE"
    testCompile "junit:junit:4.12"
}

Did you check the dependency tree again?

I had similar issue with "7.6.2". "spring-data-elasticsearch 3.2.6.RELEASE" expects "long" value but ES returns getTotalHits as "TotalHits" type inside "ElasticsearchRestTemplate" class.

That's because of the transitive dependencies IMO.

@Serkan_Akdemir what do you recommand ?

Here the dependency tree

+--- org.elasticsearch:elasticsearch:7.6.2
|    +--- org.elasticsearch:elasticsearch-core:7.6.2
|    +--- org.elasticsearch:elasticsearch-secure-sm:7.6.2
|    +--- org.elasticsearch:elasticsearch-x-content:7.6.2
|    |    +--- org.elasticsearch:elasticsearch-core:7.6.2
|    |    +--- org.yaml:snakeyaml:1.17 -> 1.25
|    |    +--- com.fasterxml.jackson.core:jackson-core:2.8.11 -> 2.10.3
|    |    +--- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:2.8.11 -> 2.10.3
|    |    +--- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.8.11 -> 2.10.3
|    |    \--- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.8.11 -> 2.10.3
|    +--- org.elasticsearch:elasticsearch-geo:7.6.2
|    +--- org.apache.lucene:lucene-core:8.4.0
|    +--- org.apache.lucene:lucene-analyzers-common:8.4.0
|    +--- org.apache.lucene:lucene-backward-codecs:8.4.0
|    +--- org.apache.lucene:lucene-grouping:8.4.0
|    +--- org.apache.lucene:lucene-highlighter:8.4.0
|    +--- org.apache.lucene:lucene-join:8.4.0
|    +--- org.apache.lucene:lucene-memory:8.4.0
|    +--- org.apache.lucene:lucene-misc:8.4.0
|    +--- org.apache.lucene:lucene-queries:8.4.0
|    +--- org.apache.lucene:lucene-queryparser:8.4.0
|    +--- org.apache.lucene:lucene-sandbox:8.4.0
|    +--- org.apache.lucene:lucene-spatial:8.4.0
|    +--- org.apache.lucene:lucene-spatial-extras:8.4.0
|    +--- org.apache.lucene:lucene-spatial3d:8.4.0
|    +--- org.apache.lucene:lucene-suggest:8.4.0
|    +--- org.elasticsearch:elasticsearch-cli:7.6.2
|    |    +--- net.sf.jopt-simple:jopt-simple:5.0.2
|    |    \--- org.elasticsearch:elasticsearch-core:7.6.2
|    +--- com.carrotsearch:hppc:0.8.1
|    +--- joda-time:joda-time:2.10.4 -> 2.10.5
|    +--- com.tdunning:t-digest:3.2
|    +--- org.hdrhistogram:HdrHistogram:2.1.9
|    +--- org.apache.logging.log4j:log4j-api:2.11.1 -> 2.12.1
|    \--- org.elasticsearch:jna:4.5.1
+--- org.elasticsearch.client:transport:7.6.2
|    +--- org.elasticsearch:elasticsearch:7.6.2 (*)
|    +--- org.elasticsearch.plugin:transport-netty4-client:7.6.2 -> 6.8.7
|    |    +--- io.netty:netty-buffer:4.1.32.Final -> 4.1.48.Final
|    |    +--- io.netty:netty-codec:4.1.32.Final -> 4.1.48.Final
|    |    +--- io.netty:netty-codec-http:4.1.32.Final -> 4.1.48.Final
|    |    |    \--- io.netty:netty-handler:4.1.48.Final
|    |    +--- io.netty:netty-common:4.1.32.Final -> 4.1.48.Final
|    |    +--- io.netty:netty-handler:4.1.32.Final -> 4.1.48.Final
|    |    +--- io.netty:netty-resolver:4.1.32.Final -> 4.1.48.Final
|    |    \--- io.netty:netty-transport:4.1.32.Final -> 4.1.48.Final
|    +--- org.elasticsearch.plugin:reindex-client:7.6.2
|    |    +--- org.elasticsearch.client:elasticsearch-rest-client:7.6.2
|    |    |    +--- org.apache.httpcomponents:httpclient:4.5.10 -> 4.5.12
|    |    |    +--- org.apache.httpcomponents:httpcore:4.4.12 -> 4.4.13
|    |    |    +--- org.apache.httpcomponents:httpasyncclient:4.1.4
|    |    |    +--- org.apache.httpcomponents:httpcore-nio:4.4.12 -> 4.4.13
|    |    |    \--- commons-codec:commons-codec:1.11 -> 1.13
|    |    \--- org.elasticsearch:elasticsearch-ssl-config:7.6.2
|    |         \--- org.elasticsearch:elasticsearch-core:7.6.2
|    +--- org.elasticsearch.plugin:lang-mustache-client:7.6.2
|    |    \--- com.github.spullara.mustache.java:compiler:0.9.6
|    +--- org.elasticsearch.plugin:percolator-client:7.6.2
|    +--- org.elasticsearch.plugin:parent-join-client:7.6.2
|    \--- org.elasticsearch.plugin:rank-eval-client:7.6.2
+--- org.springframework.boot:spring-boot-starter-web:2.2.6.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:2.2.6.RELEASE
|    |    +--- org.springframework.boot:spring-boot:2.2.6.RELEASE
|    |    |    +--- org.springframework:spring-core:5.2.5.RELEASE
|    |    |    |    \--- org.springframework:spring-jcl:5.2.5.RELEASE
|    |    |    \--- org.springframework:spring-context:5.2.5.RELEASE
|    |    |         +--- org.springframework:spring-aop:5.2.5.RELEASE
|    |    |         |    +--- org.springframework:spring-beans:5.2.5.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:5.2.5.RELEASE (*)
|    |    |         |    \--- org.springframework:spring-core:5.2.5.RELEASE (*)
|    |    |         +--- org.springframework:spring-beans:5.2.5.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:5.2.5.RELEASE (*)
|    |    |         \--- org.springframework:spring-expression:5.2.5.RELEASE
|    |    |              \--- org.springframework:spring-core:5.2.5.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:2.2.6.RELEASE
|    |    |    \--- org.springframework.boot:spring-boot:2.2.6.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:2.2.6.RELEASE
|    |    |    +--- ch.qos.logback:logback-classic:1.2.3
|    |    |    |    +--- ch.qos.logback:logback-core:1.2.3
|    |    |    |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
|    |    |    +--- org.apache.logging.log4j:log4j-to-slf4j:2.12.1
|    |    |    |    +--- org.slf4j:slf4j-api:1.7.25 -> 1.7.30
|    |    |    |    \--- org.apache.logging.log4j:log4j-api:2.12.1
|    |    |    \--- org.slf4j:jul-to-slf4j:1.7.30
|    |    |         \--- org.slf4j:slf4j-api:1.7.30
|    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
|    |    +--- org.springframework:spring-core:5.2.5.RELEASE (*)
|    |    \--- org.yaml:snakeyaml:1.25
|    +--- org.springframework.boot:spring-boot-starter-json:2.2.6.RELEASE
|    |    +--- org.springframework.boot:spring-boot-starter:2.2.6.RELEASE (*)
|    |    +--- org.springframework:spring-web:5.2.5.RELEASE
|    |    |    +--- org.springframework:spring-beans:5.2.5.RELEASE (*)
|    |    |    \--- org.springframework:spring-core:5.2.5.RELEASE (*)
|    |    +--- com.fasterxml.jackson.core:jackson-databind:2.10.3
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.10.3
|    |    |    \--- com.fasterxml.jackson.core:jackson-core:2.10.3
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.10.3
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.10.3
|    |    |    \--- com.fasterxml.jackson.core:jackson-databind:2.10.3 (*)
|    |    +--- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.10.3
|    |    |    +--- com.fasterxml.jackson.core:jackson-annotations:2.10.3
|    |    |    +--- com.fasterxml.jackson.core:jackson-core:2.10.3
|    |    |    \--- com.fasterxml.jackson.core:jackson-databind:2.10.3 (*)
|    |    \--- com.fasterxml.jackson.module:jackson-module-parameter-names:2.10.3
|    |         +--- com.fasterxml.jackson.core:jackson-core:2.10.3
|    |         \--- com.fasterxml.jackson.core:jackson-databind:2.10.3 (*)
|    +--- org.springframework.boot:spring-boot-starter-tomcat:2.2.6.RELEASE
|    |    +--- jakarta.annotation:jakarta.annotation-api:1.3.5
|    |    +--- org.apache.tomcat.embed:tomcat-embed-core:9.0.33
|    |    +--- org.apache.tomcat.embed:tomcat-embed-el:9.0.33
|    |    \--- org.apache.tomcat.embed:tomcat-embed-websocket:9.0.33
|    |         \--- org.apache.tomcat.embed:tomcat-embed-core:9.0.33
|    +--- org.springframework.boot:spring-boot-starter-validation:2.2.6.RELEASE
|    |    +--- org.springframework.boot:spring-boot-starter:2.2.6.RELEASE (*)
|    |    +--- jakarta.validation:jakarta.validation-api:2.0.2
|    |    \--- org.hibernate.validator:hibernate-validator:6.0.18.Final
|    |         +--- org.jboss.logging:jboss-logging:3.3.2.Final -> 3.4.1.Final
|    |         \--- com.fasterxml:classmate:1.3.4 -> 1.5.1
|    +--- org.springframework:spring-web:5.2.5.RELEASE (*)
|    \--- org.springframework:spring-webmvc:5.2.5.RELEASE
|         +--- org.springframework:spring-aop:5.2.5.RELEASE (*)
|         +--- org.springframework:spring-beans:5.2.5.RELEASE (*)
|         +--- org.springframework:spring-context:5.2.5.RELEASE (*)
|         +--- org.springframework:spring-core:5.2.5.RELEASE (*)
|         +--- org.springframework:spring-expression:5.2.5.RELEASE (*)
|         \--- org.springframework:spring-web:5.2.5.RELEASE (*)
+--- org.springframework.data:spring-data-commons:2.2.6.RELEASE
|    +--- org.springframework:spring-core:5.2.5.RELEASE (*)
|    +--- org.springframework:spring-beans:5.2.5.RELEASE (*)
|    \--- org.slf4j:slf4j-api:1.7.26 -> 1.7.30
+--- org.springframework.data:spring-data-elasticsearch:3.2.6.RELEASE
|    +--- org.springframework:spring-context:5.2.5.RELEASE (*)
|    +--- org.springframework:spring-tx:5.2.5.RELEASE
|    |    +--- org.springframework:spring-beans:5.2.5.RELEASE (*)
|    |    \--- org.springframework:spring-core:5.2.5.RELEASE (*)
|    +--- org.springframework.data:spring-data-commons:2.2.6.RELEASE (*)
|    +--- joda-time:joda-time:2.10.5
|    +--- org.elasticsearch.plugin:transport-netty4-client:6.8.7 (*)
|    +--- com.fasterxml.jackson.core:jackson-core:2.10.3
|    +--- com.fasterxml.jackson.core:jackson-databind:2.10.3 (*)
|    \--- org.slf4j:slf4j-api:1.7.26 -> 1.7.30
+--- org.elasticsearch.client:elasticsearch-rest-high-level-client:7.6.2
|    +--- org.elasticsearch:elasticsearch:7.6.2 (*)
|    +--- org.elasticsearch.client:elasticsearch-rest-client:7.6.2 (*)
|    +--- org.elasticsearch.plugin:mapper-extras-client:7.6.2
|    +--- org.elasticsearch.plugin:parent-join-client:7.6.2
|    +--- org.elasticsearch.plugin:aggs-matrix-stats-client:7.6.2
|    +--- org.elasticsearch.plugin:rank-eval-client:7.6.2
|    \--- org.elasticsearch.plugin:lang-mustache-client:7.6.2 (*)
\--- org.elasticsearch.client:elasticsearch-rest-client:7.6.2 (*)

Hi Team,

I do have the same issue, with the same with 3.2.6 and 7.6.1versions:

java.lang.NoSuchMethodError: org.elasticsearch.search.SearchHits.getTotalHits()J
	at org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.doCount(ElasticsearchRestTemplate.java:606) ~[spring-data-elasticsearch-3.2.6.RELEASE.jar:3.2.6.RELEASE]

Did anyone find a solution to it?

You need to use Spring Data Elasticsearch 4.0.0.RC1.

https://docs.spring.io/spring-data/elasticsearch/docs/4.0.0.RC1/reference/html/#reference

As @dadoonet mentioned, either use 4.0.0.RC1or use direct elasticsearch java api instead of Spring-data-es.

You can look at this sample application for this:

Thank you @dadoonet and @Serkan_Akdemir. I will try your solutions. Definitely I think there is a mistake either in the spring-data-elasticsearch 3.2.6.release version, either in the elasticsearch 7.6.2 version.

In spring-data-elasticsearch 3.2.6.release version, the method doCount of ElasticsearchRestTemplate class has to return a long value but it returns response.getHits().getTotalHits() which refers to SearchHits class getTotalHits() method which return TotalHits object.