1

I am using the Java HighLevelRestClient to connect to my elasticsearch instance hosted on AWS. I can make requests against the URL on postman and from my browser just fine, but when I use the client library I receive

java.net.ConnectException: Connection Refused.

(I don't currently need any authentication as this is a small public test instance). This is my code:

RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);

GetRequest getRequest = new GetRequest("some_index", "some_type","some_id");
        final String[] elasticGetResponse = new String[1];

        restHighLevelClient.getAsync(getRequest, new ActionListener() {

            @Override
            public void onResponse(GetResponse documentFields) {
                try {
                    elasticGetResponse[0] = restHighLevelClient.get(getRequest).toString();
                }

                catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Exception e) {
                e.printStackTrace();
            }
        });

Please let me know how I can fix this... thanks!

Update: Here is my code for the restClientBuilder:

MySSLHelper sslHelper = new MySSLHelper(SSLConfig.builder()
                .withKeyStoreProvider(myKeyStoreProvider)
                .withTrustStoreProvider(InternalTrustStoreProvider.INSTANCE)
                .build());

        RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("MY_ELASTICSEARCH_ENDPOINT")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
                return httpAsyncClientBuilder.setSSLContext(sslHelper.getContext());
            }
        });
2
  • 1
    where is your restClientBuilder? Commented Jun 14, 2018 at 5:09
  • @AbhijithS Updated my question with the restClientBuilder code, Commented Jun 14, 2018 at 15:53

1 Answer 1

2

I was the same problem and solved putting the port and protocol, like showed in this page: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.0/java-rest-high-getting-started-initialization.html

My code stayed like this:

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(elasticsearchHost, 9200, "http")));

Please try to do something like this:

RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("MY_ELASTICSEARCH_ENDPOINT", "MY_ELASTICSEARCH_PORT", "MY_ELASTICSEARCH_PROTOCOL"))...

Hope this helps.

Good bye.

Sign up to request clarification or add additional context in comments.

1 Comment

What is value of MY_ELASTICSEARCH_PROTOCOL, is that http? In the document guide, it also uses http as example, nothing wrong with that code above.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.