1

我有一个春天启动应用程序,我想用弹性搜索2.2.0独立的(不嵌入式服务器)中,我想使用Spring数据弹性搜索数据春季弹性搜索,那么什么是Spring Data支持弹性搜索的版本,以及如何配置它以连接到在localhost中运行的elasticsearch实例:9200?在Spring启动应用程序

其实,我尝试添加该选项到我的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年4月28日00:03:52.246 INFO 25613 --- [restartedMain] org.elasticsearch.plugins:[阿德沃夫]加载[],网站 [] 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 [本地主机/ 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.ru nWorker(ThreadPoolExecutor.java:1142) 〜[na:1.8.0_77] at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617) 〜[na:1.8.0_77] at java.lang .Thread.run(Thread.java:745) 〜[NA:1.8.0_77]

2016年4月28日00:04:01.512 ERROR 25613 --- [restartedMain] .dersAbstractElasticsearchRepository:未能加载 elasticsearch节点: org.elasticsearch.client.transport.NoNodeAvailableException:的 没有所配置的节点是可用的:[]

+0

这只是一个客户端:

,如果您需要了解更多信息,请看看这个帖子。请检查您的elasticsearch服务器状态。 –

+0

@Gemini Keith:elasticsearch启动并运行:http:// localhost:9200/{ “name”:“Hildegarde”, “cluster_name”:“elasticsearch”, “version”:{ “number”:“ 2.2.0" , “build_hash”: “8ff36d139e16f8720f2947ef62c8167a888992fe”, “build_timestamp”: “2016-01-27T13:32:39Z”, “build_snapshot”:假, “lucene_version”: “5.4.1” } , “tagline”:“你知道,搜索” } – jemlifathi

+0

看到这个答案:http://stackoverflow.com/a/36858819/4604579。 Spring Data尚未准备好ES 2.x – Val

回答

4

我从ES论坛这个答案,它工作了我:

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

我做了这些改变和它的工作非常完美。

1

我看过官方文档。 如果使用Java配置,请尝试:

@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

+0

对于Spring 1.4.0.RELEASE,您的elasticSearchTemplate方法应返回ElasticSearchTemplate。原始资料在这一点上被误认为是错误的。 – EricGreg

4

正如Jemli说,你需要使用的端口9300。

还要确保您的elastiscsearch客户端和服务器使用的是相同的主要版本。如果您使用的是elasticsearch 2.x,则需要将弹簧引导更新到最新版本(> 1.4.0.RC1)。 http://ignaciosuay.com/how-to-connect-spring-boot-to-elasticsearch-2-x-x/

+0

谢谢,真的帮了我。现在也不需要手动配置ElasticsearchTemplate。以下Spring属性将很好地处理它:spring.data.elasticsearch.cluster-nodes = localhost:9300 – EricGreg

相关问题