Search in sources :

Example 11 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project titan by thinkaurelius.

the class ElasticSearchIndex method restore.

public void restore(Map<String, Map<String, List<IndexEntry>>> documents, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    BulkRequestBuilder bulk = client.prepareBulk();
    int requests = 0;
    try {
        for (Map.Entry<String, Map<String, List<IndexEntry>>> stores : documents.entrySet()) {
            String store = stores.getKey();
            for (Map.Entry<String, List<IndexEntry>> entry : stores.getValue().entrySet()) {
                String docID = entry.getKey();
                List<IndexEntry> content = entry.getValue();
                if (content == null || content.size() == 0) {
                    // delete
                    if (log.isTraceEnabled())
                        log.trace("Deleting entire document {}", docID);
                    bulk.add(new DeleteRequest(indexName, store, docID));
                    requests++;
                } else {
                    // Add
                    if (log.isTraceEnabled())
                        log.trace("Adding entire document {}", docID);
                    bulk.add(new IndexRequest(indexName, store, docID).source(getNewDocument(content, informations.get(store), IndexMutation.determineTTL(content))));
                    requests++;
                }
            }
        }
        if (requests > 0)
            bulk.execute().actionGet();
    } catch (Exception e) {
        throw convert(e);
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) FileNotFoundException(java.io.FileNotFoundException) TitanException(com.thinkaurelius.titan.core.TitanException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) IOException(java.io.IOException)

Example 12 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class TransportShardBulkAction method shardOperationOnReplica.

@Override
public WriteReplicaResult<BulkShardRequest> shardOperationOnReplica(BulkShardRequest request, IndexShard replica) throws Exception {
    Translog.Location location = null;
    for (int i = 0; i < request.items().length; i++) {
        BulkItemRequest item = request.items()[i];
        if (shouldExecuteReplicaItem(item, i)) {
            DocWriteRequest docWriteRequest = item.request();
            DocWriteResponse primaryResponse = item.getPrimaryResponse().getResponse();
            final Engine.Result operationResult;
            try {
                switch(docWriteRequest.opType()) {
                    case CREATE:
                    case INDEX:
                        operationResult = executeIndexRequestOnReplica(primaryResponse, (IndexRequest) docWriteRequest, replica);
                        break;
                    case DELETE:
                        operationResult = executeDeleteRequestOnReplica(primaryResponse, (DeleteRequest) docWriteRequest, replica);
                        break;
                    default:
                        throw new IllegalStateException("Unexpected request operation type on replica: " + docWriteRequest.opType().getLowercase());
                }
                if (operationResult.hasFailure()) {
                    // check if any transient write operation failures should be bubbled up
                    Exception failure = operationResult.getFailure();
                    assert failure instanceof VersionConflictEngineException || failure instanceof MapperParsingException : "expected any one of [version conflict, mapper parsing, engine closed, index shard closed]" + " failures. got " + failure;
                    if (!TransportActions.isShardNotAvailableException(failure)) {
                        throw failure;
                    }
                } else {
                    location = locationToSync(location, operationResult.getTranslogLocation());
                }
            } catch (Exception e) {
                // so we will fail the shard
                if (!TransportActions.isShardNotAvailableException(e)) {
                    throw e;
                }
            }
        }
    }
    return new WriteReplicaResult<>(request, location, null, replica, logger);
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) DocWriteResponse(org.elasticsearch.action.DocWriteResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) Translog(org.elasticsearch.index.translog.Translog) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) Engine(org.elasticsearch.index.engine.Engine)

Example 13 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class TransportShardBulkAction method executeUpdateRequest.

/**
     * Executes update request, delegating to a index or delete operation after translation,
     * handles retries on version conflict and constructs update response
     * NOTE: reassigns bulk item request at <code>requestIndex</code> for replicas to
     * execute translated update request (NOOP update is an exception). NOOP updates are
     * indicated by returning a <code>null</code> operation in {@link BulkItemResultHolder}
     * */
