Elastic Search TransportClient

I am using the ElasticSearch Java client to query elastic search. I initialize the transport client every time I have to make a call. Is this correct or is it necessary to initialize once during application launch and close it when shutting down.

Below is the code to initialize the client

  Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", Config.getEsClusterName()).put("client.transport.ignore_cluster_name", true).build(); Client esClient = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress(Config.getEsHost(), Config.getEsPort())); 
+6
source share
3 answers

The elasticsearch Java client is multithreaded and each new instance has a lot of overhead.

This should be created once at the beginning of your program and shared among all callers.

Best wishes

+20
source

I created a github repository to use the java elasticsearch transport client [with Singleton design pattern] .. please .. use it. refer

+4
source

TransportClient maintains a pool of connections internally, they will be maintained throughout the life cycle of the TransportClient.

Thus, you must use static to store the TransportClient object or implement a singleton class that handles the connection. The rest of your code will only bind to this class.

0
source

Source: https://habr.com/ru/post/970276/


All Articles