# Send a blocking bulk index request via the Elasticsearch Java client (8.9)

**URL:** <https://discuss.elastic.co/t/send-a-blocking-bulk-index-request-via-the-elasticsearch-java-client-8-9/341020>\
**Category:** Elasticsearch\
**Created:** [August 17, 2023, 1:14pm UTC](https://discuss.elastic.co/t/send-a-blocking-bulk-index-request-via-the-elasticsearch-java-client-8-9/341020 "2023-08-17T13:14:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zer0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zer0/32/124706_2.png) [@Zer0](https://discuss.elastic.co/u/Zer0)\
**Post date:** [August 17, 2023, 1:14pm UTC](https://discuss.elastic.co/t/send-a-blocking-bulk-index-request-via-the-elasticsearch-java-client-8-9/341020/1 "2023-08-17T13:14:13Z")

</div>

Hi,

I have written a Spring Boot based integration test where I use the bulk api to index some documents. This is the main loop of the `reindexAll()` method:

```auto
for (page in pageExports) {
            bulkRequest.operations { ops -> ops.index { it
                .index(Index.PAGE.indexName)
                .id(page.pageId.toString())
                .document(page)
            } }
 }
 val result = esClient.bulk(bulkRequest.build())

```

The result comes back just fine and the `took` field has the value 16. My understanding was that this call is blocking. (E.g. the request only returns when all the documents have been ingested).

However right after I do the result when I do a count query on the index like so:

```auto
val result = esClient.count { it.index(listOf(DwIndex.PAGE.indexName))}.count()

```

I get 0 as a count, same if I fire a search query. If I wait on breakpoint after the indexing and before the query, or if I put a `Thread.sleep(100)` in front of the count/search, it is correct.  
So my question is what is happening here? Possible explanations that came to mind:

- Bulk is indeed asynchronous, which doesn't make a lot of sense since it has the errors for documents that could not be indexex
- Bulk is synchronous, but after the indexing, it takes some (small amount of) time for the newly indexed documents to be pushed to the shards.
- I'm doing something wrong 🙂

What would be a good way to make `reindexAll()` block until everything is ready, so the contract should be if you call `reindexAll()` you can instantly start searching. I would not like to add a `Thread.sleep()` or poll with a search request at the end of the method.

Cheers and thanks!

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [August 17, 2023, 1:30pm UTC](https://discuss.elastic.co/t/send-a-blocking-bulk-index-request-via-the-elasticsearch-java-client-8-9/341020/2 "2023-08-17T13:30:38Z")

</div>

Welcome!

Don't forget to call `refresh` before counting/searching.

See

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/be89f7cc7530cebfaeadc7c5e9bf109c71ee4caa/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientIT.java#L396-L419>

Specifically this line:

```auto
client.indices().refresh(rr -> rr.index("bulk"));

```

---

<div class="post-metadata">

**Author:** ![Zer0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zer0/32/124706_2.png) [@Zer0](https://discuss.elastic.co/u/Zer0)\
**Post date:** [August 17, 2023, 1:45pm UTC](https://discuss.elastic.co/t/send-a-blocking-bulk-index-request-via-the-elasticsearch-java-client-8-9/341020/3 "2023-08-17T13:45:06Z")

</div>

Thank you a lot for the quick reply. That fixed my issues. Also thanks for linking the tests, that will be helpful in the future as a reference!

Cheers  
Tom

---

<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:** [September 14, 2023, 1:45pm UTC](https://discuss.elastic.co/t/send-a-blocking-bulk-index-request-via-the-elasticsearch-java-client-8-9/341020/4 "2023-09-14T13:45:58Z")

</div>

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