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() {
@Override
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);
        index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
        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();
        });

        assertFalse(responseFuture.isDone());
        GetResponse getResponse = responseFuture.get();
        assertEquals("index", getResponse.getIndex());
        assertEquals("_doc", getResponse.getType());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertFalse(getResponse.isSourceEmpty());
        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.