【问题标题】:Spring Data Elastic search in a Spring Boot applicationSpring Boot 应用程序中的 Spring Data 弹性搜索
【发布时间】:2016-08-22 11:21:54
【问题描述】:

我有一个 Spring Boot 应用程序,我想在其中使用 Elastic search 2.2.0 独立(不是嵌入式服务器),我想使用 Spring Data Elastic search,那么 Spring Data 支持的 Elastic search 版本是什么以及如何我可以将其配置为连接到在 localhost:9200 中运行的 elasticsearch 实例吗?

实际上,我尝试将此选项添加到我的 application.properties 文件中:

spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=localhost:9200

后来,我创建了这个配置类:

@Configuration
public class ElasticConfig {

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

    @Bean
    public Client client() {
        TransportClient client = new TransportClient();
        TransportAddress address = new InetSocketTransportAddress(
                "localhost",9200);
        client.addTransportAddress(address);
        return client;
    }
}

我得到了这个堆栈跟踪:

2016-04-28 00:03:52.246 信息 25613 --- [restartedMain] org.elasticsearch.plugins : [Aardwolf] 加载 [], 站点 [] 2016-04-28 00:04:01.356 信息 25613 --- [restartedMain] org.elasticsearch.client.transport : [Aardwolf] 未能获得 节点信息 [#transport#-1][fathi-HP-Pavilion-g6-Notebook-PC][inet[localhost/127.0.0.1:9200]],正在断开连接...

org.elasticsearch.transport.ReceiveTimeoutTransportException: [][inet[localhost/127.0.0.1:9200]][集群:监视器/节点/信息] request_id [0] 在 [5001ms] 后超时 org.elasticsearch.transport.TransportService$TimeoutHandler.run(TransportService.java:529) ~[elasticsearch-1.5.2.jar:na] 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_77] 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_77] 在 java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_77]

2016-04-28 00:04:01.512 错误 25613 --- [restartedMain] .d.e.r.s.AbstractElasticsearchRepository :加载失败 弹性搜索节点: org.elasticsearch.client.transport.NoNodeAvailableException:没有 配置的节点可用:[]

【问题讨论】:

  • 只是一个客户。请检查您的 elasticsearch 服务器状态。
  • @Gemini Keith:elasticsearch 已启动并运行:localhost:9200 { "name" : "Hildegarde", "cluster_name" : "elasticsearch", "version" : { "number" : "2.2. 0”,“build_hash”:“8ff36d139e16f8720f2947ef62c8167a888992fe”,“build_timestamp”:“2016-01-27T13:32:39Z”,“build_snapshot”:false,“lucene_version”:“5.4.1”},“标语”:“You知道,用于搜索”}
  • 查看这个答案:stackoverflow.com/a/36858819/4604579。 Spring Data 还没有为 ES 2.x 做好准备
  • @Val 哪个版本的 ES,我可以使用?

标签: java spring elasticsearch spring-boot spring-data-elasticsearch


【解决方案1】:

我阅读了官方文档。 如果使用 Java Config,请尝试:

@Configuration
@EnableElasticsearchRepositories(basePackages = "org/springframework/data/elasticsearch/repositories")
static class Config {

@Bean
public ElasticsearchOperations elasticsearchTemplate() {
    return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
}
}   

如果使用XML,请尝试:

<?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:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">

<elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300" />

</beans>

您可以阅读http://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.introduction

【讨论】:

  • 对于 Spring 1.4.0.RELEASE,您的 elasticSearchTemplate 方法应返回 ElasticSearchTemplate。在这一点上,原始消息来源有误。
【解决方案2】:

我从 ES 论坛得到了这个答案,它对我有用:

首先,Spring Data Elasticsearch 正式与 ES 1.x 版本一起工作(对我来说它与 1.7.1 一起工作)。 二、配置中使用的端口必须是9300

我做了这些更改,效果非常好。

【讨论】:

  • 你的弹簧靴是什么版本的?
【解决方案3】:

正如 Jemli 所说,您将需要使用端口 9300。

还要确保您的 elastiscsearch 客户端和服务器使用相同的主要版本。如果您使用的是 elasticsearch 2.x,则需要将 spring boot 更新到最新版本(> 1.4.0.RC1)。

如果您需要更多信息,请查看此帖子: http://ignaciosuay.com/how-to-connect-spring-boot-to-elasticsearch-2-x-x/

【讨论】:

  • 谢谢,真的帮了我。现在也不需要手动配置 ElasticsearchTemplate。以下 Spring 属性将很好地处理它: spring.data.elasticsearch.cluster-nodes=localhost:9300
猜你喜欢
  • 2018-06-08
  • 2014-11-04
  • 1970-01-01
  • 2020-08-18
  • 2020-10-22
  • 2017-08-15
  • 2018-08-18
  • 2017-07-16
  • 1970-01-01
相关资源
最近更新 更多