spring boot 整合 elasticsearch 5.x

spring boot与elasticsearch集成有两种方式。一种是直接使用elasticsearch。一种是使用data中间件。

本文只指针使用maven集成elasticsearch 5.x,以及使用spring-boot-starter-data-elasticsearch中间件的情况。不包含以上两种情况请忽略。

第一种方式,本来没什么好说的。新建一个maven项目,引入spring boot相关依赖,再导入elasticsearch的依赖。但是,问题出现了:

依赖:

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.2.1</version>
</dependency>

得到的jar包:

导入的是5.x的版本,最终得到的2.x的版本。lucene也是5.x版本。可能有的人会说,应该显示指定elasticsearch版本啊。

首先,在transport的项目中已经引入了es的依赖。不需在项目中重复引入。

其次,在非spring boot的项目中,只引入transport就可以了。

 那么,问题只能是出在spring boot上面。

后来检查发布引入了<parent>,spring boot会自动选择最合适的版本进行添加。

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath />
</parent>

找到spring boot 1.5.3.RELEASE的源码,https://github.com/spring-projects/spring-boot/blob/1.5.x/spring-boot-dependencies/pom.xml。确实在这个版本中,引入的是elasticsearch2.x。因为使用了parent,所以,父级的依赖优先级最高。所以直接下载了spring boot 1.5.3.RELEASE所依赖的版本。而非子依赖的版本。

 https://github.com/spring-projects/spring-boot/blob/1.5.x/spring-boot-dependencies/pom.xml

 那么要想到spring boot中引入es的5.x版本,两种方式,第一种, A:显式引入es依赖,或者,B:覆盖掉spring boot的es版本。第二种,C:去掉<parent>依赖,手动添加spring-boot的依赖。三个选项的关系:(A||B)||(C)。

    <properties> 
        <elasticsearch.version>5.4.3</elasticsearch.version>
    </properties>

或者

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>5.4.3</version>
    </dependency>

第二种方式。好消息是spring已经正式发布版本支持es 5.0版本了。

 

spring data elasticsearchelasticsearch
3.0.0.RC2 5.5.0
3.0.0.M4 5.4.0
2.0.4.RELEASE 2.4.0
2.0.0.RELEASE 2.2.0
1.4.0.M1 1.7.3
1.3.0.RELEASE 1.5.2
1.2.0.RELEASE 1.4.4
1.1.0.RELEASE 1.3.2
1.0.0.RELEASE 1.1.1


不过,发布才不到一个月,直接用maven还下载不了。需要手动。

坏消息是,spring boot集成了与elasticsearch 5.x的版本,但还算不上正式发布。最新的正式版本只支持到2.4。

spring data elasticsearchelasticsearch
2.0.4.RELEASE 2.4.0
2.0.0.RELEASE 2.2.0
1.4.0.M1 1.7.3
1.3.0.RELEASE 1.5.2
1.2.0.RELEASE 1.4.4
1.1.0.RELEASE 1.3.2
1.0.0.RELEASE 1.1.1

在非正式的版本中,jdk9endpoint-infrastructure 这两个branches中,使用的是es 5.x版本。然而,尴尬的是,不久前,这两个版本都被打上了红叉。

想尝鲜的朋友,也可以去看一下最新的发布版本,2.0以上的都是使用了es 5.x版本。不过,不知道是使用的哪个branches发布,慎重使用。

以上版本,都刚发布不久,还没有被maven仓库收录。如果想使用,只能把代码拉到本地,编译,放入自己的仓库。不过,在可期的未来应该会有稳定的使用es 5.0的spring boot版本发布。

 
原文地址:https://www.cnblogs.com/eryuan/p/7380801.html