High Rest Java Client with CompletableFuture

Using Elastic search high client java client, I was able to search asynchronously

restHighLevelClient.searchAsync(searchRequest, RequestOptions.DEFAULT,new ActionListener() {
public void onResponse(SearchResponse searchResponse) {

But onResponse returns void. How do I make it work with Completablefuture in Java?
I need to collect response from onResponse method and give it to future object

I implemented my own ActionListener which returns my data type. But how do I make it work with Completable Future

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

Hi, terribly sorry this dropped off my radar. If you have not found a solution yet, I crafted one below using some of the same logic our tests use.

        IndexRequest index = new IndexRequest("index").id("id");
        String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
        index.source(document, XContentType.JSON);
        client.index(index, RequestOptions.DEFAULT);

        CompletableFuture<GetResponse> responseFuture = CompletableFuture.supplyAsync(() -> {
            PlainActionFuture<GetResponse> future = PlainActionFuture.newFuture();
            client.getAsync(new GetRequest("index", "id"), RequestOptions.DEFAULT, future);
            return future.actionGet();

        GetResponse getResponse = responseFuture.get();
        assertEquals("index", getResponse.getIndex());
        assertEquals("_doc", getResponse.getType());
        assertEquals("id", getResponse.getId());
        assertEquals(1L, getResponse.getVersion());
        assertEquals(document, getResponse.getSourceAsString());

Hope this helps, and if you also have come up with a solution, it would be nice to see as well.

Hi Michael,
What is the point of using client.getAsync inside CompletableFuture.supplyAsync lambda when CompletableFuture.supplyAsync would already execute it asynchronously?
Would client.getAsync give you any extra benefits compared to
CompletableFuture.supplyAsync(() -> client.get(new GetRequest("index", "id"), RequestOptions.DEFAULT);

You are correct, I am not sure why I made the example use the async other than that is what the original poster mentioned in their comment. Your example works the same way as mine but much less complicated, so ++ for that!