Elasticsearch REST Java Connection Client

For Elasticsearch connection, Elasticsearch REST Java API provide RestClient and RestClient.build() to get connection.

Below connection client class covers all the ways of connectivity to elastic search depend of elastic servers configuration and  accessibility accordingly you can uncomment below methods in customize Http Client.  I made this class as Singleton because Elasticsearch client keep connection persistent.

public class ElasticSearchConnectionRestTest {
	private RestClient client = null;
	private static ElasticSearchConnectionRestTest esc = null;
	private ElasticSearchConnectionRestTest() {

	public static synchronized ElasticSearchConnectionRestTest getInstance() {
		if (esc == null) {
			esc = new ElasticSearchConnectionRestTest();
		return esc;

	public RestClient getClient() {
		if (client == null) {
		return client;

private RestClient getElasticSearchClient() {

//Basic credential settings
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("usrid", "password"));

client = RestClient.builder(new HttpHost("elasticserchhost1",
new HttpHost("elasticserchhost2",
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
 //Security Settings
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
	return httpClientBuilder
//disable preemptive authentication so that same request done when next


//Proxy server settings
.setProxy(new HttpHost("one.proxy.att.com", 8080))

//setting for key store for JKS	SSL

//Number of threads will execute
//connection timeout
//socket connection timeout
//Max retry timeout
return client;

private void closeConnnection()
}catch(IOException ex)

Details of API’s used by Elasticsearch REST Connection


This callback method allows to modify the http client configuration like  encrypted ,proxy, communication over ssl, socket timeout etc.  By using customizeHttpClient we can configure all these values.


We can set default header if need to sent some value with in  every request.


The timeout value if request do multiple attempts for same request.


This is listener to get notification whenever get any node fails and need to get any action for same.

Methods of CustomizeHttpClient Builder

Timeout Configuration

connectTimeout: Default value is 1 seconds

socketTimeout: Default 30 seconds

maxRetryTimeoutMilliseconds: Default 30 seconds

Thread Configuration

IoThreadCount:  Client start with default one thread and a number of worker threads used by connection manager, as many as the number of locally detected processors.

Authentication Configuration

setDefaultCredentialsProvider :  This method require basic credentials for authentication.

httpClientBuilder.disableAuthCaching(): We can disable authentication caching and sent in  every request headers to elasticsearch  if it will accepted and,  if it get failed by receiving a HTTP 401 response message, it will resend the exact same request again with the basic authentication header.

Encrypted Communication

setSSLContext:  Set this value for SSL Context for encrypted communication.

Read More on Elasticsearch REST


Integrate Filebeat, Kafka, Logstash, Elasticsearch and Kibana