Search in sources :

Example 1 with DocumentResult

use of io.searchbox.core.DocumentResult in project opennms by OpenNMS.

the class EventToIndex method executeSingleAction.

/**
	 * This executes single Elasticsearch actions, creating indices as needed.
	 * 
	 * @param client
	 * @param action
	 * @throws IOException
	 */
private static void executeSingleAction(JestClient client, BulkableAction<DocumentResult> action) throws IOException {
    DocumentResult result = client.execute(action);
    if (result == null || result.getResponseCode() == 404) {
        if (LOG.isDebugEnabled()) {
            if (result == null) {
                logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), null, -1, null);
            } else {
                logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage());
            }
            LOG.debug("index name " + action.getIndex() + " doesn't exist, creating new index");
        }
        createIndex(client, action.getIndex(), action.getType());
        result = client.execute(action);
    }
    if (result == null) {
        logEsError(action.getRestMethodName(), action.getIndex(), action.getType(), null, -1, null);
    } else if (!result.isSucceeded()) {
        logEsError(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage());
    } else if (LOG.isDebugEnabled()) {
        logEsDebug(action.getRestMethodName(), action.getIndex(), action.getType(), result.getJsonString(), result.getResponseCode(), result.getErrorMessage());
    }
}
Also used : DocumentResult(io.searchbox.core.DocumentResult)

Example 2 with DocumentResult

use of io.searchbox.core.DocumentResult 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();
            }
        }
    }
}
Also used : DocumentResult(io.searchbox.core.DocumentResult) BulkableAction(io.searchbox.action.BulkableAction) Bulk(io.searchbox.core.Bulk) BulkResult(io.searchbox.core.BulkResult) BulkResultItem(io.searchbox.core.BulkResult.BulkResultItem) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) EnumMap(java.util.EnumMap)

Aggregations

DocumentResult (io.searchbox.core.DocumentResult)2 BulkableAction (io.searchbox.action.BulkableAction)1 Bulk (io.searchbox.core.Bulk)1 BulkResult (io.searchbox.core.BulkResult)1 BulkResultItem (io.searchbox.core.BulkResult.BulkResultItem)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1