Search in sources :

Example 16 with DeleteRequest

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

the class TransportUpdateAction method shardOperation.

protected void shardOperation(final UpdateRequest request, final ActionListener<UpdateResponse> listener, final int retryCount) {
    final ShardId shardId = request.getShardId();
    final IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    final IndexShard indexShard = indexService.getShard(shardId.getId());
    final UpdateHelper.Result result = updateHelper.prepare(request, indexShard, threadPool::absoluteTimeInMillis);
    switch(result.getResponseResult()) {
        case CREATED:
            IndexRequest upsertRequest = result.action();
            // we fetch it from the index request so we don't generate the bytes twice, its already done in the index request
            final BytesReference upsertSourceBytes = upsertRequest.source();
            bulkAction.execute(toSingleItemBulkRequest(upsertRequest), wrapBulkResponse(ActionListener.<IndexResponse>wrap(response -> {
                UpdateResponse update = new UpdateResponse(response.getShardInfo(), response.getShardId(), response.getType(), response.getId(), response.getSeqNo(), response.getVersion(), response.getResult());
                if ((request.fetchSource() != null && request.fetchSource().fetchSource()) || (request.fields() != null && request.fields().length > 0)) {
                    Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(upsertSourceBytes, true, upsertRequest.getContentType());
                    update.setGetResult(updateHelper.extractGetResult(request, request.concreteIndex(), response.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), upsertSourceBytes));
                } else {
                    update.setGetResult(null);
                }
                update.setForcedRefresh(response.forcedRefresh());
                listener.onResponse(update);
            }, exception -> handleUpdateFailureWithRetry(listener, request, exception, retryCount))));
            break;
        case UPDATED:
            IndexRequest indexRequest = result.action();
            // we fetch it from the index request so we don't generate the bytes twice, its already done in the index request
            final BytesReference indexSourceBytes = indexRequest.source();
            bulkAction.execute(toSingleItemBulkRequest(indexRequest), wrapBulkResponse(ActionListener.<IndexResponse>wrap(response -> {
                UpdateResponse update = new UpdateResponse(response.getShardInfo(), response.getShardId(), response.getType(), response.getId(), response.getSeqNo(), response.getVersion(), response.getResult());
                update.setGetResult(updateHelper.extractGetResult(request, request.concreteIndex(), response.getVersion(), result.updatedSourceAsMap(), result.updateSourceContentType(), indexSourceBytes));
                update.setForcedRefresh(response.forcedRefresh());
                listener.onResponse(update);
            }, exception -> handleUpdateFailureWithRetry(listener, request, exception, retryCount))));
            break;
        case DELETED:
            DeleteRequest deleteRequest = result.action();
            bulkAction.execute(toSingleItemBulkRequest(deleteRequest), wrapBulkResponse(ActionListener.<DeleteResponse>wrap(response -> {
                UpdateResponse update = new UpdateResponse(response.getShardInfo(), response.getShardId(), response.getType(), response.getId(), response.getSeqNo(), response.getVersion(), response.getResult());
                update.setGetResult(updateHelper.extractGetResult(request, request.concreteIndex(), response.getVersion(), result.updatedSourceAsMap(), result.updateSourceContentType(), null));
                update.setForcedRefresh(response.forcedRefresh());
                listener.onResponse(update);
            }, exception -> handleUpdateFailureWithRetry(listener, request, exception, retryCount))));
            break;
        case NOOP:
            UpdateResponse update = result.action();
            IndexService indexServiceOrNull = indicesService.indexService(shardId.getIndex());
            if (indexServiceOrNull != null) {
                IndexShard shard = indexService.getShardOrNull(shardId.getId());
                if (shard != null) {
                    shard.noopUpdate(request.type());
                }
            }
            listener.onResponse(update);
            break;
        default:
            throw new IllegalStateException("Illegal result " + result.getResponseResult());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) ShardId(org.elasticsearch.index.shard.ShardId) XContentType(org.elasticsearch.common.xcontent.XContentType) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Map(java.util.Map) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 17 with DeleteRequest

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

the class UpdateHelper method prepare.

/**
     * Prepares an update request by converting it into an index or delete request or an update response (no action).
     */
