[Java] Quick way to get the failure number in BulkResponse?

Hello everybody,

I use BulkProcessor and want to get the number of failure messages in BulkResponse. With the current APIs, I see 2 ways.

Way 1: iterate over BulkResponse and count

  @Override
  public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
    int failActions = 0;
    if (response.hasFailures()) {
      System.out.println("bulk process has failure: " + response.buildFailureMessage());
      for (BulkItemResponse item: response) {
        if (item.isFailed()) failActions++;
      }
    }

Way 2: count the line number in failureMessage string (returned from response.buildFailureMessage()). It seems a bit tricky because it assumes there is no new line inside each BulkItemResponse's failure.

I wonder if it would be better to have getFailureNumber() in BulkResponse.

Regards,

trango

Way 1 will traverse the responses twice (potentially thrice if you include
hasFailures). An efficient method will only traverse the items once.. Way 2
is a hack. :slight_smile:

I simply get all the failures and build the failure message myself over the
known failures instead of the entire list.

List failureResponses =
Arrays.stream(response.getItems()).filter(BulkItemResponse::isFailed).collect(Collectors.toList());

int failActions = failureResponses.size()
...

It would be great if Elasticsearch would return a known failure state in
the response instead of having to traverse all the responses in hasFailures.

Cheers,

Ivan

1 Like

Thanks @Ivan, yours is indeed the fastest so far :slight_smile:

Cheers,

trango