private static BulkItemResultHolder executeUpdateRequest(UpdateRequest updateRequest, IndexShard primary, IndexMetaData metaData, BulkShardRequest request, int requestIndex, UpdateHelper updateHelper, LongSupplier nowInMillis, final MappingUpdatePerformer mappingUpdater) throws Exception {
    Engine.Result updateOperationResult = null;
    UpdateResponse updateResponse = null;
    BulkItemRequest replicaRequest = request.items()[requestIndex];
    int maxAttempts = updateRequest.retryOnConflict();
    for (int attemptCount = 0; attemptCount <= maxAttempts; attemptCount++) {
        final UpdateHelper.Result translate;
        // translate update request
        try {
            translate = updateHelper.prepare(updateRequest, primary, nowInMillis);
        } catch (Exception failure) {
            // we may fail translating a update to index or delete operation
            // we use index result to communicate failure while translating update request
            updateOperationResult = new Engine.IndexResult(failure, updateRequest.version(), SequenceNumbersService.UNASSIGNED_SEQ_NO);
            // out of retry loop
            break;
        }
        // execute translated update request
        switch(translate.getResponseResult()) {
            case CREATED:
            case UPDATED:
                IndexRequest indexRequest = translate.action();
                MappingMetaData mappingMd = metaData.mappingOrDefault(indexRequest.type());
                indexRequest.process(mappingMd, request.index());
                updateOperationResult = executeIndexRequestOnPrimary(indexRequest, primary, mappingUpdater);
                break;
            case DELETED:
                DeleteRequest deleteRequest = translate.action();
                updateOperationResult = executeDeleteRequestOnPrimary(deleteRequest, primary);
                break;
            case NOOP:
                primary.noopUpdate(updateRequest.type());
                break;
            default:
                throw new IllegalStateException("Illegal update operation " + translate.getResponseResult());
        }
        if (updateOperationResult == null) {
            // this is a noop operation
            updateResponse = translate.action();
            // out of retry loop
            break;
        } else if (updateOperationResult.hasFailure() == false) {
            // set translated update (index/delete) request for replica execution in bulk items
            switch(updateOperationResult.getOperationType()) {
                case INDEX:
                    IndexRequest updateIndexRequest = translate.action();
                    final IndexResponse indexResponse = new IndexResponse(primary.shardId(), updateIndexRequest.type(), updateIndexRequest.id(), updateOperationResult.getSeqNo(), updateOperationResult.getVersion(), ((Engine.IndexResult) updateOperationResult).isCreated());
                    BytesReference indexSourceAsBytes = updateIndexRequest.source();
                    updateResponse = new UpdateResponse(indexResponse.getShardInfo(), indexResponse.getShardId(), indexResponse.getType(), indexResponse.getId(), indexResponse.getSeqNo(), indexResponse.getVersion(), indexResponse.getResult());
                    if ((updateRequest.fetchSource() != null && updateRequest.fetchSource().fetchSource()) || (updateRequest.fields() != null && updateRequest.fields().length > 0)) {
                        Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(indexSourceAsBytes, true, updateIndexRequest.getContentType());
                        updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, request.index(), indexResponse.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), indexSourceAsBytes));
                    }
                    // set translated request as replica request
                    replicaRequest = new BulkItemRequest(request.items()[requestIndex].id(), updateIndexRequest);
                    break;
                case DELETE:
                    DeleteRequest updateDeleteRequest = translate.action();
                    DeleteResponse deleteResponse = new DeleteResponse(primary.shardId(), updateDeleteRequest.type(), updateDeleteRequest.id(), updateOperationResult.getSeqNo(), updateOperationResult.getVersion(), ((Engine.DeleteResult) updateOperationResult).isFound());
                    updateResponse = new UpdateResponse(deleteResponse.getShardInfo(), deleteResponse.getShardId(), deleteResponse.getType(), deleteResponse.getId(), deleteResponse.getSeqNo(), deleteResponse.getVersion(), deleteResponse.getResult());
                    updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, request.index(), deleteResponse.getVersion(), translate.updatedSourceAsMap(), translate.updateSourceContentType(), null));
                    // set translated request as replica request
                    replicaRequest = new BulkItemRequest(request.items()[requestIndex].id(), updateDeleteRequest);
                    break;
            }
            assert updateOperationResult.getSeqNo() != SequenceNumbersService.UNASSIGNED_SEQ_NO;
            // out of retry loop
            break;
        } else if (updateOperationResult.getFailure() instanceof VersionConflictEngineException == false) {
            // out of retry loop
            break;
        }
    }
    return new BulkItemResultHolder(updateResponse, updateOperationResult, replicaRequest);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexRequest(org.elasticsearch.action.index.IndexRequest) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateHelper(org.elasticsearch.action.update.UpdateHelper) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) Engine(org.elasticsearch.index.engine.Engine) Tuple(org.elasticsearch.common.collect.Tuple)

