elastic / elasticsearch-java

Official Elasticsearch Java Client
Apache License 2.0
397 stars 228 forks source link

Encountering NoSuchMethodError when Creating Elasticsearch Client #811

Closed Trio-official closed 2 months ago

Trio-official commented 2 months ago

Java API client version

8.12.2

Java version

1.8.0_371

Elasticsearch Version

8.12.0

Problem description

I encountered a NoSuchMethodError when attempting to create an Elasticsearch client object in my Spring Boot Maven project. The error occurred specifically at the instantiation of the RestClientTransport object within the Elasticsearch Java API.

Error Message:

Exception in thread "main" java.lang.NoSuchMethodError: org.elasticsearch.client.RequestOptions$Builder.setHttpAsyncResponseConsumerFactory(Lorg/elasticsearch/client/HttpAsyncResponseConsumerFactory;)Lorg/elasticsearch/client/RequestOptions$Builder;

Code Snippet:

try {
    RestClient restClient = RestClient.builder(
            new HttpHost("localhost", 9200, "http")).build();
    ElasticsearchTransport transport = new RestClientTransport(
            restClient, new JacksonJsonpMapper());
    ElasticsearchClient esClient = new ElasticsearchClient(transport);
    System.out.println("ESClient "+esClient);
} catch(Exception ex) {
    System.out.println("Unable to create Elasticsearch client: "+ ex);
    // Handle exception appropriately
}

Imported Dependencies:

<dependency>
      <groupId>co.elastic.clients</groupId>
      <artifactId>elasticsearch-java</artifactId>
      <version>8.12.2</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.12.3</version>
    </dependency>

Environment Details:

Class Imports:

import org.elasticsearch.client.RestClient;
import org.apache.http.HttpHost;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import co.elastic.clients.elasticsearch.cluster.HealthResponse;