I am new to java coming from python. I know there are lot of answers out there to connect ElasticSearch with java. But it is difficult for me to understand and some are outdated. In python, I can easily import elasticsearch module and connect to it.
Here's the Code in python:
from elasticsearch import Elasticsearch
es = Elasticsearch('localhost', port=9200, http_auth=('username', 'password'), scheme="http")
But in java, i have included the elasticsearch maven dependency in pom.xml. I want to connect to elasticsearch. I came to know RestHighLevelClient can do this job. I found this code. But don't know how to make it connect to Elastic Search.
public RestHighLevelClient createESRestClient() {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(esUserName, esPassword));
RestClientBuilder restClientBuilder = RestClient
.builder(new HttpHost(esRestclientHost, 9200, "http"));
// Use this one if your ElasticSearch server is setup to use username & password authentication
if (esAuthentication) {
restClientBuilder.setHttpClientConfigCallback(h -> h.setDefaultCredentialsProvider(credentialsProvider));
}
return new RestHighLevelClient(restClientBuilder);
}
Any one can help me or show me some sample code to connect with Elastic Search with java. In python, it was done in two lines. Help me with java.