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

相關問題