List<Object> result = null;
long retriesLeft = this.bulkIndexRetries;
int attempt = 0;
BulkResponse response = null;
do {
// build the bulk request for this iteration
BulkRequestBuilder bulkBuilder = client.prepareBulk();
for (Entry<String,IndexRequest> entry : bulkIndexRequests.entrySet()) {
bulkBuilder.add(entry.getValue());
}
for (Entry<String,DeleteRequest> entry : bulkDeleteRequests.entrySet()) {
bulkBuilder.add(entry.getValue());
}
attempt++;
result = new ArrayList<Object>();
if(response != null) {
// at least second time through
try {
Thread.sleep(this.bulkIndexRetryWaitMs);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
response = bulkBuilder.execute().actionGet();
if(response != null) {
for (BulkItemResponse bulkItemResponse : response.getItems()) {
if(!bulkItemResponse.isFailed()) {
String itemId = bulkItemResponse.getId();
String itemRev = revisions.get(itemId);
Map<String, Object> itemResponse = new HashMap<String, Object>();
itemResponse.put("id", itemId);
itemResponse.put("rev", itemRev);
result.add(itemResponse);
// remove the item from the bulk requests list
// so we don't try to index it again
bulkIndexRequests.remove(itemId);
bulkDeleteRequests.remove(itemId);
} else {
Failure failure = bulkItemResponse.getFailure();
// if the error is fatal don't retry
if(failureMessageAppearsFatal(failure.getMessage())) {
throw new RuntimeException("indexing error " + failure.getMessage());
}
}
}
}
retriesLeft--;
} while((response != null) && (response.hasFailures()) && (retriesLeft > 0));
if(response == null) {
throw new RuntimeException("indexing error, bulk response was null");
}