1

是新来ElasticSearch连接...春数据ElasticSearch不能与ElasticSearch 5.5.0

真正爱的API(尤其是ElasticsearchTemplate &支持单元测试)...

使用下面这个例子ElasticSearch 5.5.0(以下环节都有完整的代码内联,也可以作为一个可下载的zip文件):

https://www.mkyong.com/spring-boot/spring-boot-spring-data-elasticsearch-example/

Maven依赖:

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

<properties> 
    <java.version>1.8</java.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-elasticsearch</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 

EsConfig:

@Configuration 
@EnableElasticsearchRepositories(basePackages = "com.mkyong.book.repository") 
public class EsConfig { 

    @Value("${elasticsearch.host}") 
    private String EsHost; 

    @Value("${elasticsearch.port}") 
    private int EsPort; 

    @Value("${elasticsearch.clustername}") 
    private String EsClusterName; 

    @Bean 
    public Client client() throws Exception { 

     Settings esSettings = Settings.settingsBuilder() 
       .put("cluster.name", EsClusterName) 
       .build(); 

     //https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html 
     return TransportClient.builder() 
       .settings(esSettings) 
       .build() 
       .addTransportAddress(
        new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort)); 
    } 

    @Bean 
    public ElasticsearchOperations elasticsearchTemplate() throws Exception { 
     return new ElasticsearchTemplate(client()); 
    } 
} 

的src /主/资源:

elasticsearch.clustername = mkyong-cluster 
elasticsearch.host = localhost 
elasticsearch.port = 9300 

当发出以下,或者:

mvn spring-boot:run 

mvn clean test 

标准输出:

Caused by: NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{127.0.0.1:9300}]] 
    at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:326) 
    at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:223) 
    at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55) 

而且,得到我的ElasticSearch 5.5.0引擎的stdout如下:

[2017-07-23T02:48:46,135][WARN ][o.e.t.n.Netty4Transport ] [vY7jxpr] exception caught on transport layer [[id: 0xa7e950be, L:/127.0.0.1:9300 - R:/127.0.0.1:60190]], closing connection 
    java.lang.IllegalStateException: Received message from unsupported version: [1.0.0] minimal compatible version is: [5.0.0] 
     at org.elasticsearch.transport.TcpTransport.messageReceived(TcpTransport.java:1379) ~[elasticsearch-5.5.0.jar:5.5.0] 
     at org.elasticsearch.transport.netty4.Netty4MessageChannelHandler.channelRead(Netty4MessageChannelHandler.java:74) ~[transport-netty4-5.5.0.jar:5.5.0]  

有没有办法使用Spring数据ElasticSearch与ElasticSearch 5.5.0引擎?

如果是这样,连接将如何实现?

我使用Elastic Search提供的Elastic Search Java客户端,能够在ElasticSearch 5.5.0上连接并创建索引,但真的希望我可以使用这个库的功能。

回答

1

Spring Boot 1.5.1不支持ElasticSearch 5.x(您提供的文章也会这样说)。为了能够与ElasticSearch 5.x的工作,你需要使用Spring引导2.

尝试的一个里程碑,具有以下依赖配置项目:

# spring boot 
compile 'org.springframework.boot:spring-boot:2.0.0.M2' 

# elasticsearch 
compile 'org.elasticsearch:elasticsearch:5.5.0' 
compile 'org.elasticsearch.client:transport:5.5.0' 

# spring-data-elasticsearch for spring boot 
compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch:2.0.0.M2' 

这应该允许您使用所有Elasticsearch Java API和spring-data-elasticsearch的好东西。

更新:基本摇篮(V4.0.1)配置,看起来像(的build.gradle文件):

buildscript { 
    ext { 
     springBootVersion = '2.0.0.M2' 
    } 
    repositories { 
     mavenCentral() 
     maven { url 'https://repo.spring.io/libs-snapshot' } 
     maven { url 'http://repo.spring.io/milestone/' } 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

apply plugin: 'java-library' 
apply plugin: 'org.springframework.boot' 
apply plugin: 'io.spring.dependency-management' 

jar { 
    baseName = 'elastic' 
    version = '0.0.1' 
} 

repositories { 
    mavenCentral() 
    jcenter() 
    maven { url 'https://repo.spring.io/libs-snapshot' } 
    maven { url 'http://repo.spring.io/milestone/' } 
} 

dependencies { 
    compile 'org.springframework.boot:spring-boot-starter-web' 
    compile 'org.springframework.boot:spring-boot-starter-logging' 

    compile 'org.springframework.boot:spring-boot-starter-data-elasticsearch' 

    compile 'org.elasticsearch:elasticsearch:5.4.1' 
    compile 'org.elasticsearch.client:transport:5.4.1' 

    compile 'com.google.guava:guava:20.0' 

    testCompile 'junit:junit:4.12' 
} 
+0

非常感谢您!这是个好消息,但我不知道如何使用我的Maven pom.xml来配置它......另外,我似乎无法找到它在文章中提到的地方Spring Boot 1.5.1缺少ElasticSearch 5.x 。支持。 –

+0

在文章的开头部分有** Note **部分: 'SpringBoot 1.5.1.RELEASE和Spring Data Elasticsearch 2.10.RELEASE仅支持ElasticSearch 2.4.0。他们不支持ElasticSearch 5.x版本的最新版本......'我主要使用gradle进行依赖管理 - 我使用配置更新了我的答案,因此可以更轻松地将它转换为maven pom。我建议使用https://start.spring.io/来生成pom文件的启动程序 - 它也将为里程碑生成正确的存储库,因为它们不存在于中央存储库中。 – Joanna