elasticsearch,Java,Spring,Spring Boot,Maven,elasticsearch" /> elasticsearch,Java,Spring,Spring Boot,Maven,elasticsearch" />

弹性搜索导致java.lang.NoSuchFieldError的Spring引导:忽略\u弃用

弹性搜索导致java.lang.NoSuchFieldError的Spring引导:忽略\u弃用,java,spring,spring-boot,maven,elasticsearch,Java,Spring,Spring Boot,Maven,elasticsearch,我不熟悉弹性搜索。开始使用弹性搜索构建Spring启动应用程序 使用最新的ES版本“elasticsearch-7.7.1”,为了集成,我使用以下maven依赖项: <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId>

我不熟悉弹性搜索。开始使用弹性搜索构建Spring启动应用程序

使用最新的ES版本“elasticsearch-7.7.1”,为了集成,我使用以下maven依赖项:

    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.7.1</version>
    </dependency>
将以下属性添加到application.yaml

elasticsearch:
  host: localhost
在应用程序启动时获取以下异常:

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)
原因:org.springframework.beans.Bean实例化异常:未能实例化[org.elasticsearch.client.RestHighLevelClient]:工厂方法“client”引发异常;嵌套异常为java.lang.NoSuchFieldError:忽略\u弃用
位于org.springframework.beans.factory.support.SimpleInstallationStrategy.instantiate(SimpleInstallationStrategy.java:185)
位于org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
... 省略19个公共框架
原因:java.lang.NoSuchFieldError:忽略\u弃用
位于org.elasticsearch.client.RestHighLevelClient.(RestHighLevelClient.java:1902)
位于com.sbs.communicationcontrol.search.config.ESConfig.client(ESConfig.java:14)

有人能帮助解释为什么会发生此异常吗?

您没有正确初始化elasticsearch客户端,是否可以尝试使用以下代码:

请注意,我使用的版本接受主机、端口和
http
方案,它对我来说运行良好,是

并使用下面的配置

elasticsearch.host=localhost
elasticsearch.port=9500

经过一些研发后,通过添加以下两个依赖项修复了此问题:

        <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>

org.elasticsearch.client
elasticsearch rest客户端
7.7.1
org.elasticsearch
弹性搜索
7.7.1

我在升级到
org.elasticsearch.client:elasticsearch rest高级客户端:7.8.1
时遇到了相同的问题,从
6.8.5
。问题背后的原因是将
elasticsearch rest高级客户机
单独更新到最新版本与它的一些依赖的弹性搜索依赖项冲突,这些依赖项是由spring boot作为传递依赖项引入类路径的。当我检查依赖关系树时,我发现
org.springframework.boot:spring boot:2.3.1.RELEASE
依赖关系带来
org.elasticsearch.client:elasticsearch rest client:7.6.2
org.elasticsearch:elasticsearch:7.6.2
,7.6.2与
7.8.1
冲突

摘自
RestHighLevelClient
引用
IGNORE_DEPRECATIONS
Java文档的代码片段

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;
   .....
   .....
}

错误本身表明我们需要更新所有相关的elasticsearch库,尽管我无法找到任何现成的
elastic search BOM
,以解决此版本冲突,但我已完成以下解决方法

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


您可以设置所有Spring boot的版本:

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

避免在多个位置覆盖它。

没有解决问题,仍然面临相同的问题@DevChauhan您可以使用7.6.0版本的rest客户端吗?您还可以将代码上载到github,以便我可以查看并重新检查您的弹性版本。必须是同一版本的驱动程序。是一样的吗?@DevChauhan这已经是相当长的一段时间了,如果答案对你有用的话,请你投票,如果你需要更多信息,请告诉我。这对我来说非常有用,使用spring boot 2.1.5版本和elastic search 7.9.3。谢谢
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')
            
      }
  }
}

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