How to get from a BulkItemResponse to corresponding Request

1.2k Views Asked by At

I'm using Elasticsearch's bulk requests in Java and I'm trying to handle the situations where some error happens:

BulkResponse bulkResponse = bulkRequest.get();
if (bulkResponse.hasFailures()) {
    for (BulkItemResponse response : bulkResponse) {
        if (response.isFailed()
        || response.getResponse().getShardInfo().getFailed() > 0) {
            //Find the corresponding request and resend it
        }
    }
}

After finding the response with error, I want to re-send its request since in my case errors could be momentary and in most of the cases a retry could resolve the problem.

So my question is, how to get from BulkItemResponse to the original Request that led it? Is there any way better than relying on the order of requests and responses?

1

There are 1 best solutions below

6
On

No you don't have that AFAIK. You need somehow to keep all elements you added to a bulk and in case of error use the id coming from response.getId() to associate that with the original data.

So something similar to:

HashMap<String, Object> myData = new HashMap<>();

BulkRequestBuilder brb = BulkAction.INSTANCE.newRequestBuilder(client);

myData.put(myObject.getId(), myObject);
brb.add(new IndexRequest("index", "type", myObject.getId()).source(myObject.toJson()));

// Other actions
BulkResponse response = client.bulk(brb.request()).get();
response.forEach(bulkItemResponse -> {
    if (bulkItemResponse.isFailed()) {
        Object objectToSendAgain = myData.get(bulkItemResponse.getId());
        // Do what is needed with this object
    }
});

myData = null;

I hope this helps.