How to handle shard failures in ElasticSearch

Viewed 67

I'm trying to make my application that uses ElasticSearch resilient to failures. Looking at the Java client documentation on indexing responses it says I should look for shard errors in the response and handle them.

ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
    // TODO: Handle the situation where number of successful shards is less than total shards
}
if (shardInfo.getFailed() > 0) {
    for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
        String reason = failure.reason();
        // TODO: Handle the potential failures
    }
}

How do I handle those failure scenarios? I want to make sure that my data is safely stored in ElasticSearch before continuing. If there were no successfully shards obviously I can just retry, however if there were some successful and some failed or not all shards succeeded what should I do.

I couldn't see any guidance in the documentation around this (Java client or REST API's).

0 Answers
Related