@SuppressWarnings("unchecked")
protected Result prepare(ShardId shardId, UpdateRequest request, final GetResult getResult, LongSupplier nowInMillis) {
    if (!getResult.isExists()) {
        if (request.upsertRequest() == null && !request.docAsUpsert()) {
            throw new DocumentMissingException(shardId, request.type(), request.id());
        }
        IndexRequest indexRequest = request.docAsUpsert() ? request.doc() : request.upsertRequest();
        if (request.scriptedUpsert() && request.script() != null) {
            // Run the script to perform the create logic
            IndexRequest upsert = request.upsertRequest();
            Map<String, Object> upsertDoc = upsert.sourceAsMap();
            Map<String, Object> ctx = new HashMap<>(2);
            // Tell the script that this is a create and not an update
            ctx.put("op", "create");
            ctx.put("_source", upsertDoc);
            ctx.put("_now", nowInMillis.getAsLong());
            ctx = executeScript(request.script, ctx);
            //Allow the script to abort the create by setting "op" to "none"
            String scriptOpChoice = (String) ctx.get("op");
            // (the default) or "none", meaning abort upsert
            if (!"create".equals(scriptOpChoice)) {
                if (!"none".equals(scriptOpChoice)) {
                    logger.warn("Used upsert operation [{}] for script [{}], doing nothing...", scriptOpChoice, request.script.getIdOrCode());
                }
                UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
                update.setGetResult(getResult);
                return new Result(update, DocWriteResponse.Result.NOOP, upsertDoc, XContentType.JSON);
            }
            indexRequest.source((Map) ctx.get("_source"));
        }
        indexRequest.index(request.index()).type(request.type()).id(request.id()).create(true).setRefreshPolicy(request.getRefreshPolicy()).routing(request.routing()).parent(request.parent()).waitForActiveShards(request.waitForActiveShards());
        if (request.versionType() != VersionType.INTERNAL) {
            // in all but the internal versioning mode, we want to create the new document using the given version.
            indexRequest.version(request.version()).versionType(request.versionType());
        }
        return new Result(indexRequest, DocWriteResponse.Result.CREATED, null, null);
    }
    long updateVersion = getResult.getVersion();
    if (request.versionType() != VersionType.INTERNAL) {
        assert request.versionType() == VersionType.FORCE;
        // remember, match_any is excluded by the conflict test
        updateVersion = request.version();
    }
    if (getResult.internalSourceRef() == null) {
        // no source, we can't do nothing, through a failure...
        throw new DocumentSourceMissingException(shardId, request.type(), request.id());
    }
    Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
    String operation = null;
    final Map<String, Object> updatedSourceAsMap;
    final XContentType updateSourceContentType = sourceAndContent.v1();
    String routing = getResult.getFields().containsKey(RoutingFieldMapper.NAME) ? getResult.field(RoutingFieldMapper.NAME).getValue().toString() : null;
    String parent = getResult.getFields().containsKey(ParentFieldMapper.NAME) ? getResult.field(ParentFieldMapper.NAME).getValue().toString() : null;
    if (request.script() == null && request.doc() != null) {
        IndexRequest indexRequest = request.doc();
        updatedSourceAsMap = sourceAndContent.v2();
        if (indexRequest.routing() != null) {
            routing = indexRequest.routing();
        }
        if (indexRequest.parent() != null) {
            parent = indexRequest.parent();
        }
        boolean noop = !XContentHelper.update(updatedSourceAsMap, indexRequest.sourceAsMap(), request.detectNoop());
        // cases where users repopulating multi-fields or adding synonyms, etc.
        if (request.detectNoop() && noop) {
            operation = "none";
        }
    } else {
        Map<String, Object> ctx = new HashMap<>(16);
        ctx.put("_index", getResult.getIndex());
        ctx.put("_type", getResult.getType());
        ctx.put("_id", getResult.getId());
        ctx.put("_version", getResult.getVersion());
        ctx.put("_routing", routing);
        ctx.put("_parent", parent);
        ctx.put("_source", sourceAndContent.v2());
        ctx.put("_now", nowInMillis.getAsLong());
        ctx = executeScript(request.script, ctx);
        operation = (String) ctx.get("op");
        updatedSourceAsMap = (Map<String, Object>) ctx.get("_source");
    }
    if (operation == null || "index".equals(operation)) {
        final IndexRequest indexRequest = Requests.indexRequest(request.index()).type(request.type()).id(request.id()).routing(routing).parent(parent).source(updatedSourceAsMap, updateSourceContentType).version(updateVersion).versionType(request.versionType()).waitForActiveShards(request.waitForActiveShards()).setRefreshPolicy(request.getRefreshPolicy());
        return new Result(indexRequest, DocWriteResponse.Result.UPDATED, updatedSourceAsMap, updateSourceContentType);
    } else if ("delete".equals(operation)) {
        DeleteRequest deleteRequest = Requests.deleteRequest(request.index()).type(request.type()).id(request.id()).routing(routing).parent(parent).version(updateVersion).versionType(request.versionType()).waitForActiveShards(request.waitForActiveShards()).setRefreshPolicy(request.getRefreshPolicy());
        return new Result(deleteRequest, DocWriteResponse.Result.DELETED, updatedSourceAsMap, updateSourceContentType);
    } else if ("none".equals(operation)) {
        UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
        update.setGetResult(extractGetResult(request, request.index(), getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef()));
        return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
    } else {
        logger.warn("Used update operation [{}] for script [{}], doing nothing...", operation, request.script.getIdOrCode());
        UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
        return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
    }
}
Also used : DocumentSourceMissingException(org.elasticsearch.index.engine.DocumentSourceMissingException) HashMap(java.util.HashMap) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetResult(org.elasticsearch.index.get.GetResult) XContentType(org.elasticsearch.common.xcontent.XContentType) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) HashMap(java.util.HashMap) Map(java.util.Map) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 18 with DeleteRequest

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

