Skip to content

Commit 0804744

Browse files
committed
Scripting: Change keys for inline/stored scripts to source/id (#25127)
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.
1 parent 3ba968c commit 0804744

File tree

78 files changed

+379
-326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+379
-326
lines changed

core/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/GetStoredScriptResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void writeTo(StreamOutput out) throws IOException {
8181
if (out.getVersion().onOrAfter(Version.V_5_3_0)) {
8282
source.writeTo(out);
8383
} else {
84-
out.writeString(source.getCode());
84+
out.writeString(source.getSource());
8585
}
8686
}
8787
}

core/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestGetStoredScriptAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuil
9393
if (lang == null) {
9494
builder.startObject(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName());
9595
builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), source.getLang());
96-
builder.field(StoredScriptSource.CODE_PARSE_FIELD.getPreferredName(), source.getCode());
96+
builder.field(StoredScriptSource.SOURCE_PARSE_FIELD.getPreferredName(), source.getSource());
9797

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

102102
builder.endObject();
103103
} else {
104-
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getCode());
104+
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getSource());
105105
}
106106
}
107107

core/src/main/java/org/elasticsearch/script/Script.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,8 @@ private void setParams(Map<String, Object> params) {
242242
*/
243243
private Script build(String defaultLang) {
244244
if (type == null) {
245-
throw new IllegalArgumentException(
246-
"must specify either code for an [" + ScriptType.INLINE.getParseField().getPreferredName() + "] script " +
247-
"or an id for a [" + ScriptType.STORED.getParseField().getPreferredName() + "] script " +
248-
"or [" + ScriptType.FILE.getParseField().getPreferredName() + "] script");
245+
throw new IllegalArgumentException("must specify either [source] for an inline script, [id] for a stored script, " +
246+
"or [" + ScriptType.FILE.getParseField().getPreferredName() + "] for a file script");
249247
}
250248

251249
if (type == ScriptType.INLINE) {
@@ -341,7 +339,10 @@ public static Script parse(XContentParser parser) throws IOException {
341339
*
342340
* {@code
343341
* {
344-
* "<type (inline, stored, file)>" : "<idOrCode>",
342+
* // Exactly one of "id" or "source" must be specified
343+
* "id" : "<id>",
344+
* // OR
345+
* "source": "<source>",
345346
* "lang" : "<lang>",
346347
* "options" : {
347348
* "option0" : "<option0>",
@@ -359,7 +360,7 @@ public static Script parse(XContentParser parser) throws IOException {
359360
* Example:
360361
* {@code
361362
* {
362-
* "inline" : "return Math.log(doc.popularity) * params.multiplier",
363+
* "source" : "return Math.log(doc.popularity) * params.multiplier",
363364
* "lang" : "painless",
364365
* "params" : {
365366
* "multiplier" : 100.0
@@ -372,7 +373,7 @@ public static Script parse(XContentParser parser) throws IOException {
372373
*
373374
* {@code
374375
* {
375-
* "inline" : { "query" : ... },
376+
* "source" : { "query" : ... },
376377
* "lang" : "<lang>",
377378
* "options" : {
378379
* "option0" : "<option0>",
@@ -618,7 +619,7 @@ public void writeTo(StreamOutput out) throws IOException {
618619
*
619620
* {@code
620621
* {
621-
* "<type (inline, stored, file)>" : "<idOrCode>",
622+
* "<(id, source)>" : "<idOrCode>",
622623
* "lang" : "<lang>",
623624
* "options" : {
624625
* "option0" : "<option0>",
@@ -636,7 +637,7 @@ public void writeTo(StreamOutput out) throws IOException {
636637
* Example:
637638
* {@code
638639
* {
639-
* "inline" : "return Math.log(doc.popularity) * params.multiplier;",
640+
* "source" : "return Math.log(doc.popularity) * params.multiplier;",
640641
* "lang" : "painless",
641642
* "params" : {
642643
* "multiplier" : 100.0
@@ -651,7 +652,7 @@ public void writeTo(StreamOutput out) throws IOException {
651652
*
652653
* {@code
653654
* {
654-
* "inline" : { "query" : ... },
655+
* "source" : { "query" : ... },
655656
* "lang" : "<lang>",
656657
* "options" : {
657658
* "option0" : "<option0>",
@@ -672,8 +673,12 @@ public XContentBuilder toXContent(XContentBuilder builder, Params builderParams)
672673

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

675-
if (type == ScriptType.INLINE && contentType != null && builder.contentType().mediaType().equals(contentType)) {
676-
builder.rawField(type.getParseField().getPreferredName(), new BytesArray(idOrCode));
676+
if (type == ScriptType.INLINE) {
677+
if (contentType != null && builder.contentType().mediaType().equals(contentType)) {
678+
builder.rawField(ScriptType.INLINE.getParseField().getPreferredName(), new BytesArray(idOrCode));
679+
} else {
680+
builder.field(ScriptType.INLINE.getParseField().getPreferredName(), idOrCode);
681+
}
677682
} else {
678683
builder.field(type.getParseField().getPreferredName(), idOrCode);
679684
}

core/src/main/java/org/elasticsearch/script/ScriptMetaData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public ScriptMetaData(StreamInput in) throws IOException {
336336
throw new IllegalArgumentException("illegal stored script id [" + id + "], does not contain lang");
337337
} else {
338338
source = new StoredScriptSource(in);
339-
source = new StoredScriptSource(id.substring(0, split), source.getCode(), Collections.emptyMap());
339+
source = new StoredScriptSource(id.substring(0, split), source.getSource(), Collections.emptyMap());
340340
}
341341
// Version 5.3+ can just be parsed normally using StoredScriptSource.
342342
} else {

core/src/main/java/org/elasticsearch/script/ScriptService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public CompiledScript compile(Script script, ScriptContext scriptContext) {
245245
// the script has been updated since the last compilation
246246
StoredScriptSource source = getScriptFromClusterState(id, lang);
247247
lang = source.getLang();
248-
idOrCode = source.getCode();
248+
idOrCode = source.getSource();
249249
options = source.getOptions();
250250
}
251251

@@ -407,11 +407,11 @@ public void putStoredScript(ClusterService clusterService, PutStoredScriptReques
407407
ScriptEngineService scriptEngineService = getScriptEngineServiceForLang(source.getLang());
408408

409409
if (isAnyScriptContextEnabled(source.getLang(), ScriptType.STORED)) {
410-
Object compiled = scriptEngineService.compile(request.id(), source.getCode(), Collections.emptyMap());
410+
Object compiled = scriptEngineService.compile(request.id(), source.getSource(), Collections.emptyMap());
411411

412412
if (compiled == null) {
413413
throw new IllegalArgumentException("failed to parse/compile stored script [" + request.id() + "]" +
414-
(source.getCode() == null ? "" : " using code [" + source.getCode() + "]"));
414+
(source.getSource() == null ? "" : " using code [" + source.getSource() + "]"));
415415
}
416416
} else {
417417
throw new IllegalArgumentException(

core/src/main/java/org/elasticsearch/script/ScriptType.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.common.io.stream.Writeable;
2626

2727
import java.io.IOException;
28+
import java.util.Locale;
2829

2930
/**
3031
* ScriptType represents the way a script is stored and retrieved from the {@link ScriptService}.
@@ -40,7 +41,7 @@ public enum ScriptType implements Writeable {
4041
* (Groovy and others), but can be overriden by the specific {@link ScriptEngineService}
4142
* if the language is naturally secure (Painless, Mustache, and Expressions).
4243
*/
43-
INLINE ( 0 , new ParseField("inline") , false ),
44+
INLINE ( 0 , new ParseField("source", "inline") , false ),
4445

4546
/**
4647
* STORED scripts are saved as part of the {@link org.elasticsearch.cluster.ClusterState}
@@ -49,7 +50,7 @@ public enum ScriptType implements Writeable {
4950
* (Groovy and others), but can be overriden by the specific {@link ScriptEngineService}
5051
* if the language is naturally secure (Painless, Mustache, and Expressions).
5152
*/
52-
STORED ( 1 , new ParseField("stored", "id") , false ),
53+
STORED ( 1 , new ParseField("id", "stored") , false ),
5354

5455
/**
5556
* FILE scripts are loaded from disk either on start-up or on-the-fly depending on
@@ -111,7 +112,7 @@ public int getId() {
111112
* @return The unique name for this {@link ScriptType} based on the {@link ParseField}.
112113
*/
113114
public String getName() {
114-
return parseField.getPreferredName();
115+
return name().toLowerCase(Locale.ROOT);
115116
}
116117

117118
/**

0 commit comments

Comments
 (0)