use of io.searchbox.core.BulkResult.BulkResultItem in project opennms by OpenNMS.
the class EventToIndex method sendEvents.
private void sendEvents(final List<BulkableAction<DocumentResult>> actions) {
if (actions != null && actions.size() > 0) {
// Split the actions up by index
for (Map.Entry<String, List<BulkableAction<DocumentResult>>> entry : actions.stream().collect(Collectors.groupingBy(BulkableAction::getIndex)).entrySet()) {
try {
final List<BulkableAction<DocumentResult>> actionList = entry.getValue();
if (entry.getValue().size() == 1) {
// Not bulk
final BulkableAction<DocumentResult> action = actionList.get(0);
executeSingleAction(getJestClient(), action);
} else {
// The type will be identical for all events in the same index
final String type = actionList.get(0).getType();
final Bulk.Builder builder = new Bulk.Builder().defaultIndex(entry.getKey()).defaultType(type);
// Add all actions to the bulk operation
builder.addAction(actionList);
final Bulk bulk = builder.build();
BulkResult result = getJestClient().execute(bulk);
// If the bulk command fails completely...
if (result == null || !result.isSucceeded()) {
if (result == null) {
logEsError("Bulk API action", entry.getKey(), type, null, -1, null);
} else {
logEsError("Bulk API action", entry.getKey(), type, result.getJsonString(), result.getResponseCode(), result.getErrorMessage());
}
// Try and issue the bulk actions individually as a fallback
for (BulkableAction<DocumentResult> action : entry.getValue()) {
executeSingleAction(getJestClient(), action);
}
continue;
}
// Check the return codes of the results
List<BulkResultItem> items = result.getItems();
boolean all404s = true;
for (BulkResultItem item : items) {
if (item.status != 404) {
all404s = false;
break;
}
}
if (all404s) {
if (LOG.isDebugEnabled()) {
LOG.debug("index name " + entry.getKey() + " doesn't exist, creating new index");
}
createIndex(getJestClient(), entry.getKey(), type);
result = getJestClient().execute(bulk);
}
// If the bulk command fails completely...
if (result == null || !result.isSucceeded()) {
if (result == null) {
logEsError("Bulk API action", entry.getKey(), type, null, -1, null);
} else {
logEsError("Bulk API action", entry.getKey(), type, result.getJsonString(), result.getResponseCode(), result.getErrorMessage());
}
// Try and issue the bulk actions individually as a fallback
for (BulkableAction<DocumentResult> action : entry.getValue()) {
executeSingleAction(getJestClient(), action);
}
continue;
}
// Log any unsuccessful completions as errors
for (BulkResultItem item : result.getItems()) {
if (item.status >= 200 && item.status < 300) {
if (LOG.isDebugEnabled()) {
// If debug is enabled, log all completions
logEsDebug(item.operation, entry.getKey(), item.type, "none", item.status, item.error);
}
} else {
logEsError(item.operation, entry.getKey(), item.type, "none", item.status, item.error);
}
}
}
} catch (Throwable ex) {
LOG.error("Unexpected problem sending event to Elasticsearch", ex);
// Shutdown the ES client, it will be recreated as needed
closeJestClient();
}
}
}
}
Aggregations