Connecting to ElasticSearch via JavaAPI is slow on the first hit

I have a Java server running on one of our on-premise servers and it will connect to Elastic server hosted in AWS.

Whenever a request is made from the Java application it will connect to elastic with the below piece of code. And once querying is complete, the client will be closed.

HttpHost[] httpValues=null;
if(hostName.contains(",")) {
	String[] hostNames=hostName.split(",");
	httpValues=new HttpHost[hostNames.length];
	for(int i=0;i<hostNames.length;i++) {
		httpValues[i]=new HttpHost(hostNames[i],esPort,method);
	}
}else {
	httpValues=new HttpHost[1];
	httpValues[0]=new HttpHost(hostName,esPort,method);
}
return new RestHighLevelClient(RestClient.builder(httpValues));

The issue I face is when the below piece of code gets executes it takes exactly 30 seconds to respond for the first time and the subsequent hits are returning results faster. Any idea what could be the issue

new RestHighLevelClient(RestClient.builder(httpValues));

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.