Search in sources :

Example 1 with Bulk

use of io.searchbox.core.Bulk in project gerrit by GerritCodeReview.

the class AbstractElasticIndex method delete.

@Override
public void delete(K c) throws IOException {
    Bulk bulk = addActions(new Bulk.Builder(), c).refresh(true).build();
    JestResult result = client.execute(bulk);
    if (!result.isSucceeded()) {
        throw new IOException(String.format("Failed to delete change %s in index %s: %s", c, indexName, result.getErrorMessage()));
    }
}
Also used : IOException(java.io.IOException) Bulk(io.searchbox.core.Bulk) JestResult(io.searchbox.client.JestResult)

Example 2 with Bulk

use of io.searchbox.core.Bulk in project gerrit by GerritCodeReview.

the class ElasticAccountIndex method replace.

@Override
public void replace(AccountState as) throws IOException {
    Bulk bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(ACCOUNTS).addAction(insert(ACCOUNTS, as)).refresh(true).build();
    JestResult result = client.execute(bulk);
    if (!result.isSucceeded()) {
        throw new IOException(String.format("Failed to replace account %s in index %s: %s", as.getAccount().getId(), indexName, result.getErrorMessage()));
    }
}
Also used : Builder(io.searchbox.core.Bulk.Builder) IOException(java.io.IOException) Bulk(io.searchbox.core.Bulk) JestResult(io.searchbox.client.JestResult)

Example 3 with Bulk

use of io.searchbox.core.Bulk in project gerrit by GerritCodeReview.

the class ElasticChangeIndex method replace.

@Override
public void replace(ChangeData cd) throws IOException {
    String deleteIndex;
    String insertIndex;
    try {
        if (cd.change().getStatus().isOpen()) {
            insertIndex = OPEN_CHANGES;
            deleteIndex = CLOSED_CHANGES;
        } else {
            insertIndex = CLOSED_CHANGES;
            deleteIndex = OPEN_CHANGES;
        }
    } catch (OrmException e) {
        throw new IOException(e);
    }
    Bulk bulk = new Bulk.Builder().defaultIndex(indexName).defaultType("changes").addAction(insert(insertIndex, cd)).addAction(delete(deleteIndex, cd.getId())).refresh(true).build();
    JestResult result = client.execute(bulk);
    if (!result.isSucceeded()) {
        throw new IOException(String.format("Failed to replace change %s in index %s: %s", cd.getId(), indexName, result.getErrorMessage()));
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) Builder(io.searchbox.core.Bulk.Builder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) Bulk(io.searchbox.core.Bulk) JestResult(io.searchbox.client.JestResult)

Example 4 with Bulk

use of io.searchbox.core.Bulk 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)

Example 5 with Bulk

use of io.searchbox.core.Bulk in project gerrit by GerritCodeReview.

the class ElasticGroupIndex method replace.

@Override
public void replace(AccountGroup group) throws IOException {
    Bulk bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(GROUPS).addAction(insert(GROUPS, group)).refresh(true).build();
    JestResult result = client.execute(bulk);
    if (!result.isSucceeded()) {
        throw new IOException(String.format("Failed to replace group %s in index %s: %s", group.getGroupUUID().get(), indexName, result.getErrorMessage()));
    }
}
Also used : Builder(io.searchbox.core.Bulk.Builder) IOException(java.io.IOException) Bulk(io.searchbox.core.Bulk) JestResult(io.searchbox.client.JestResult)

Aggregations

Bulk (io.searchbox.core.Bulk)5 JestResult (io.searchbox.client.JestResult)4 IOException (java.io.IOException)4 Builder (io.searchbox.core.Bulk.Builder)3 OrmException (com.google.gwtorm.server.OrmException)1 BulkableAction (io.searchbox.action.BulkableAction)1 BulkResult (io.searchbox.core.BulkResult)1 BulkResultItem (io.searchbox.core.BulkResult.BulkResultItem)1 DocumentResult (io.searchbox.core.DocumentResult)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 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1