您现在的位置是: 首页 > 后端开发 > Java SpringBoot 集成 Elasticsearch 7.8.1 出现错误 Factory method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS

SpringBoot 集成 Elasticsearch 7.8.1 出现错误 Factory method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS

2020-09-15 Java 5338人已围观 9752次浏览

简介SpringBoot 集成 Elasticsearch 7.8.1 出现错误 FactorySpringBoot 集成 Elasticsearch 7.8.1 出现错误 Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS

由于项目中大量用到了搜索,如果全部使用数据库本身的模糊查询,效率是很低的,所以想到在项目中集成 Elasticsearch 来专用搜索

之前项目中,引用的版本是 6.8.8

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

但是考虑到 Elasticsearch 目前已经升级到 7.8.X 了,这里想尝试一下新版本,使用 docker 搭建好服务,配置好 ik 分词后,项目引用 7.8.1 版本的 elasticsearch-rest-high-level-cliet

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

然后启动项目,不出意外是报错了

Failed to instantiate [org.elasticsearch.client.RestHighLevelClient]: Factory method 'elasticsearchRestHighLevelClient' threw exception; nested exception is java.lang.NoSuchFieldError: IGNORE_DEPRECATIONS

根据提示内容,想到可能是缺少依赖(版本是 6.8.8 时,这样引用是没错的,也有可能是项目中已经包含了必要的引用,但配置文件中没写明)

之后查看下面两文档发现,必要的引用还有一个客户端版本,也就是 elasticsearch 的客户端

https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client/7.8.1
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-compatibility.html

根据说明,修改 pom.xml 文件

<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.8.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-high-level-client -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.8.1</version>
</dependency>

之后 maven reload ,重新运行项目,错误解决。果然问题是出在了缺少依赖上

很赞哦! (0)

站长推荐

站点信息

  • 网站地图