1.使用spring-data-elasticsearch首选要解决jar报冲突问题
我自己的配置:
版本:elasticsearch6.3.1
jdk:1.8
pom文件:
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.7.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.0.7.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-elasticsearch --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.5.RELEASE</version> </dependency> </dependencies>
elasticsearch版本和spring-data-elasticsearch对应关系如下图
顺便说一下elasticsearch版本和jdk版本对应关系
综合考虑,spring-data-elasticsearch 我选择了3.1.5这个版本
还有一点需要特别强调:由于spring-data-elasticsearch,我选择了3.1.5这个版本,这个版本比较高,所以spring-test和spring-webmvc不能为5.0以下的版本,否则会各种报错,我在这里选择了5.0.7这个版本。
spring-elasticsearch配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd "> <!-- 扫描repository包,自动创建实现 --> <elasticsearch:repositories base-package="com.lyt.fyblb.repository"/> <!-- 配置elasticSearch的连接 --> <elasticsearch:transport-client id="esClient" cluster-name="my-application" cluster-nodes="192.168.31.129:9301,192.168.31.129:9302,192.168.31.129:9303"/> <!-- ElasticSearch模版对象 --> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="esClient"></constructor-arg> </bean> </beans>