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>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.17.0</version>
</dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.13.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>8.13.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
Environment Details:
- Elasticsearch Version: 8.12.0
- Application Type: Spring Boot Maven project
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;