0

I was wonder if anyone here has used RestHighLevelClient to connect to AWS ElasticSearch. Not sure if this is something AWS ElasticSearch supports yet.I'm currently getting a ConnectionClosedException everytime I try to connect.

Here's what I have:

public SearchResponse getQuery(){
    RestHighLevelClient client = new RestHighLevelClient(
            RestClient.builder(new HttpHost("localhost", 4430, "http")));
    SearchRequest searchRequest = new SearchRequest("msglog-dev-2018.05.21");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchAllQuery());
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = null;
    try{
        searchResponse =client.search(searchRequest);
    }catch(IOException e){
        e.printStackTrace();
    }
    return searchResponse;
}

and the error I get is

org.apache.http.ConnectionClosedException: Connection closed
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.endOfInput(HttpAsyncRequestExecutor.java:347)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:261)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)                                                             
                                                                   ...........
2
  • But you are connecting to the localhost instance in your code?! Commented Jul 14, 2018 at 4:38
  • Yes, my ElasticSearch is currently being tunneled to that port Commented Jul 17, 2018 at 14:01

1 Answer 1

2

Yes, we use the RHLC with AWS. Here's a sample that should get you going in the right direction. This demonstrates a direct call (which is probably relevant for more readers) -- but it can easily be adapted to match your tunneling needs by adjusting the settings of the connection parameters (host, port, protocol).

private static final String HOST = "your-es-endpoint.es.amazonaws.com";
private static final int PORT = 443;
private static final String PROTOCOL = "https";

private static final RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(HOST, PORT, PROTOCOL)));

public static void getESDocs() {
    try {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchAllQuery());   // adjust search logic here
        SearchRequest searchRequest = new SearchRequest("your-index-here");
        searchRequest.source(sourceBuilder);
        final SearchResponse searchResponse = client.search(searchRequest);

        /* process results here... */

        }
    } catch (Exception e) {
         /* handle connection/proc error here */
    } finally {
         client.close();    // example to release driver resource (see doc link)
    }
}

This example is effective for version 6.3.x (YMMV): API documentation

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

2 Comments

helpful, just add RequestOptions.DEFAULT in client.search(searchRequest, RequestOptions.DEFAULT); for version 6.7.x n above
Where to put username and password of Elasticsearch on aws

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.