简体   繁体   中英

correct use of TransportClient for elasticsearch

I have a problem with creating the TransportClient bean for elastic search the compiller it says that the constructor is private, and it is like that. How i can correctly create the bean? Here is my config class:

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

}

The elastic search dependency is: elasticsearch-2.4.2

The API has changed! Be careful with the version you are using.

From Version 5.5 Transport Client :

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)

Note that you have to set the cluster name if you use one different than "elasticsearch":

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

You need to use the provided builder methods for initialization. Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM