Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the high-level REST client skip types for document deletion. #34041

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client;

import org.elasticsearch.action.support.ActiveShardCount;
import org.elasticsearch.client.security.RefreshPolicy;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.VersionType;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* A request to delete a document.
*/
public class DeleteRequest implements Validatable {

private final String index, id;

private String routing;
private long version = Versions.MATCH_ANY;
private VersionType versionType = VersionType.INTERNAL;
private TimeValue timeout;
private RefreshPolicy refreshPolicy = RefreshPolicy.NONE;
private ActiveShardCount waitForActiveShards = ActiveShardCount.DEFAULT;

/**
* Create a request to delete the given {@code id} in {@code index}.
*/
public DeleteRequest(String index, String id) {
this.index = Objects.requireNonNull(index);
this.id = Objects.requireNonNull(id);
}

@Override
public Optional<ValidationException> validate() {
List<String> validationErrors = new ArrayList<>();
if (versionType.validateVersionForWrites(version) == false) {
validationErrors.add("illegal version value [" + version + "] for version type [" + versionType.name() + "]");
}
if (versionType == VersionType.FORCE) {
validationErrors.add("version type [force] may no longer be used");
}
if (validationErrors.isEmpty()) {
return Optional.empty();
} else {
ValidationException ex = new ValidationException();
for (String error : validationErrors) {
ex.addValidationError(error);
}
return Optional.of(ex);
}
}

/**
* Return the name of the index that has the document to delete.
*/
public String getIndex() {
return index;
}

/**
* Return the id of the document to delete.
*/
public String getId() {
return id;
}

/**
* Set the routing value.
* @see #getRouting()
*/
public DeleteRequest setRouting(String routing) {
if (routing != null && routing.length() == 0) {
this.routing = null;
} else {
this.routing = routing;
}
return this;
}

/**
* Return the routing value, ie. the value whose hash is used to find ou
* which shard has the document to delete. {@code null} values mean that
* the {@link #getId()} should be used.
*/
public String getRouting() {
return this.routing;
}

/**
* Set the expected version of the document to delete. If the document
* has a different version then the delete request will fail.
*/
public DeleteRequest setVersion(long version) {
this.version = version;
return this;
}

/**
* Return the expected version of the document to delete.
*/
public long getVersion() {
return this.version;
}

/**
* Sets the type of versioning to use.
*/
public DeleteRequest setVersionType(VersionType versionType) {
this.versionType = Objects.requireNonNull(versionType);
return this;
}

/**
* Return the type of versioning to use.
*/
public VersionType getVersionType() {
return this.versionType;
}

/** Return the timeout of this delete request. */
public TimeValue getTimeout() {
return timeout;
}

/**
* Set the timeout of this delete request.
*/
public DeleteRequest setTimeout(TimeValue timeout) {
this.timeout = timeout;
return this;
}

/**
* Set the timeout of this delete request.
*/
public DeleteRequest setTimeout(String timeout) {
return setTimeout(TimeValue.parseTimeValue(timeout, null, getClass().getSimpleName() + ".timeout"));
}

/**
* Return the refresh policy for this delete request.
*/
public RefreshPolicy getRefreshPolicy() {
return refreshPolicy;
}

/**
* Set the refresh policy for this delete request and return {@code this}.
*/
public DeleteRequest setRefreshPolicy(RefreshPolicy refreshPolicy) {
this.refreshPolicy = refreshPolicy;
return this;
}

/**
* How many shards should be active.
* @see DeleteRequest#setWaitForActiveShards(ActiveShardCount)
*/
public ActiveShardCount getWaitForActiveShards() {
return waitForActiveShards;
}

/**
* Sets the number of shard copies that must be active before proceeding with the replication
* operation. Defaults to {@link ActiveShardCount#DEFAULT}, which requires one shard copy
* (the primary) to be active. Set this value to {@link ActiveShardCount#ALL} to
* wait for all shards (primary and all replicas) to be active. Otherwise, use
* {@link ActiveShardCount#from(int)} to set this value to any non-negative integer, up to the
* total number of shard copies (number of replicas + 1).
*/
public DeleteRequest setWaitForActiveShards(ActiveShardCount waitForActiveShards) {
this.waitForActiveShards = Objects.requireNonNull(waitForActiveShards);
return this;
}

@Override
public String toString() {
return "delete {[" + index + "][" + id + "]}";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.client;

import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
import org.elasticsearch.common.xcontent.ObjectParser.ValueType;
import org.elasticsearch.common.xcontent.XContentParseException;
import org.elasticsearch.index.seqno.SequenceNumbers;

import java.util.Objects;

import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;

/**
* An object representing a response to the deletion of a document.
* @see DeleteRequest
*/
public final class DeleteResponse {

static final ConstructingObjectParser<DeleteResponse, Void> PARSER = new ConstructingObjectParser<>("delete_response",
a -> {
String index = (String) a[0];
String id = (String) a[1];
Result result = (Result) a[2];
ShardInfo shardInfo = (ShardInfo) a[3];
long version = (Long) a[4];
long seqNo = a[5] == null ? SequenceNumbers.UNASSIGNED_SEQ_NO : (Long) a[5];
long primaryTerm = a[6] == null ? 0 : (Long) a[6];
boolean forcedRefresh = a[7] == null ? false : ((Boolean) a[7]);
return new DeleteResponse(index, id, result, shardInfo, version, seqNo, primaryTerm, forcedRefresh);
});

static {
PARSER.declareString(constructorArg(), new ParseField("_index"));
PARSER.declareString(constructorArg(), new ParseField("_id"));
PARSER.declareField(constructorArg(), (parser, c) -> {
switch (parser.text()) {
case "deleted":
return Result.DELETED;
case "not_found":
return Result.NOT_FOUND;
default:
throw new XContentParseException(parser.getTokenLocation(), "Unexpected _result value: [" + parser.text() + "]");
}
}, new ParseField("result"), ValueType.STRING);
PARSER.declareObject(constructorArg(), ShardInfo.PARSER, new ParseField(ShardInfo.SHARDS_FIELD));
PARSER.declareLong(constructorArg(), new ParseField("_version"));
PARSER.declareLong(optionalConstructorArg(), new ParseField("_seq_no"));
PARSER.declareLong(optionalConstructorArg(), new ParseField("_primary_term"));
PARSER.declareBoolean(optionalConstructorArg(), new ParseField("forced_refresh"));
}

private final String index, id;
private final Result result;
private final long version;
private final long primaryTerm;
private final long seqNo;
private final ShardInfo shardInfo;
private final boolean forcedRefresh;

private DeleteResponse(String index, String id, Result result, ShardInfo shardInfo,
long version, long seqNo, long primaryTerm, boolean forcedRefresh) {
this.index = Objects.requireNonNull(index, "_index may not be null");
this.id = Objects.requireNonNull(id, "_id may not be null");
this.result = Objects.requireNonNull(result, "_result may not be null");
this.shardInfo = Objects.requireNonNull(shardInfo, "_shards may not be null");
this.version = version;
this.seqNo = seqNo;
this.primaryTerm = primaryTerm;
this.forcedRefresh = forcedRefresh;
}

/**
* Return whether the document has been deleted or whether it already did not exist.
*/
public Result getResult() {
return result;
}

/**
* Return the index that the deleted document belonged to.
*/
public String getIndex() {
return index;
}

/**
* Return the id of the deleted document.
*/
public String getId() {
return this.id;
}

/**
* Returns the current version of the deleted document or {@link Versions#MATCH_ANY} if the document didn't exist.
*/
public long getVersion() {
return version;
}

/**
* Returns the sequence number assigned for this change. Returns {@link SequenceNumbers#UNASSIGNED_SEQ_NO}
* if the document didn't exist.
*/
public long getSeqNo() {
return seqNo;
}

/**
* Return the primary term of the deleted document, or {@code 0} if the document didn't exist.
*/
public long getPrimaryTerm() {
return primaryTerm;
}

/**
* Return information about how many shards processed the request and potential failures.
*/
public ShardInfo getShardInfo() {
return shardInfo;
}

/**
* Return whether a refresh has been performed.
*/
public boolean isForcedRefresh() {
return forcedRefresh;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.explain.ExplainRequest;
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesRequest;
import org.elasticsearch.action.get.GetRequest;
Expand Down Expand Up @@ -94,16 +93,17 @@ private RequestConverters() {
}

static Request delete(DeleteRequest deleteRequest) {
String endpoint = endpoint(deleteRequest.index(), deleteRequest.type(), deleteRequest.id());
String endpoint = endpoint(deleteRequest.getIndex(), "_doc", deleteRequest.getId());
Request request = new Request(HttpDelete.METHOD_NAME, endpoint);

Params parameters = new Params(request);
parameters.withRouting(deleteRequest.routing());
parameters.withTimeout(deleteRequest.timeout());
parameters.withVersion(deleteRequest.version());
parameters.withVersionType(deleteRequest.versionType());
parameters.withDisabledTypes();
parameters.withRouting(deleteRequest.getRouting());
parameters.withTimeout(deleteRequest.getTimeout());
parameters.withVersion(deleteRequest.getVersion());
parameters.withVersionType(deleteRequest.getVersionType());
parameters.withRefreshPolicy(deleteRequest.getRefreshPolicy());
parameters.withWaitForActiveShards(deleteRequest.waitForActiveShards());
parameters.withWaitForActiveShards(deleteRequest.getWaitForActiveShards());
return request;
}

Expand Down Expand Up @@ -923,6 +923,10 @@ Params withWaitForEvents(Priority waitForEvents) {
}
return this;
}

Params withDisabledTypes() {
return putParam("include_type_name", "false");
}
}

/**
Expand Down