Skip to content

Commit

Permalink
Scripting: Change keys for inline/stored scripts to source/id (#25127)
Browse files Browse the repository at this point in the history
This commit adds back "id" as the key within a script to specify a
stored script (which with file scripts now gone is no longer ambiguous).
It also adds "source" as a replacement for "code". This is in an attempt
to normalize how scripts are specified across both put stored scripts and script usages, including search template requests. This also deprecates the old inline/stored keys.
  • Loading branch information
rjernst authored Jun 9, 2017
1 parent 2950210 commit a03b6c2
Show file tree
Hide file tree
Showing 92 changed files with 432 additions and 403 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
source.writeTo(out);
} else {
out.writeString(source.getCode());
out.writeString(source.getSource());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuil
if (lang == null) {
builder.startObject(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName());
builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), source.getLang());
builder.field(StoredScriptSource.CODE_PARSE_FIELD.getPreferredName(), source.getCode());
builder.field(StoredScriptSource.SOURCE_PARSE_FIELD.getPreferredName(), source.getSource());

if (source.getOptions().isEmpty() == false) {
builder.field(StoredScriptSource.OPTIONS_PARSE_FIELD.getPreferredName(), source.getOptions());
}

builder.endObject();
} else {
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getCode());
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getSource());
}
}

