Follow simple steps for making the RestHighLevelClient ready for connecting TLS+Auth Elastic Search
Create a CredentialsProvider using BasicCredentialsProvider provided by Apache httpclient like below
final CredentialsProvider credentialProvider = new BasicCredentialsProvider();
credentialProvider.setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(
ES_USERNAME,
ES_PASSWORD
));
Create a HttpHost provide by apache using Host, Port and Protocol like below
HttpHost httpHost = new HttpHost("ELASTIC_SEARCH_HOST", 9200, "https");
Here I used "https" since TLS is enabled on ES. You can use "http" for normal ES.
And the final step is to create RestHighLevelCLient like below
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(nodes)
.setHttpClientConfigCallback(httpAsyncClientBuilder -> {
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialProvider);
return httpAsyncClientBuilder;
}
));