the class DocWriteRequest method readDocumentRequest.

/** read a document write (index/delete/update) request */
static DocWriteRequest readDocumentRequest(StreamInput in) throws IOException {
    byte type = in.readByte();
    DocWriteRequest docWriteRequest;
    if (type == 0) {
        IndexRequest indexRequest = new IndexRequest();
        indexRequest.readFrom(in);
        docWriteRequest = indexRequest;
    } else if (type == 1) {
        DeleteRequest deleteRequest = new DeleteRequest();
        deleteRequest.readFrom(in);
        docWriteRequest = deleteRequest;
    } else if (type == 2) {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.readFrom(in);
        docWriteRequest = updateRequest;
    } else {
        throw new IllegalStateException("invalid request type [" + type + " ]");
    }
    return docWriteRequest;
}
Also used : UpdateRequest(org.elasticsearch.action.update.UpdateRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 19 with DeleteRequest

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

the class RestDeleteAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    DeleteRequest deleteRequest = new DeleteRequest(request.param("index"), request.param("type"), request.param("id"));
    deleteRequest.routing(request.param("routing"));
    deleteRequest.parent(request.param("parent"));
    deleteRequest.timeout(request.paramAsTime("timeout", DeleteRequest.DEFAULT_TIMEOUT));
    deleteRequest.setRefreshPolicy(request.param("refresh"));
    deleteRequest.version(RestActions.parseVersion(request));
    deleteRequest.versionType(VersionType.fromString(request.param("version_type"), deleteRequest.versionType()));
    String waitForActiveShards = request.param("wait_for_active_shards");
    if (waitForActiveShards != null) {
        deleteRequest.waitForActiveShards(ActiveShardCount.parseString(waitForActiveShards));
    }
    return channel -> client.delete(deleteRequest, new RestStatusToXContentListener<>(channel));
}
Also used : ActiveShardCount(org.elasticsearch.action.support.ActiveShardCount) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) Settings(org.elasticsearch.common.settings.Settings) DELETE(org.elasticsearch.rest.RestRequest.Method.DELETE) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) RestStatusToXContentListener(org.elasticsearch.rest.action.RestStatusToXContentListener) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) VersionType(org.elasticsearch.index.VersionType) RestActions(org.elasticsearch.rest.action.RestActions) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 20 with DeleteRequest

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

the class DeleteDocumentationIT method testDelete.

/**
     * This test documents docs/java-rest/high-level/document/delete.asciidoc
     */
public void testDelete() throws IOException {
    RestHighLevelClient client = highLevelClient();
    // tag::delete-request[]
    DeleteRequest request = new DeleteRequest(// <1>
    "index", // <2>
    "type", // <3>
    "id");
    // end::delete-request[]
    // tag::delete-request-props[]
    // <1>
    request.timeout(TimeValue.timeValueSeconds(1));
    // <2>
    request.timeout("1s");
    // <3>
    request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
    // <4>
    request.setRefreshPolicy("wait_for");
    // <5>
    request.version(2);
    // <6>
    request.versionType(VersionType.EXTERNAL);
    // end::delete-request-props[]
    // tag::delete-execute[]
    DeleteResponse response = client.delete(request);
    try {
        // tag::delete-notfound[]
        if (response.getResult().equals(DocWriteResponse.Result.NOT_FOUND)) {
            // <1>
            throw new Exception("Can't find document to be removed");
        }
    // end::delete-notfound[]
    } catch (Exception ignored) {
    }
    // tag::delete-execute-async[]
    client.deleteAsync(request, new ActionListener<DeleteResponse>() {

        @Override
        public void onResponse(DeleteResponse deleteResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    });
    // tag::delete-conflict[]
    try {
        client.delete(request);
    } catch (ElasticsearchException exception) {
        if (exception.status().equals(RestStatus.CONFLICT)) {
        // <1>
        }
    }
// end::delete-conflict[]
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchException(org.elasticsearch.ElasticsearchException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) IOException(java.io.IOException)

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