Ash_s94
Ash_s94

Reputation: 817

Java RestHighLevelClient with AWS ElasticSearch

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)                                                             
                                                                   ...........

Upvotes: 0

Views: 3875

Answers (1)

mjk
mjk

Reputation: 659

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

Upvotes: 2

Related Questions