1. 程式人生 > >springboot整合elasticsearch-rest-high-level-client的坑

springboot整合elasticsearch-rest-high-level-client的坑

我使用的是Elasticsearch7.2.0,由於官網上推薦使用elasticsearch-rest-high-level-client端整合在springboot所以嘗試爬坑。

首先直接引入官網的配置檔案

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

然後在執行時報錯:

...
...
Caused by: java.lang.ClassNotFoundException: org.elasticsearch.common.xcontent.DeprecationHandler
...
...

百度了一下說jar裡的Elasticsearch版本有錯,要移除了再重新引入,所以把配置檔案改成下面的樣子

        <dependency>
		    <groupId>org.elasticsearch.client</groupId>
		    <artifactId>elasticsearch-rest-high-level-client</artifactId>
		    <version>7.2.0</version>
		    <exclusions>
	       <exclusion>
	         <groupId>org.elasticsearch</groupId>
	         <artifactId>elasticsearch</artifactId>
	       </exclusion>
	     </exclusions>
		</dependency>
		
		<!-- 正確版本 -->
		<dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.2.0</version>
        </dependency>

然後再次啟動,Elasticsearch正常執行,但是發現spring的rest服務執行不正常,配置的Filter沒有正常執行,且不報錯。網上找了很久也沒有找到有類似錯誤的問題,後來反覆檢視官網,他說elasticsearch-rest-high-level-client是基於elasticsearch-rest-client的,我就猜這個基於會不會是依賴的意思呢?

於是把配置改成了這樣:

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

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

然後再次啟動,Elasticsearch正常執行,REST服務也正常,我的老天鵝,終於可以了。

後來我找到了這篇文章Elasticsearch High Level REST Client,具體