gpt4 book ai didi

java - 正确使用 TransportClient 进行 Elasticsearch

转载 作者:行者123 更新时间:2023-11-29 09:52:03 31 4
gpt4 key购买 nike

我在为 Elasticsearch 创建 TransportClient bean 时遇到问题,编译器说构造函数是私有(private)的,就像那样。我如何才能正确创建 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依赖为:elasticsearch-2.4.2

最佳答案

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);

关于java - 正确使用 TransportClient 进行 Elasticsearch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43342333/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com