Expand Down
34 changes: 22 additions & 12 deletions core/src/main/java/org/elasticsearch/script/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public final class Script implements ToXContentObject, Writeable {
*/
public static final ParseField SCRIPT_PARSE_FIELD = new ParseField("script");

/**
* Standard {@link ParseField} for source on the inner level.
*/
public static final ParseField SOURCE_PARSE_FIELD = new ParseField("source");

/**
* Standard {@link ParseField} for lang on the inner level.
*/
Expand Down Expand Up @@ -218,9 +223,7 @@ private void setParams(Map<String, Object> params) {
*/
private Script build(String defaultLang) {
if (type == null) {
throw new IllegalArgumentException(
"must specify either code for an [" + ScriptType.INLINE.getParseField().getPreferredName() + "] script " +
"or an id for a [" + ScriptType.STORED.getParseField().getPreferredName() + "] script");
throw new IllegalArgumentException("must specify either [source] for an inline script or [id] for a stored script");
}

if (type == ScriptType.INLINE) {
Expand Down Expand Up @@ -299,7 +302,10 @@ public static Script parse(XContentParser parser) throws IOException {
*
* {@code
* {
* "<type (inline, stored, file)>" : "<idOrCode>",
* // Exactly one of "id" or "source" must be specified
* "id" : "<id>",
* // OR
* "source": "<source>",
* "lang" : "<lang>",
* "options" : {
* "option0" : "<option0>",
Expand All @@ -317,7 +323,7 @@ public static Script parse(XContentParser parser) throws IOException {
* Example:
* {@code
* {
* "inline" : "return Math.log(doc.popularity) * params.multiplier",
* "source" : "return Math.log(doc.popularity) * params.multiplier",
* "lang" : "painless",
* "params" : {
* "multiplier" : 100.0
Expand All @@ -330,7 +336,7 @@ public static Script parse(XContentParser parser) throws IOException {
*
* {@code
* {
* "inline" : { "query" : ... },
* "source" : { "query" : ... },
* "lang" : "<lang>",
* "options" : {
* "option0" : "<option0>",
Expand Down Expand Up @@ -567,7 +573,7 @@ public void writeTo(StreamOutput out) throws IOException {
*
* {@code
* {
* "<type (inline, stored, file)>" : "<idOrCode>",
* "<(id, source)>" : "<idOrCode>",
* "lang" : "<lang>",
* "options" : {
* "option0" : "<option0>",
Expand All @@ -585,7 +591,7 @@ public void writeTo(StreamOutput out) throws IOException {
* Example:
* {@code
* {
* "inline" : "return Math.log(doc.popularity) * params.multiplier;",
* "source" : "return Math.log(doc.popularity) * params.multiplier;",
* "lang" : "painless",
* "params" : {
* "multiplier" : 100.0
Expand All @@ -600,7 +606,7 @@ public void writeTo(StreamOutput out) throws IOException {
*
* {@code
* {
* "inline" : { "query" : ... },
* "source" : { "query" : ... },
* "lang" : "<lang>",
* "options" : {
* "option0" : "<option0>",
Expand All @@ -621,10 +627,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params builderParams)

String contentType = options == null ? null : options.get(CONTENT_TYPE_OPTION);

if (type == ScriptType.INLINE && contentType != null && builder.contentType().mediaType().equals(contentType)) {
builder.rawField(type.getParseField().getPreferredName(), new BytesArray(idOrCode));
if (type == ScriptType.INLINE) {
if (contentType != null && builder.contentType().mediaType().equals(contentType)) {
builder.rawField(SOURCE_PARSE_FIELD.getPreferredName(), new BytesArray(idOrCode));
} else {
builder.field(SOURCE_PARSE_FIELD.getPreferredName(), idOrCode);
}
} else {
builder.field(type.getParseField().getPreferredName(), idOrCode);
builder.field("id", idOrCode);
}

if (lang != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public ScriptMetaData(StreamInput in) throws IOException {
throw new IllegalArgumentException("illegal stored script id [" + id + "], does not contain lang");
} else {
source = new StoredScriptSource(in);
source = new StoredScriptSource(id.substring(0, split), source.getCode(), Collections.emptyMap());
source = new StoredScriptSource(id.substring(0, split), source.getSource(), Collections.emptyMap());
}
// Version 5.3+ can just be parsed normally using StoredScriptSource.
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryTyp
// the script has been updated since the last compilation
StoredScriptSource source = getScriptFromClusterState(id, lang);
lang = source.getLang();
idOrCode = source.getCode();
idOrCode = source.getSource();
options = source.getOptions();
}

Expand Down Expand Up @@ -429,7 +429,7 @@ public void putStoredScript(ClusterService clusterService, PutStoredScriptReques
if (context == null) {
throw new IllegalArgumentException("Unknown context [" + request.context() + "]");
}
scriptEngine.compile(request.id(), source.getCode(), context, Collections.emptyMap());
scriptEngine.compile(request.id(), source.getSource(), context, Collections.emptyMap());
}
} catch (ScriptException good) {
throw good;
Expand Down
7 changes: 4 additions & 3 deletions core/src/main/java/org/elasticsearch/script/ScriptType.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.elasticsearch.common.io.stream.Writeable;

import java.io.IOException;
import java.util.Locale;

/**
* ScriptType represents the way a script is stored and retrieved from the {@link ScriptService}.
Expand All @@ -40,7 +41,7 @@ public enum ScriptType implements Writeable {
* (Groovy and others), but can be overridden by the specific {@link ScriptEngine}
* if the language is naturally secure (Painless, Mustache, and Expressions).
*/
INLINE ( 0 , new ParseField("inline") , false ),
INLINE ( 0 , new ParseField("source", "inline") , false ),

/**
* STORED scripts are saved as part of the {@link org.elasticsearch.cluster.ClusterState}
Expand All @@ -49,7 +50,7 @@ public enum ScriptType implements Writeable {
* (Groovy and others), but can be overridden by the specific {@link ScriptEngine}
* if the language is naturally secure (Painless, Mustache, and Expressions).
*/
STORED ( 1 , new ParseField("stored", "id") , false );
STORED ( 1 , new ParseField("id", "stored") , false );

/**
* Reads an int from the input stream and converts it to a {@link ScriptType}.
Expand Down Expand Up @@ -101,7 +102,7 @@ public int getId() {
* @return The unique name for this {@link ScriptType} based on the {@link ParseField}.
*/
public String getName() {
return parseField.getPreferredName();
return name().toLowerCase(Locale.ROOT);
}

/**
Expand Down
Loading

0 comments on commit a03b6c2

Please sign in to comment.