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

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:

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:

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 :slightly_smiling_face:

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!

Welcome!

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

See

Specifically this line:

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

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

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