How to manage high level client connection to Elasticsearch

I wonder how to manage connection when using RestHighlevelClient in Java. The old connection is automatically disconnected. So I know that i need to connect and close when i need it. Is this right?

So what is I to be managed as follows?
For example, If i want to search for Search,

  1. Connect with Elasticsearch
  2. Run the search command
  3. Close the connection.

You should not open a new HTTP connection for every search that you are executing. Initiating a TCP connection, possibly a TLS connection on top of that requires time that adds to your search.

The general approach is to create a single High Level Rest Client in your application and reuse that one. You only need to close it when you shut down your application.

Hope that makes sense!

Thank you for answer.
There is a problem that you are using as if you are currently answered.
If i do not use it for a long time, it will automatically disconnect.
What should I do in this case?
Now I call Ping () and Retry is performing three times. But this is not correct.

Hey,

can you explain why it automatically disconnects? I expect there is a firewall in between, that is killing the TCP connections. However the low level client should retry after some time. Did this not work in your case?

--Alex