# High Rest Java Client with CompletableFuture

**URL:** <https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952>\
**Category:** Elasticsearch\
**Created:** [June 3, 2019, 11:57am UTC](https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952 "2019-06-03T11:57:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santosh\_Pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santosh_pandey/32/46221_2.png) [@Santosh\_Pandey](https://discuss.elastic.co/u/Santosh_Pandey)\
**Post date:** [June 3, 2019, 11:57am UTC](https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952/1 "2019-06-03T11:57:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 1, 2019, 11:59am UTC](https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952/2 "2019-07-01T11:59:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/baz/32/5183_2.png) [@baz](https://discuss.elastic.co/u/baz)\
**Post date:** [August 6, 2019, 8:30pm UTC](https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952/3 "2019-08-06T20:30:48Z")

</div>

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.

```auto
        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.

---

<div class="post-metadata">

**Author:** ![baz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/baz/32/5183_2.png) [@baz](https://discuss.elastic.co/u/baz)\
**Post date:** [August 6, 2019, 8:31pm UTC](https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952/4 "2019-08-06T20:31:09Z")

</div>



---

<div class="post-metadata">

**Author:** ![Anver\_S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anver_s/32/46385_2.png) [@Anver\_S](https://discuss.elastic.co/u/Anver_S)\
**Post date:** [November 5, 2019, 3:52pm UTC](https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952/5 "2019-11-05T15:52:05Z")

</div>

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);`  
?

---

<div class="post-metadata">

**Author:** ![baz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/baz/32/5183_2.png) [@baz](https://discuss.elastic.co/u/baz)\
**Post date:** [November 11, 2019, 2:19pm UTC](https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952/6 "2019-11-11T14:19:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 4, 2022, 7:10am UTC](https://discuss.elastic.co/t/high-rest-java-client-with-completablefuture/183952/7 "2022-11-04T07:10:36Z")

</div>


