Search in sources :

Example 51 with BulkRequestBuilder

use of org.elasticsearch.action.bulk.BulkRequestBuilder 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 52 with BulkRequestBuilder

use of org.elasticsearch.action.bulk.BulkRequestBuilder 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

BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)52 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)36 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)14 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)12 IOException (java.io.IOException)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)8 SearchHit (org.elasticsearch.search.SearchHit)8 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)7 HashMap (java.util.HashMap)6 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)5 FailedNodeException (org.elasticsearch.action.FailedNodeException)5 IndexRequest (org.elasticsearch.action.index.IndexRequest)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 IllegalBehaviorStateException (org.dbflute.exception.IllegalBehaviorStateException)4 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)4 UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)4 SearchHits (org.elasticsearch.search.SearchHits)4