I have 2 indexes with names -
source_index
,
destination_index
Both indexes have same mappings.
I have written following code for making reindex and search requests -
@Autowired
RestHighLevelClient client;
public BulkByScrollResponse reIndexElasticResponse(ReindexRequest reindexRequest){
BulkByScrollResponse reindexResponse = null;
try{
reindexResponse = client.reindex(reindexRequest,RequestOptions.DEFAULT);
} catch (IOException e){
throw new RuntimeException(e);
}
return reindexResponse;
}
public SearchResponse searchElasticResponseByScrollAPI(SearchScrollRequest searchScrollRequest) {
SearchResponse searchResponse = null;
try {
searchResponse = client.scroll(searchScrollRequest,RequestOptions.DEFAULT);
} catch (IOException e) {
throw new RuntimeException(e);
}
return searchResponse;
}
Now my application is reindexing only selected records from source_index
to destination_index
.
And just after reindexing request, I am running one search query on destination_index
.
The problem is that my search query is not giving me correct response as I think that reindexing request is not working in synchronous manner.
If I use Thread.sleep(5000);
after my reindexing request then my search query gives me correct result.
reIndexElasticResponse(reindexRequest);
Thread.sleep(5000);
searchElasticResponseByScrollAPI(searchScrollRequest);
The above code will work fine. But if I comment Thread.sleep(5000)
then search query doesn't give expected output.
I want my program to wait until all the records are reindexed and then run the search query on destination_index