While initializing RestHighLevel client do we always need to use new RestHighLevel client and new host?

Viewed 50

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));

My question is do we always need to use new HttpHost ? Is there a way where we can maintain a pool of connections ?

We are having GET and POST apis and client.search() is always called when there is a request triggered from front end. So everytime a new request is triggered a new connection is created. Is there a better approach of handling it. ?

1 Answers

You don't need to create multiple RestHighLevelClient in your application as mentioned in the official doc JHLRC is a thread safe client and typically instantiated by the application at startup and can be used by all the requests in your application.

from the same doc

The RestHighLevelClient is thread-safe. It is typically instantiated by the application at startup time or when the first request is executed.

Internally JHLRC maintains the pool of connection also does the load-balancing between Elasticsearch nodes, so you don't have to worry about these internals.

Related