使用 Spring Boot 的 Elasticsearch 无法正常工作

Elastic Search with SpringBoot not working

我是 elasticsearch 的新手,我想将使用 Springboot (1.3.1.RELEASE) 开发的网络应用程序连接到安装在 unix VM 中的远程弹性搜索实例,所以我将以下内容添加到我的 pom.xml

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

这是我application.yml

中的配置
 spring:
   data:
     elasticsearch:
       host: 192.168.56.101
       port: 9300
       cluster-nodes: 192.168.56.101:9300

但我遇到以下异常:

2015-12-20 23:26:58.621  INFO 8580 --- [on(3)-127.0.0.1]    o.s.d.e.c.TransportClientFactoryBean     : adding transport node :   192.168.56.101:9300
2015-12-20 23:26:58.710  WARN 8580 --- [ I/O worker #1}]    org.elasticsearch.transport.netty        : [Coldfire] Message not fully read (response) for [0] handler   future(org.elasticsearch.client.transport.TransportClientNodesService$SimpleNodeSampler@54acdf57), error [true], resetting
2015-12-20 23:26:58.717  INFO 8580 --- [on(3)-127.0.0.1] org.elasticsearch.client.transport       : [Coldfire] failed to get node info for [#transport#-1][fgonzalez-HP][inet[/192.168.56.101:9300]], disconnecting...

org.elasticsearch.transport.RemoteTransportException: Failed to deserialize exception response from stream
Caused by: org.elasticsearch.transport.TransportSerializationException: Failed to deserialize exception response from stream
at org.elasticsearch.transport.netty.MessageChannelHandler.handlerResponseError(MessageChannelHandler.java:176) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.transport.netty.MessageChannelHandler.messageReceived(MessageChannelHandler.java:128) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline$DefaultChannelHandlerContext.sendUpstream(DefaultChannelPipeline.java:791) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:296) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:462) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.callDecode(FrameDecoder.java:443) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.handler.codec.frame.FrameDecoder.messageReceived(FrameDecoder.java:303) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.SimpleChannelUpstreamHandler.handleUpstream(SimpleChannelUpstreamHandler.java:70) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:564) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:559) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:268) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.Channels.fireMessageReceived(Channels.java:255) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.read(NioWorker.java:88) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.process(AbstractNioWorker.java:108) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:89) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.channel.socket.nio.NioWorker.run(NioWorker.java:178) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108) ~[elasticsearch-1.5.2.jar:na]
at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker.run(DeadLockProofWorker.java:42) ~[elasticsearch-1.5.2.jar:na]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_20]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_20]
at java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_20]
Caused by: java.io.StreamCorruptedException: Unsupported version: 1

有人知道这个异常的原因吗?

看起来与安装的 ElasticSearch (2.1.1) 版本不兼容。

是否意味着我不能使用最新版本的Springboot和最新版本的elasticsearch?然后我应该降级我的弹性搜索版本还是有人知道任何其他修复?

如果有帮助,这是我创建的文档:

@Document(indexName = "lng_index",type = "")
public class LanguageDoc {

@Id
private String id;

private String text;

public LanguageDoc() {
}

public LanguageDoc(String id, String text) {
    this.id = id;
    this.text = text;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}
}

我的 Spring 数据仓库:

@Repository
public interface LanguageRepository extends   ElasticsearchRepository<LanguageDoc,String> {
}

还有 elasticsearch.yml :

# Lock the memory on startup:
#
# bootstrap.mlockall: true
#
# Make sure that the `ES_HEAP_SIZE` environment variable is set to about   half the memory
# available on the system and that the owner of the process is allowed to   use this limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -------------------------------  ----
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#   network.host: 192.168.0.1
network.host: 0.0.0.0
#
# Set a custom port for HTTP:
#
# http.port: 9200
#
# For more information, see the documentation at:
# <http://www.elastic.co/guide/en/elasticsearch/reference/current/modules- network.html>

提前感谢您的帮助!!

郑重声明,以防其他人遇到同样的问题,我已通过将 ElasticSearch 的版本降级到 1.5.2(与 Sprinboot 附带的 elasticSearch 的内存实例相同)来修复它

另一种解决方案是使用 JEST 客户端。

只需添加依赖项

    <dependency>
        <groupId>io.searchbox</groupId>
        <artifactId>jest</artifactId>
        <version>2.0.3</version>
    </dependency>

并使用

配置
spring.elasticsearch.jest.uris=http://192.168.99.100:9200
spring.elasticsearch.jest.read-timeout=5000

Link to documentation