Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch java client initialization fails

I'm getting this error message while trying to run an application which connects to elasticsearch.

An attempt was made to call the method org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;)V but it does not exist. Its class, org.elasticsearch.client.RestHighLevelClient, is available from the following locations:

jar:file:/path/application/target/application-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/elasticsearch-rest-high-level-client-5.6.3.jar!/org/elasticsearch/client/RestHighLevelClient.class

It was loaded from the following location:

jar:file:/path/application/target/application-0.0.1-SNAPSHOT.jar!/BOOT-INF/lib/elasticsearch-rest-high-level-client-5.6.3.jar!/

Action:

Correct the classpath of your application so that it contains a single, compatible version of org.elasticsearch.client.RestHighLevelClient

The application builds without errors and only one version of elasticsearch SDK exists in my maven repository.

This is the relevant portion of my pom:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath />
    </parent>

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.6.3</version>
    </dependency>

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-client</artifactId>
        <version>5.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>5.6.3</version>
        <exclusions>
            <exclusion>
                <groupId>org.elasticsearch.client</groupId>
                <artifactId>elasticsearch-rest-client</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

This is the exception that I get while running the application:

org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'restHighLevelClient' threw exception; nested exception is java.lang.NoSuchMethodError: org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;)V

This is how I'm initializing the RestHighLevelClient:

RestClientBuilder builder = RestClient
                .builder(new HttpHost(hostname, port, scheme));
builder.setMaxRetryTimeoutMillis(timeout);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder.build());
like image 654
varshavp27 Avatar asked Dec 04 '18 10:12

varshavp27


2 Answers

Spring boot will try to autoconfigure elasticsearch which will internally use elastic 6 RestHighLevelClient(org.elasticsearch.client.RestClientBuilder builder) to create the elastic client. If you want to connect to an older version of elasticsearch please exclude elasticsearch autoconfiguration.

@EnableAutoConfiguration(exclude={ElasticsearchAutoConfiguration.class, RestClientAutoConfiguration.class})
like image 134
Sankaran Avatar answered Nov 08 '22 14:11

Sankaran


By looking at the exception

java.lang.NoSuchMethodError: org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder;)

There is no constructor in RestHighLevelClient which takes RestClientBuilder as a parameter in <version>5.6.3</version>.

Did you try using version <version>7.0.0-alpha1</version> ?

Update:

Exception An attempt was made to call the method org.elasticsearch.client.RestHighLevelClient.<init>(Lorg/elasticsearch/client/RestClientBuilder; tells that the code is it trying to execute a method that belongs to elasticsearch version 6. In your case perhaps you have multiple version of Elasticsearch libs provided in run time, or your code might be complied by version 6, but in run time the version 5 is provided.

like image 27
Dushmantha Avatar answered Nov 08 '22 13:11

Dushmantha