我在创建用于弹性搜索的TransportClient bean时遇到问题,编译器说构造函数是私有的,就像那样。我怎样才能正确创建bean? 这是我的配置类:
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
答案 0 :(得分:7)
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);
答案 1 :(得分:2)
您需要使用提供的构建器方法进行初始化。例如:
TransportClient client = TransportClient.builder().build();