2017-04-11 57 views
0

我在創建用於彈性搜索的TransportClient bean時遇到了問題,它說構造函數是私有的,它就是這樣的。我如何正確創建bean? 這裏是我的配置類:正確使用TransportClient進行彈性搜索

import org.elasticsearch.client.Client; 
import org.elasticsearch.client.transport.TransportClient; 
import org.elasticsearch.common.transport.InetSocketTransportAddress; 
import org.elasticsearch.common.transport.TransportAddress; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; 
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; 
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; 

@Configuration 
@EnableElasticsearchRepositories(basePackages = "example.spring.data.es.repository") 
@ComponentScan(basePackages = { "example.spring.data.es.service" }) 
public class Config { 

@Bean 
public Client client() { 
//here compiller says that the TransportClient() is private. How else i cna create the transport client? 
    TransportClient client = new TransportClient(); 

    TransportAddress address = new InetSocketTransportAddress(
      "localhost",9200); 
    client.addTransportAddress(address); 
    return client; 
} 

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

}

彈性搜索的依賴是:elasticsearch-2.4.2

回答

1

您需要使用所提供的建造方法進行初始化。示例:

TransportClient client = TransportClient.builder().build(); 
+0

的確,TY,我發現它太多。我非常感興趣,因爲我發現網絡上的例子聲稱它的工作原理,我雖然在一開始就沒有正確的jar依賴。謝謝你的答案。 – DanutClapa

1

該API已更改!小心你使用的版本。

Version 5.5 Transport Client

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) 

請注意,你有,如果你使用一個比 「elasticsearch」 不同來設置羣集名稱:

Settings settings = Settings.builder().put("cluster.name", "myClusterName").build(); 
TransportClient client = new PreBuiltTransportClient(settings);