How to use the RestHighLevelClient for the cloud?

I use the Java RestHighLevelClient, and it works fine with localhost elastic search (in which I pass "localhost" and 9200, "http") like the example in here

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-getting-started-initialization.html

But how do I use this for elastic search cloud? and I suppose the host would be ".us-east-1.aws.found.io" and my port will be "9203" and scheme is always "https"?

There is a user/password associated with the cloud version... how to pass that along?

This is happening in the Low Level client. See https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_basic_authentication.html

Here is a sample code I'm using:

Note that decodeUrl() is basically returning the host url like https://XYZ.es.io:9243.

Thank you!

The following works perfectly fine in a little standalone Java Application (where the constants are pointing to my instance)...

    RestHighLevelClient restHighLevelClient = null;
    if (!"localhost".equals(ELASTIC_HOST)) {

        RestClientBuilder builder = RestClient
                .builder(new HttpHost(ELASTIC_HOST, Integer.parseInt(ELASTIC_PORT), ELASTIC_SCHEME));

        // if not localhost, specify username and password
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(ELASTIC_USER, ELASTIC_PASSWORD));

        builder.setHttpClientConfigCallback(
                httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        builder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(40000).setSocketTimeout(50000)).setMaxRetryTimeoutMillis(60001);

        restHighLevelClient = new RestHighLevelClient(builder);

    } else {
        restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost(ELASTIC_HOST, Integer.parseInt(ELASTIC_PORT), "http")));
    }

    try {
        LOGGER.info("ping");
        boolean pingResult = restHighLevelClient.ping(RequestOptions.DEFAULT);
        LOGGER.info("pingResult=" + pingResult);
    } catch (Exception e) {
        LOGGER.error("Can't even ping: ",e);
    }

However, inside a Web application (in a data access object)... this ping will fail (and any search, show indices etc would also fail) with a Timeout.

03-21-2019 12:20:55 ERROR ElasticsearchDAO:388 - Can't even ping: 
java.io.IOException: listener timeout after waiting for [60001] ms
	at org.elasticsearch.client.RestClient$SyncResponseListener.get(RestClient.java:899)
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:227)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1256)
	at org.elasticsearch.client.RestHighLevelClient.ping(RestHighLevelClient.java:372)

Any idea what extra steps a Java web application may need to access elastic search on cloud?

The only thing that I can think about would be a conflicting dependency or something like this.
What is the web server you are deploying this app to?

Thanks. I am using Wildfly 10. I also tried using "http" with 9200, and same result...

Is there any way to print debug all classes to see what is happening?
Or at least org.elasticsearch and org.apache packages?

Thanks @dadoonet, yes, conflicting maven artifact was the culprit.
I was able to use mvn dependency:tree to figure that elastic search uses a particular version of apache httpcomponent which uses a particular version of commons-codec... and I had another place that uses a different version which conflicted it.

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