我在創建用於彈性搜索的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
的確,TY,我發現它太多。我非常感興趣,因爲我發現網絡上的例子聲稱它的工作原理,我雖然在一開始就沒有正確的jar依賴。謝謝你的答案。 – DanutClapa