Search in sources :

Example 36 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project metacat by Netflix.

the class ElasticSearchUtilImpl method bulkSaveToIndex.

/**
 * Bulk save of the entities.
 *
 * @param type index type
 * @param docs metacat documents
 */
private void bulkSaveToIndex(final String type, final List<ElasticSearchDoc> docs, final String index) {
    if (docs != null && !docs.isEmpty()) {
        try {
            RETRY_ES_PUBLISH.call(() -> {
                final BulkRequestBuilder bulkRequest = client.prepareBulk();
                for (ElasticSearchDoc doc : docs) {
                    final IndexRequestBuilder indexRequestBuilder = prepareIndexRequest(index, type, doc);
                    if (indexRequestBuilder != null) {
                        bulkRequest.add(indexRequestBuilder);
                    }
                }
                if (bulkRequest.numberOfActions() > 0) {
                    final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
                    log.info("Bulk saving metadata of index {} type {} with size {}.", index, type, docs.size());
                    if (bulkResponse.hasFailures()) {
                        for (BulkItemResponse item : bulkResponse.getItems()) {
                            if (item.isFailed()) {
                                log.error("Failed saving metadata of {} index type {} with id {}. Message: {}", index, type, item.getId(), item.getFailureMessage());
                                this.elasticSearchMetric.getElasticSearchSaveFailureCounter().increment();
                                log("ElasticSearchUtil.bulkSaveToIndex.index", type, item.getId(), null, item.getFailureMessage(), null, true, index);
                            }
                        }
                    }
                }
                return null;
            });
        } catch (Exception e) {
            log.error("Failed saving metadatas of index {} type {}. {}", index, type, e);
            this.elasticSearchMetric.getElasticSearchBulkSaveFailureCounter().increment();
            final List<String> docIds = docs.stream().map(ElasticSearchDoc::getId).collect(Collectors.toList());
            log("ElasticSearchUtil.bulkSaveToIndex", type, docIds.toString(), null, e.getMessage(), e, true, index);
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) List(java.util.List) ArrayList(java.util.ArrayList) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) FailedNodeException(org.elasticsearch.action.FailedNodeException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ReceiveTimeoutTransportException(org.elasticsearch.transport.ReceiveTimeoutTransportException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 37 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project metacat by Netflix.

the class ElasticSearchUtilImpl method softDeleteDoc.

/* Use elasticSearch bulk API to mark the documents as deleted
    * @param type index type
    * @param ids list of entity ids
    * @param metacatRequestContext context containing the user name
    */
private void softDeleteDoc(final String type, final List<String> ids, final MetacatRequestContext metacatRequestContext) {
    try {
        RETRY_ES_PUBLISH.call(() -> {
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            final XContentBuilder builder = XContentFactory.contentBuilder(contentType);
            builder.startObject().field(ElasticSearchDoc.Field.DELETED, true).field(ElasticSearchDoc.Field.TIMESTAMP, java.time.Instant.now().toEpochMilli()).field(ElasticSearchDoc.Field.USER, metacatRequestContext.getUserName()).endObject();
            ids.forEach(id -> bulkRequest.add(client.prepareUpdate(esIndex, type, id).setRetryOnConflict(NO_OF_CONFLICT_RETRIES).setDoc(builder)));
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet();
            if (bulkResponse.hasFailures()) {
                for (BulkItemResponse item : bulkResponse.getItems()) {
                    if (item.isFailed()) {
                        log.error("Failed soft deleting metadata of type {} with id {}. Message: {}", type, item.getId(), item.getFailureMessage());
                        this.elasticSearchMetric.getElasticSearchDeleteFailureCounter().increment();
                        log("ElasticSearchUtil.bulkSoftDelete.item", type, item.getId(), null, item.getFailureMessage(), null, true);
                    }
                }
            }
            return null;
        });
    } catch (Exception e) {
        log.error("Failed soft deleting metadata of type {} with ids {}. {}", type, ids, e);
        this.elasticSearchMetric.getElasticSearchBulkDeleteFailureCounter().increment();
        log("ElasticSearchUtil.bulkSoftDelete", type, ids.toString(), null, e.getMessage(), e, true);
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) FailedNodeException(org.elasticsearch.action.FailedNodeException) NodeClosedException(org.elasticsearch.node.NodeClosedException) ReceiveTimeoutTransportException(org.elasticsearch.transport.ReceiveTimeoutTransportException) NoNodeAvailableException(org.elasticsearch.client.transport.NoNodeAvailableException) TransportException(org.elasticsearch.transport.TransportException) ElasticsearchTimeoutException(org.elasticsearch.ElasticsearchTimeoutException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Aggregations

BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)37 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)30 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)14 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)9 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)7 IndexRequest (org.elasticsearch.action.index.IndexRequest)6 ArrayList (java.util.ArrayList)5 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)5 FailedNodeException (org.elasticsearch.action.FailedNodeException)5 NoNodeAvailableException (org.elasticsearch.client.transport.NoNodeAvailableException)5 NodeClosedException (org.elasticsearch.node.NodeClosedException)5 ReceiveTimeoutTransportException (org.elasticsearch.transport.ReceiveTimeoutTransportException)5 TransportException (org.elasticsearch.transport.TransportException)5 BulkWriterResponse (org.apache.metron.common.writer.BulkWriterResponse)4 Tuple (org.apache.storm.tuple.Tuple)4 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)4 IndexResponse (org.elasticsearch.action.index.IndexResponse)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)4 Client (org.elasticsearch.client.Client)4