Example 14 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class TransportShardBulkAction method executeBulkItemRequest.

/** Executes bulk item requests and handles request execution exceptions */
static Translog.Location executeBulkItemRequest(IndexMetaData metaData, IndexShard primary, BulkShardRequest request, Translog.Location location, int requestIndex, UpdateHelper updateHelper, LongSupplier nowInMillisSupplier, final MappingUpdatePerformer mappingUpdater) throws Exception {
    final DocWriteRequest itemRequest = request.items()[requestIndex].request();
    final DocWriteRequest.OpType opType = itemRequest.opType();
    final BulkItemResultHolder responseHolder;
    switch(itemRequest.opType()) {
        case CREATE:
        case INDEX:
            responseHolder = executeIndexRequest((IndexRequest) itemRequest, request.items()[requestIndex], primary, mappingUpdater);
            break;
        case UPDATE:
            responseHolder = executeUpdateRequest((UpdateRequest) itemRequest, primary, metaData, request, requestIndex, updateHelper, nowInMillisSupplier, mappingUpdater);
            break;
        case DELETE:
            responseHolder = executeDeleteRequest((DeleteRequest) itemRequest, request.items()[requestIndex], primary);
            break;
        default:
            throw new IllegalStateException("unexpected opType [" + itemRequest.opType() + "] found");
    }
    final BulkItemRequest replicaRequest = responseHolder.replicaRequest;
    // update the bulk item request because update request execution can mutate the bulk item request
    request.items()[requestIndex] = replicaRequest;
    // Modify the replica request, if needed, and return a new translog location
    location = updateReplicaRequest(responseHolder, opType, location, request);
    assert replicaRequest.getPrimaryResponse() != null : "replica request must have a primary response";
    return location;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 15 with DeleteRequest

use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.

the class AsyncDeleteByQueryAction method buildRequest.

@Override
protected RequestWrapper<DeleteRequest> buildRequest(ScrollableHitSource.Hit doc) {
    DeleteRequest delete = new DeleteRequest();
    delete.index(doc.getIndex());
    delete.type(doc.getType());
    delete.id(doc.getId());
    delete.version(doc.getVersion());
    return wrap(delete);
}
Also used : DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Aggregations

DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)33 IndexRequest (org.elasticsearch.action.index.IndexRequest)25 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)18 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)8 IOException (java.io.IOException)7 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)7 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)7 ArrayList (java.util.ArrayList)5 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 GetRequest (org.elasticsearch.action.get.GetRequest)4 BytesReference (org.elasticsearch.common.bytes.BytesReference)4 XContentType (org.elasticsearch.common.xcontent.XContentType)4 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)3 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)3 VersionConflictEngineException (org.elasticsearch.index.engine.VersionConflictEngineException)3 TitanException (com.thinkaurelius.titan.core.TitanException)2 FileNotFoundException (java.io.FileNotFoundException)2