javaelasticsearchelasticsearch-6

Elasticsearch 6 RestHighLevelClient: How to know when the result of an IndexRequest is ready to be read?


I'm writing a unit test where I need to write to an Elasticsearch 6 index using a RestHighLevelClient in the Java Elasticsearch 6 library, then read from the index. How can I know when the results of an IndexRequest are ready to be read from the index via RestHighLevelClient.search? For example:

RestHighLevelClient client;
//client initialization
BulkRequest request = new BulkRequest();
request.add(new IndexRequest(...));
BulkResponse response = client.bulk(request);
//process response

SearchRequest request = new SearchRequest(...);
SearchResponse scrollResponse = client.search(request);
//scrollResponse is empty!

Basically, if I put a Thread.sleep between the write and the read, the response has the content I wrote, so I think the requests are being made properly. Is there a way I can be sure to wait until the client.bulk(request) part has completely finished writing before I do the read operation?


Solution

  • This will force a refresh as part of this request.

    request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);