|
@@ -19,8 +19,6 @@
|
|
|
|
|
|
package org.elasticsearch.action.update;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
-
|
|
|
import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.action.ActionRequestValidationException;
|
|
|
import org.elasticsearch.action.DocWriteRequest;
|
|
@@ -30,11 +28,14 @@ import org.elasticsearch.action.support.WriteRequest;
|
|
|
import org.elasticsearch.action.support.replication.ReplicationRequest;
|
|
|
import org.elasticsearch.action.support.single.instance.InstanceShardOperationRequest;
|
|
|
import org.elasticsearch.common.Nullable;
|
|
|
+import org.elasticsearch.common.ParseField;
|
|
|
+import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
import org.elasticsearch.common.lucene.uid.Versions;
|
|
|
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
|
|
|
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
|
|
+import org.elasticsearch.common.xcontent.ObjectParser;
|
|
|
import org.elasticsearch.common.xcontent.ToXContentObject;
|
|
|
import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
@@ -48,15 +49,46 @@ import org.elasticsearch.script.ScriptType;
|
|
|
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import static org.elasticsearch.action.ValidateActions.addValidationError;
|
|
|
|
|
|
public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
implements DocWriteRequest<UpdateRequest>, WriteRequest<UpdateRequest>, ToXContentObject {
|
|
|
+ private static ObjectParser<UpdateRequest, Void> PARSER;
|
|
|
+
|
|
|
+ private static final ParseField SCRIPT_FIELD = new ParseField("script");
|
|
|
+ private static final ParseField SCRIPTED_UPSERT_FIELD = new ParseField("scripted_upsert");
|
|
|
+ private static final ParseField UPSERT_FIELD = new ParseField("upsert");
|
|
|
+ private static final ParseField DOC_FIELD = new ParseField("doc");
|
|
|
+ private static final ParseField DOC_AS_UPSERT_FIELD = new ParseField("doc_as_upsert");
|
|
|
+ private static final ParseField DETECT_NOOP_FIELD = new ParseField("detect_noop");
|
|
|
+ private static final ParseField SOURCE_FIELD = new ParseField("_source");
|
|
|
+
|
|
|
+ static {
|
|
|
+ PARSER = new ObjectParser<>(UpdateRequest.class.getSimpleName());
|
|
|
+ PARSER.declareField((request, script) -> request.script = script,
|
|
|
+ (parser, context) -> Script.parse(parser), SCRIPT_FIELD, ObjectParser.ValueType.OBJECT_OR_STRING);
|
|
|
+ PARSER.declareBoolean(UpdateRequest::scriptedUpsert, SCRIPTED_UPSERT_FIELD);
|
|
|
+ PARSER.declareObject((request, builder) -> request.safeUpsertRequest().source(builder),
|
|
|
+ (parser, context) -> {
|
|
|
+ XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType());
|
|
|
+ builder.copyCurrentStructure(parser);
|
|
|
+ return builder;
|
|
|
+ }, UPSERT_FIELD);
|
|
|
+ PARSER.declareObject((request, builder) -> request.safeDoc().source(builder),
|
|
|
+ (parser, context) -> {
|
|
|
+ XContentBuilder docBuilder = XContentFactory.contentBuilder(parser.contentType());
|
|
|
+ docBuilder.copyCurrentStructure(parser);
|
|
|
+ return docBuilder;
|
|
|
+ }, DOC_FIELD);
|
|
|
+ PARSER.declareBoolean(UpdateRequest::docAsUpsert, DOC_AS_UPSERT_FIELD);
|
|
|
+ PARSER.declareBoolean(UpdateRequest::detectNoop, DETECT_NOOP_FIELD);
|
|
|
+ PARSER.declareField(UpdateRequest::fetchSource,
|
|
|
+ (parser, context) -> FetchSourceContext.fromXContent(parser), SOURCE_FIELD,
|
|
|
+ ObjectParser.ValueType.OBJECT_ARRAY_BOOLEAN_OR_STRING);
|
|
|
+ }
|
|
|
|
|
|
private String type;
|
|
|
private String id;
|
|
@@ -66,7 +98,6 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
@Nullable
|
|
|
Script script;
|
|
|
|
|
|
- private String[] fields;
|
|
|
private FetchSourceContext fetchSourceContext;
|
|
|
|
|
|
private long version = Versions.MATCH_ANY;
|
|
@@ -365,16 +396,6 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * Explicitly specify the fields that will be returned. By default, nothing is returned.
|
|
|
- * @deprecated Use {@link UpdateRequest#fetchSource(String[], String[])} instead
|
|
|
- */
|
|
|
- @Deprecated
|
|
|
- public UpdateRequest fields(String... fields) {
|
|
|
- this.fields = fields;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Indicate that _source should be returned with every hit, with an
|
|
|
* "include" and/or "exclude" set which can include simple wildcard
|
|
@@ -389,7 +410,9 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
*/
|
|
|
public UpdateRequest fetchSource(@Nullable String include, @Nullable String exclude) {
|
|
|
FetchSourceContext context = this.fetchSourceContext == null ? FetchSourceContext.FETCH_SOURCE : this.fetchSourceContext;
|
|
|
- this.fetchSourceContext = new FetchSourceContext(context.fetchSource(), new String[] {include}, new String[]{exclude});
|
|
|
+ String[] includes = include == null ? Strings.EMPTY_ARRAY : new String[]{include};
|
|
|
+ String[] excludes = exclude == null ? Strings.EMPTY_ARRAY : new String[]{exclude};
|
|
|
+ this.fetchSourceContext = new FetchSourceContext(context.fetchSource(), includes, excludes);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
@@ -428,16 +451,6 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- /**
|
|
|
- * Get the fields to be returned.
|
|
|
- * @deprecated Use {@link UpdateRequest#fetchSource()} instead
|
|
|
- */
|
|
|
- @Deprecated
|
|
|
- public String[] fields() {
|
|
|
- return fields;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Gets the {@link FetchSourceContext} which defines how the _source should
|
|
|
* be fetched.
|
|
@@ -707,49 +720,7 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
}
|
|
|
|
|
|
public UpdateRequest fromXContent(XContentParser parser) throws IOException {
|
|
|
- Script script = null;
|
|
|
- XContentParser.Token token = parser.nextToken();
|
|
|
- if (token == null) {
|
|
|
- return this;
|
|
|
- }
|
|
|
- String currentFieldName = null;
|
|
|
- while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
|
|
- if (token == XContentParser.Token.FIELD_NAME) {
|
|
|
- currentFieldName = parser.currentName();
|
|
|
- } else if ("script".equals(currentFieldName)) {
|
|
|
- script = Script.parse(parser);
|
|
|
- } else if ("scripted_upsert".equals(currentFieldName)) {
|
|
|
- scriptedUpsert = parser.booleanValue();
|
|
|
- } else if ("upsert".equals(currentFieldName)) {
|
|
|
- XContentBuilder builder = XContentFactory.contentBuilder(parser.contentType());
|
|
|
- builder.copyCurrentStructure(parser);
|
|
|
- safeUpsertRequest().source(builder);
|
|
|
- } else if ("doc".equals(currentFieldName)) {
|
|
|
- XContentBuilder docBuilder = XContentFactory.contentBuilder(parser.contentType());
|
|
|
- docBuilder.copyCurrentStructure(parser);
|
|
|
- safeDoc().source(docBuilder);
|
|
|
- } else if ("doc_as_upsert".equals(currentFieldName)) {
|
|
|
- docAsUpsert(parser.booleanValue());
|
|
|
- } else if ("detect_noop".equals(currentFieldName)) {
|
|
|
- detectNoop(parser.booleanValue());
|
|
|
- } else if ("fields".equals(currentFieldName)) {
|
|
|
- List<Object> fields = null;
|
|
|
- if (token == XContentParser.Token.START_ARRAY) {
|
|
|
- fields = (List) parser.list();
|
|
|
- } else if (token.isValue()) {
|
|
|
- fields = Collections.singletonList(parser.text());
|
|
|
- }
|
|
|
- if (fields != null) {
|
|
|
- fields(fields.toArray(new String[fields.size()]));
|
|
|
- }
|
|
|
- } else if ("_source".equals(currentFieldName)) {
|
|
|
- fetchSourceContext = FetchSourceContext.fromXContent(parser);
|
|
|
- }
|
|
|
- }
|
|
|
- if (script != null) {
|
|
|
- this.script = script;
|
|
|
- }
|
|
|
- return this;
|
|
|
+ return PARSER.parse(parser, this, null);
|
|
|
}
|
|
|
|
|
|
public boolean docAsUpsert() {
|
|
@@ -789,7 +760,12 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
doc = new IndexRequest();
|
|
|
doc.readFrom(in);
|
|
|
}
|
|
|
- fields = in.readOptionalStringArray();
|
|
|
+ if (in.getVersion().before(Version.V_7_0_0_alpha1)) {
|
|
|
+ String[] fields = in.readOptionalStringArray();
|
|
|
+ if (fields != null) {
|
|
|
+ throw new IllegalArgumentException("[fields] is no longer supported");
|
|
|
+ }
|
|
|
+ }
|
|
|
fetchSourceContext = in.readOptionalWriteable(FetchSourceContext::new);
|
|
|
if (in.readBoolean()) {
|
|
|
upsertRequest = new IndexRequest();
|
|
@@ -812,7 +788,7 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
|
|
|
out.writeOptionalString(null); // _parent
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
boolean hasScript = script != null;
|
|
|
out.writeBoolean(hasScript);
|
|
|
if (hasScript) {
|
|
@@ -830,7 +806,9 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
doc.id(id);
|
|
|
doc.writeTo(out);
|
|
|
}
|
|
|
- out.writeOptionalStringArray(fields);
|
|
|
+ if (out.getVersion().before(Version.V_7_0_0_alpha1)) {
|
|
|
+ out.writeOptionalStringArray(null);
|
|
|
+ }
|
|
|
out.writeOptionalWriteable(fetchSourceContext);
|
|
|
if (upsertRequest == null) {
|
|
|
out.writeBoolean(false);
|
|
@@ -880,9 +858,6 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
if (detectNoop == false) {
|
|
|
builder.field("detect_noop", detectNoop);
|
|
|
}
|
|
|
- if (fields != null) {
|
|
|
- builder.array("fields", fields);
|
|
|
- }
|
|
|
if (fetchSourceContext != null) {
|
|
|
builder.field("_source", fetchSourceContext);
|
|
|
}
|
|
@@ -908,9 +883,6 @@ public class UpdateRequest extends InstanceShardOperationRequest<UpdateRequest>
|
|
|
}
|
|
|
res.append(", scripted_upsert[").append(scriptedUpsert).append("]");
|
|
|
res.append(", detect_noop[").append(detectNoop).append("]");
|
|
|
- if (fields != null) {
|
|
|
- res.append(", fields[").append(Arrays.toString(fields)).append("]");
|
|
|
- }
|
|
|
return res.append("}").toString();
|
|
|
}
|
|
|
}
|