9
votes

I'm new to Elastic search. Started building a Spring boot application with Elastic search.

Using the latest ES version "elasticsearch-7.7.1" and for integration, I'm using below maven dependency:

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.7.1</version>
    </dependency>

Added below configuration to my spring boot app:

@Configuration
public class ESConfig {

  @Bean(destroyMethod = "close")
  public RestHighLevelClient client() {
    RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost")));
    return restHighLevelClient;
  }

}

Added below properties to application.yaml

elasticsearch:
  host: localhost

Getting below Exception on Application startup:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'client' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
    ... 19 common frames omitted
Caused by: java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS
    at org.elasticsearch.client.RestHighLevelClient.<clinit>(RestHighLevelClient.java:1902)
    at com.sbs.communicationcontrol.search.config.ESConfig.client(ESConfig.java:14)

Can anyone please help why this exception occurred?

4
Check your elastic version. Must be the same version of a driver. Are be same? - fuzeto
@DevChauhan its been quite some time, it would be great if you can upvote the answer if it was useful for you and let me know if you need more info - user156327

4 Answers

16
votes

After some R&D, fixed the issue by adding below two dependencies:

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.7.1</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.7.1</version>
        </dependency>
3
votes

I encountered the same problem when I was upgrading to org.elasticsearch.client:elasticsearch-rest-high-level-client:7.8.1 from 6.8.5. The reason behind the problem is updating elasticsearch-rest-high-level-client alone to the latest version conflicts with some of its dependent elastic search dependencies which were brought into classpath as transitive dependencies by spring-boot. When I check with the dependency tree, I found that the org.springframework.boot:spring-boot:2.3.1.RELEASE dependency brings org.elasticsearch.client:elasticsearch-rest-client:7.6.2, org.elasticsearch:elasticsearch:7.6.2 and the 7.6.2 conflicts with 7.8.1.

An Excerpt code snippet from the RestHighLevelClient citing IGNORE_DEPRECATIONS Java Docs.

public class RestHighLevelClient implements Closeable {
    ....
    ....
    /**
     * Ignores deprecation warnings. This is appropriate because it is only
     * used to parse responses from Elasticsearch. Any deprecation warnings
     * emitted there just mean that you are talking to an old version of
     * Elasticsearch. There isn't anything you can do about the deprecation.
     */
    private static final DeprecationHandler DEPRECATION_HANDLER = DeprecationHandler.IGNORE_DEPRECATIONS;
   .....
   .....
}

The error itself indicates us to update all related elasticsearch libraries, though I couldn't find any out-of-box elastic search BOM to resolve this version conflicts, I have done the below workaround.

dependencies{

    implementation 'org.elasticsearch.client:elasticsearch-rest-client:7.8.1'
    implementation 'org.elasticsearch.client:elasticsearch-rest-client:7.8.1'
    implementation 'org.elasticsearch:elasticsearch:7.8.1'

}

//Since version 7.6.2 is selected by rule, substituting the version 7.8.1 as below

configurations.all {
    resolutionStrategy {
        dependencySubstitution {
            substitute module('org.elasticsearch.client:elasticsearch-rest-high-level-client') with module('org.elasticsearch.client:elasticsearch-rest-high-level-client:7.8.1')
            substitute module('org.elasticsearch.client:elasticsearch-rest-client') with module('org.elasticsearch.client:elasticsearch-rest-client:7.8.1')
            substitute module('org.elasticsearch:elasticsearch') with module('org.elasticsearch:elasticsearch:7.8.1')
            
      }
  }
}

0
votes

You are not correctly initializing your elasticsearch client, can you try with below code:

Please note that I am using the version which accepts the host, port and http scheme and it works fine for me and is the standard for creating the client.

@Configuration
@Primary
public class ElasticsearchConfig {

    /**
     * Creates a Elasticsearch client from config
     *
     * @return Elasticsearch client
     */
    @Bean(destroyMethod = "close")
    public RestHighLevelClient client() {
        RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost("localhost", 9500, "http")));
        return client;
    }
}

And use below config

elasticsearch.host=localhost
elasticsearch.port=9500
0
votes

You can set the version for all of the Spring-boot:

ext {
    set('elasticsearch.version', '6.2.0')
}

to avoid overriding it in multiple places.