protected void executeBulkRequest(BulkRequestBuilder bulkRequest) {
   if (bulkRequest.numberOfActions() == 0) return;
   BulkResponse bulkResponse = bulkRequest.execute().actionGet();
   if (!bulkResponse.hasFailures()) return;
   for (BulkItemResponse response : bulkResponse) {
     if (!response.isFailed()) continue;
     LOG.warning(
         String.format(
             "Unable to save Entity %s in %s/%s, cause: %s",
             response.getId(),
             response.getIndex(),
             response.getType(),
             response.getFailureMessage()));
   }
 }
Exemple #2
0
    @Override
    public void onResponse(BulkResponse response) {
      semaphore.release();
      counter.addAndGet(response.getItems().length);

      for (BulkItemResponse item : response.getItems()) {
        if (item.isFailed()) {
          LOGGER.error(
              "index [{}], type [{}], id [{}], message [{}]",
              item.getIndex(),
              item.getType(),
              item.getId(),
              item.getFailureMessage());
        }
      }
    }
  /**
   * This method processes failures by iterating through each bulk response item
   *
   * @param response, a BulkResponse
   */
  private void processBulkResponseFailure(BulkResponse response) {
    logger.warn("There was failures when executing bulk : " + response.buildFailureMessage());

    if (!logger.isDebugEnabled()) return;

    for (BulkItemResponse item : response.getItems()) {
      if (item.isFailed()) {
        logger.debug(
            "Error {} occurred on index {}, type {}, id {} for {} operation ",
            item.getFailureMessage(),
            item.getIndex(),
            item.getType(),
            item.getId(),
            item.getOpType());
      }
    }
  }
 @Override
 public void bulkIndex(List<IndexQuery> queries) {
   BulkRequestBuilder bulkRequest = client.prepareBulk();
   for (IndexQuery query : queries) {
     bulkRequest.add(prepareIndex(query));
   }
   BulkResponse bulkResponse = bulkRequest.execute().actionGet();
   if (bulkResponse.hasFailures()) {
     Map<String, String> failedDocuments = new HashMap<String, String>();
     for (BulkItemResponse item : bulkResponse.getItems()) {
       if (item.isFailed()) failedDocuments.put(item.getId(), item.getFailureMessage());
     }
     throw new ElasticsearchException(
         "Bulk indexing has failures. Use ElasticsearchException.getFailedDocuments() for detailed messages ["
             + failedDocuments
             + "]",
         failedDocuments);
   }
 }
  @Test
  public void testBulk_withIndexAndDeleteItems() throws Exception {
    prepareIndex(1);
    BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
    for (int i = 0; i < 10; i++) {
      bulkRequestBuilder.add(client().prepareIndex("idx", "type").setSource("{}"));
    }

    BulkResponse bulkResponse = bulkRequestBuilder.get();
    bulkRequestBuilder = client().prepareBulk();
    for (BulkItemResponse item : bulkResponse) {
      assertThat(item.isFailed(), equalTo(false));
      assertShardInfo(item.getResponse());
      bulkRequestBuilder.add(client().prepareDelete("idx", "type", item.getId()));
    }

    bulkResponse = bulkRequestBuilder.get();
    for (BulkItemResponse item : bulkResponse) {
      assertThat(item.isFailed(), equalTo(false));
      assertShardInfo(item.getResponse());
    }
  }