我无法让Spring-data-elasticsearch适用于Elasticsearch 2.0+版本。
使用以下POM
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.project.treasure</groupId>
<artifactId>search</artifactId>
<version>1.0.0</version>
<packaging>war</packaging>
<inceptionYear>2015</inceptionYear>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.project.treasure.search.application.Application</start-class>
<java.version>1.8</java.version>
<elasticsearch.version>2.2.0</elasticsearch.version>
<springdata.commons>1.12.0.BUILD-SNAPSHOT</springdata.commons>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>com.project.treasure.search</groupId>
<artifactId>search-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Json format -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<!-- Kafka dependencies -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.8.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.9.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>conf/*.properties</exclude>
</excludes>
</resource>
</resources>
<finalName>ROOT</finalName>
</build>
</project>
添加自定义ElasticsearchConfiguration
package com.package.project.module.application;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.core.env.Environment;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import java.net.InetSocketAddress;
import javax.annotation.Resource;
@Configuration
@PropertySources({
@PropertySource(value = "classpath:elasticsearch.properties", ignoreResourceNotFound = true),
@PropertySource(value = "classpath:app.properties", ignoreResourceNotFound = true) })
@EnableElasticsearchRepositories(basePackages = "com.package.project.module.repository")
public class ElasticsearchConfiguration {
@Resource
private Environment environment;
@Bean
public Client client() {
Settings settings = Settings.settingsBuilder().put("cluster.name", environment.getProperty("cluster.name")).build();
Client client = TransportClient.builder().settings(settings).build();
String[] addresses = environment.getProperty("cluster.nodes").split(",");
for (int i = 0; i < addresses.length; i++) {
System.out.println(addresses[i].trim());
TransportAddress address = new InetSocketTransportAddress(new InetSocketAddress(addresses[i].trim(), Integer.parseInt(environment.getProperty("elasticsearch.port"))));
((TransportClient) client).addTransportAddress(address);
}
return client;
}
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchTemplate(client());
}
}
在 Application.java
下面@Configuration
@ComponentScan(basePackages = "com.package.project.module")
@EnableAutoConfiguration(exclude = {ElasticsearchConfiguration.class})
public class Application extends SpringBootServletInitializer {
我仍然在我的有效POM中接受elasticsearch 1.5.2(以及另外的2.0.1.RELEASE版本)作为依赖。
在运行应用程序时收到以下错误。
java.lang.IllegalStateException: Error processing condition on org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration.elasticsearchClient
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:64)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:102)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:178)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:140)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:333)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
at com.snapdeal.treasure.search.application.Application.main(Application.java:20)
Caused by: java.lang.IllegalArgumentException: @ConditionalOnMissingBean annotations must specify at least one bean (type, name or annotation)
at org.springframework.util.Assert.isTrue(Assert.java:68)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.validate(OnBeanCondition.java:279)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition$BeanSearchSpec.<init>(OnBeanCondition.java:275)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:111)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47)
... 17 common frames omitted
我遵循spring-data-elasticsearch github repo和docs中提到的说明。
答案 0 :(得分:9)
问题是spring-boot
1.3.2.RELEASE尚未使用spring-data-elasticsearch
2.0.0-RELEASE。
spring-boot
1.3.2将包含spring-boot-starter-data-elasticsearch
1.3.2-RELEASE仍然depends on elasticsearch 1.5.2
从spring-boot
1.4.0-RELEASE开始支持ES 2.0,但尚未发布(但您可以尝试使用1.4.0.BUILD-SNAPSHOT来查看是否有帮助)
由于您还明确包含spring-data-elasticsearch
2.0.0-RELEASE,因此您将获得包含elasticsearch的2.0版本,但您应该删除该依赖项,因为它由spring-boot-starters
本身管理
答案 1 :(得分:3)
您需要等待spring boot 1.4.x才能使用elasticsearch v2.2.2
Spring-boot分支1.3.x仍在使用elasticsearch v1.5.2。
尝试改变你的pom并改变:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
并添加:
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
答案 2 :(得分:2)
请按照以下步骤
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.0.0.RELEASE</version>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>1.3.5.RELEASE</version>