Skip to content

Commit a03b6c2

Browse files
authored
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 2950210 commit a03b6c2

File tree

92 files changed

+432
-403
lines changed

Some content is hidden

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

92 files changed

+432
-403
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
@@ -98,15 +98,15 @@ public RestResponse buildResponse(GetStoredScriptResponse response, XContentBuil
9898
if (lang == null) {
9999
builder.startObject(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName());
100100
builder.field(StoredScriptSource.LANG_PARSE_FIELD.getPreferredName(), source.getLang());
101-
builder.field(StoredScriptSource.CODE_PARSE_FIELD.getPreferredName(), source.getCode());
101+
builder.field(StoredScriptSource.SOURCE_PARSE_FIELD.getPreferredName(), source.getSource());
102102

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

107107
builder.endObject();
108108
} else {
109-
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getCode());
109+
builder.field(StoredScriptSource.SCRIPT_PARSE_FIELD.getPreferredName(), source.getSource());
110110
}
111111
}
112112

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

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public final class Script implements ToXContentObject, Writeable {
113113
*/
114114
public static final ParseField SCRIPT_PARSE_FIELD = new ParseField("script");
115115

116+
/**
117+
* Standard {@link ParseField} for source on the inner level.
118+
*/
119+
public static final ParseField SOURCE_PARSE_FIELD = new ParseField("source");
120+
116121
/**
117122
* Standard {@link ParseField} for lang on the inner level.
118123
*/
@@ -218,9 +223,7 @@ private void setParams(Map<String, Object> params) {
218223
*/
219224
private Script build(String defaultLang) {
220225
if (type == null) {
221-
throw new IllegalArgumentException(
222-
"must specify either code for an [" + ScriptType.INLINE.getParseField().getPreferredName() + "] script " +
223-
"or an id for a [" + ScriptType.STORED.getParseField().getPreferredName() + "] script");
226+
throw new IllegalArgumentException("must specify either [source] for an inline script or [id] for a stored script");
224227
}
225228

226229
if (type == ScriptType.INLINE) {
@@ -299,7 +302,10 @@ public static Script parse(XContentParser parser) throws IOException {
299302
*
300303
* {@code
301304
* {
302-
* "<type (inline, stored, file)>" : "<idOrCode>",
305+
* // Exactly one of "id" or "source" must be specified
306+
* "id" : "<id>",
307+
* // OR
308+
* "source": "<source>",
303309
* "lang" : "<lang>",
304310
* "options" : {
305311
* "option0" : "<option0>",
@@ -317,7 +323,7 @@ public static Script parse(XContentParser parser) throws IOException {
317323
* Example:
318324
* {@code
319325
* {
320-
* "inline" : "return Math.log(doc.popularity) * params.multiplier",
326+
* "source" : "return Math.log(doc.popularity) * params.multiplier",
321327
* "lang" : "painless",
322328
* "params" : {
323329
* "multiplier" : 100.0
@@ -330,7 +336,7 @@ public static Script parse(XContentParser parser) throws IOException {
330336
*
331337
* {@code
332338
* {
333-
* "inline" : { "query" : ... },
339+
* "source" : { "query" : ... },
334340
* "lang" : "<lang>",
335341
* "options" : {
336342
* "option0" : "<option0>",
@@ -567,7 +573,7 @@ public void writeTo(StreamOutput out) throws IOException {
567573
*
568574
* {@code
569575
* {
570-
* "<type (inline, stored, file)>" : "<idOrCode>",
576+
* "<(id, source)>" : "<idOrCode>",
571577
* "lang" : "<lang>",
572578
* "options" : {
573579
* "option0" : "<option0>",
@@ -585,7 +591,7 @@ public void writeTo(StreamOutput out) throws IOException {
585591
* Example:
586592
* {@code
587593
* {
588-
* "inline" : "return Math.log(doc.popularity) * params.multiplier;",
594+
* "source" : "return Math.log(doc.popularity) * params.multiplier;",
589595
* "lang" : "painless",
590596
* "params" : {
591597
* "multiplier" : 100.0
@@ -600,7 +606,7 @@ public void writeTo(StreamOutput out) throws IOException {
600606
*
601607
* {@code
602608
* {
603-
* "inline" : { "query" : ... },
609+
* "source" : { "query" : ... },
604610
* "lang" : "<lang>",
605611
* "options" : {
606612
* "option0" : "<option0>",
@@ -621,10 +627,14 @@ public XContentBuilder toXContent(XContentBuilder builder, Params builderParams)
621627

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

624-
if (type == ScriptType.INLINE && contentType != null && builder.contentType().mediaType().equals(contentType)) {
625-
builder.rawField(type.getParseField().getPreferredName(), new BytesArray(idOrCode));
630+
if (type == ScriptType.INLINE) {
631+
if (contentType != null && builder.contentType().mediaType().equals(contentType)) {
632+
builder.rawField(SOURCE_PARSE_FIELD.getPreferredName(), new BytesArray(idOrCode));
633+
} else {
634+
builder.field(SOURCE_PARSE_FIELD.getPreferredName(), idOrCode);
635+
}
626636
} else {
627-
builder.field(type.getParseField().getPreferredName(), idOrCode);
637+
builder.field("id", idOrCode);
628638
}
629639

630640
if (lang != null) {

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryTyp
256256
// the script has been updated since the last compilation
257257
StoredScriptSource source = getScriptFromClusterState(id, lang);
258258
lang = source.getLang();
259-
idOrCode = source.getCode();
259+
idOrCode = source.getSource();
260260
options = source.getOptions();
261261
}
262262

@@ -429,7 +429,7 @@ public void putStoredScript(ClusterService clusterService, PutStoredScriptReques
429429
if (context == null) {
430430
throw new IllegalArgumentException("Unknown context [" + request.context() + "]");
431431
}
432-
scriptEngine.compile(request.id(), source.getCode(), context, Collections.emptyMap());
432+
scriptEngine.compile(request.id(), source.getSource(), context, Collections.emptyMap());
433433
}
434434
} catch (ScriptException good) {
435435
throw good;

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 overridden by the specific {@link ScriptEngine}
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 overridden by the specific {@link ScriptEngine}
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
* Reads an int from the input stream and converts it to a {@link ScriptType}.
@@ -101,7 +102,7 @@ public int getId() {
101102
* @return The unique name for this {@link ScriptType} based on the {@link ParseField}.
102103
*/
103104
public String getName() {
104-
return parseField.getPreferredName();
105+
return name().toLowerCase(Locale.ROOT);
105106
}
106107

107108
/**

0 commit comments

Comments
 (0)