Search in sources :

Example 11 with Failure

use of org.elasticsearch.action.bulk.BulkItemResponse.Failure in project snow-owl by b2ihealthcare.

the class EsIndexAdmin method bulkIndexByScroll.

private boolean bulkIndexByScroll(final EsClient client, final DocumentMapping mapping, final Expression filter, final String command, final org.elasticsearch.script.Script script, final String operationDescription) {
    final QueryBuilder query = new EsQueryBuilder(mapping, settings, log).build(filter);
    boolean needsRefresh = false;
    long versionConflicts = 0;
    int attempts = DEFAULT_MAX_NUMBER_OF_VERSION_CONFLICT_RETRIES;
    do {
        try {
            final BulkByScrollResponse response;
            final int batchSize = Integer.parseInt((String) settings.get(IndexClientFactory.RESULT_WINDOW_KEY));
            if ("update".equals(command)) {
                response = client.updateByQuery(getTypeIndex(mapping), batchSize, script, query);
            } else if ("delete".equals(command)) {
                response = client.deleteByQuery(getTypeIndex(mapping), batchSize, query);
            } else {
                throw new UnsupportedOperationException("Not implemented command: " + command);
            }
            final long updateCount = response.getUpdated();
            final long deleteCount = response.getDeleted();
            final long noops = response.getNoops();
            final List<Failure> failures = response.getBulkFailures();
            versionConflicts = response.getVersionConflicts();
            boolean updated = updateCount > 0;
            if (updated) {
                log().info("Updated {} {} documents with bulk {}", updateCount, mapping.typeAsString(), operationDescription);
                needsRefresh = true;
            }
            boolean deleted = deleteCount > 0;
            if (deleted) {
                log().info("Deleted {} {} documents with bulk {}", deleteCount, mapping.typeAsString(), operationDescription);
                needsRefresh = true;
            }
            if (!updated && !deleted) {
                log().warn("Bulk {} could not be applied to {} documents, no-ops ({}), conflicts ({})", operationDescription, mapping.typeAsString(), noops, versionConflicts);
            }
            if (failures.size() > 0) {
                boolean versionConflictsOnly = true;
                for (Failure failure : failures) {
                    final String failureMessage = failure.getCause().getMessage();
                    final int failureStatus = failure.getStatus().getStatus();
                    if (failureStatus != RestStatus.CONFLICT.getStatus()) {
                        versionConflictsOnly = false;
                        log().error("Index failure during bulk update: {}", failureMessage);
                    } else {
                        log().warn("Version conflict reason: {}", failureMessage);
                    }
                }
                if (!versionConflictsOnly) {
                    throw new IllegalStateException("There were indexing failures during bulk updates. See logs for all failures.");
                }
            }
            if (attempts <= 0) {
                throw new IndexException("There were indexing failures during bulk updates. See logs for all failures.", null);
            }
            if (versionConflicts > 0) {
                --attempts;
                try {
                    Thread.sleep(100 + random.nextInt(900));
                    refresh(Collections.singleton(mapping));
                } catch (InterruptedException e) {
                    throw new IndexException("Interrupted", e);
                }
            }
        } catch (IOException e) {
            throw new IndexException("Could not execute bulk update.", e);
        }
    } while (versionConflicts > 0);
    return needsRefresh;
}
Also used : EsQueryBuilder(com.b2international.index.es.query.EsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) EsQueryBuilder(com.b2international.index.es.query.EsQueryBuilder) Failure(org.elasticsearch.action.bulk.BulkItemResponse.Failure)

Aggregations

Failure (org.elasticsearch.action.bulk.BulkItemResponse.Failure)11 SearchFailure (org.elasticsearch.action.bulk.byscroll.ScrollableHitSource.SearchFailure)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)3 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)3 IOException (java.io.IOException)2 BulkByScrollResponse (org.elasticsearch.action.bulk.byscroll.BulkByScrollResponse)2 ShardSearchFailure (org.elasticsearch.action.search.ShardSearchFailure)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 EsQueryBuilder (com.b2international.index.es.query.EsQueryBuilder)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)1 DocMap (org.codelibs.fess.util.DocMap)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1