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

**URL:** <https://discuss.elastic.co/t/java-quick-way-to-get-the-failure-number-in-bulkresponse/57985>\
**Category:** Elasticsearch\
**Created:** [August 14, 2016, 12:10pm UTC](https://discuss.elastic.co/t/java-quick-way-to-get-the-failure-number-in-bulkresponse/57985 "2016-08-14T12:10:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trango](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@trango](https://discuss.elastic.co/u/trango)\
**Post date:** [August 14, 2016, 12:10pm UTC](https://discuss.elastic.co/t/java-quick-way-to-get-the-failure-number-in-bulkresponse/57985/1 "2016-08-14T12:10:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [August 14, 2016, 6:17pm UTC](https://discuss.elastic.co/t/java-quick-way-to-get-the-failure-number-in-bulkresponse/57985/2 "2016-08-14T18:17:05Z")

</div>

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. 🙂

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

---

<div class="post-metadata">

**Author:** ![trango](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@trango](https://discuss.elastic.co/u/trango)\
**Post date:** [August 21, 2016, 4:24am UTC](https://discuss.elastic.co/t/java-quick-way-to-get-the-failure-number-in-bulkresponse/57985/3 "2016-08-21T04:24:04Z")

</div>

Thanks @Ivan, yours is indeed the fastest so far 🙂

Cheers,

trango

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:26pm UTC](https://discuss.elastic.co/t/java-quick-way-to-get-the-failure-number-in-bulkresponse/57985/4 "2017-07-05T22:26:23Z")

</div>


