Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot with Elastic Search causing java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS

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?

like image 259
Dev Chauhan Avatar asked Jun 12 '20 06:06

Dev Chauhan


2 Answers

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>
like image 91
Dev Chauhan Avatar answered Oct 27 '22 10:10

Dev Chauhan


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')
            
      }
  }
}

like image 3
Prasanth Rajendran Avatar answered Oct 27 '22 12:10

Prasanth Rajendran