Search in sources :

Example 26 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
     */
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.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());
                        registry.counter(registry.createId(Metrics.CounterElasticSearchDelete.name()).withTags(Metrics.statusFailureMap)).increment();
                        log("ElasticSearchUtil.bulkSoftDelete.item", type, item.getId(), null, item.getFailureMessage(), null, true);
                    }
                }
            }
            return null;
        });
    } catch (Exception e) {
        log.error(String.format("Failed soft deleting metadata of type %s with ids %s", type, ids), e);
        registry.counter(registry.createId(Metrics.CounterElasticSearchBulkDelete.name()).withTags(Metrics.statusFailureMap)).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)

Example 27 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project samza by apache.

the class ElasticsearchSystemProducerTest method getRespWithFailedDocument.

private BulkResponse getRespWithFailedDocument(RestStatus status) {
    BulkResponse response = mock(BulkResponse.class);
    when(response.hasFailures()).thenReturn(true);
    BulkItemResponse itemResp = mock(BulkItemResponse.class);
    when(itemResp.isFailed()).thenReturn(true);
    BulkItemResponse.Failure failure = mock(BulkItemResponse.Failure.class);
    when(failure.getStatus()).thenReturn(status);
    when(itemResp.getFailure()).thenReturn(failure);
    BulkItemResponse[] itemResponses = new BulkItemResponse[] { itemResp };
    when(response.getItems()).thenReturn(itemResponses);
    return response;
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse)

Example 28 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project fess by codelibs.

the class FessEsClient method addAll.

public void addAll(final String index, final String type, final List<Map<String, Object>> docList) {
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    final BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    for (final Map<String, Object> doc : docList) {
        final Object id = doc.remove(fessConfig.getIndexFieldId());
        bulkRequestBuilder.add(client.prepareIndex(index, type, id.toString()).setSource(new DocMap(doc)));
    }
    final BulkResponse response = bulkRequestBuilder.execute().actionGet(ComponentUtil.getFessConfig().getIndexBulkTimeout());
    if (response.hasFailures()) {
        if (logger.isDebugEnabled()) {
            @SuppressWarnings("rawtypes") final List<DocWriteRequest> requests = bulkRequestBuilder.request().requests();
            final BulkItemResponse[] items = response.getItems();
            if (requests.size() == items.length) {
                for (int i = 0; i < requests.size(); i++) {
                    final BulkItemResponse resp = items[i];
                    if (resp.isFailed() && resp.getFailure() != null) {
                        final DocWriteRequest<?> req = requests.get(i);
                        final Failure failure = resp.getFailure();
                        logger.debug("Failed Request: " + req + "\n=>" + failure.getMessage());
                    }
                }
            }
        }
        throw new FessEsClientException(response.buildFailureMessage());
    }
}
Also used : BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) DocMap(org.codelibs.fess.util.DocMap) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) Failure(org.elasticsearch.action.bulk.BulkItemResponse.Failure)

Example 29 with BulkItemResponse

use of org.elasticsearch.action.bulk.BulkItemResponse in project fess by codelibs.

the class EsAbstractBehavior method delegateBatchRequest.

protected <BUILDER> int[] delegateBatchRequest(final List<? extends Entity> entityList, Function<EsAbstractEntity, BUILDER> call) {
    @SuppressWarnings("unchecked") final BulkList<? extends Entity, BUILDER> bulkList = (BulkList<? extends Entity, BUILDER>) entityList;
    final RequestOptionCall<BUILDER> builderEntityCall = bulkList.getEntityCall();
    final BulkRequestBuilder bulkBuilder = client.prepareBulk();
    for (final Entity entity : entityList) {
        final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
        BUILDER builder = call.apply(esEntity);
        if (builder instanceof IndexRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((IndexRequestBuilder) builder);
        } else if (builder instanceof UpdateRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((UpdateRequestBuilder) builder);
        } else if (builder instanceof DeleteRequestBuilder) {
            if (builderEntityCall != null) {
                builderEntityCall.callback(builder);
            }
            bulkBuilder.add((DeleteRequestBuilder) builder);
        }
    }
    final RequestOptionCall<BulkRequestBuilder> builderCall = bulkList.getCall();
    if (builderCall != null) {
        builderCall.callback(bulkBuilder);
    }
    final BulkResponse response = bulkBuilder.execute().actionGet(bulkTimeout);
    final BulkItemResponse[] itemResponses = response.getItems();
    if (itemResponses.length != entityList.size()) {
        throw new IllegalStateException("Invalid response size: " + itemResponses.length + " != " + entityList.size());
    }
    final int[] results = new int[itemResponses.length];
    for (int i = 0; i < itemResponses.length; i++) {
        final BulkItemResponse itemResponse = itemResponses[i];
        final Entity entity = entityList.get(i);
        if (entity instanceof EsAbstractEntity) {
            ((EsAbstractEntity) entity).asDocMeta().id(itemResponse.getId());
        }
        results[i] = itemResponse.isFailed() ? 0 : 1;
    }
    return results;
}
Also used : DeleteRequestBuilder(org.elasticsearch.action.delete.DeleteRequestBuilder) Entity(org.dbflute.Entity) UpdateRequestBuilder(org.elasticsearch.action.update.UpdateRequestBuilder) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder)

Aggregations

BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)29 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)24 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)13 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)8 IndexRequest (org.elasticsearch.action.index.IndexRequest)6 ArrayList (java.util.ArrayList)5 EsRejectedExecutionException (org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)5 ElasticsearchTimeoutException (org.elasticsearch.ElasticsearchTimeoutException)4 FailedNodeException (org.elasticsearch.action.FailedNodeException)4 Failure (org.elasticsearch.action.bulk.BulkItemResponse.Failure)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 UpdateRequestBuilder (org.elasticsearch.action.update.UpdateRequestBuilder)4 IOException (java.io.IOException)3 List (java.util.List)3 Entity (org.dbflute.Entity)3 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)3 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)3 SearchFailure (org.elasticsearch.action.bulk.byscroll.ScrollableHitSource.SearchFailure)3 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)3 DeleteRequestBuilder (org.elasticsearch.action.delete.DeleteRequestBuilder)3