Skip to content

Commit c7ccde3

Browse files
committed
Backport of clean up of Script.
Relates #21321
1 parent 25af595 commit c7ccde3

File tree

112 files changed

+1272
-783
lines changed

Some content is hidden

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

112 files changed

+1272
-783
lines changed

core/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ BulkRequest internalAdd(UpdateRequest request, @Nullable Object payload) {
168168
sizeInBytes += request.upsertRequest().source().length();
169169
}
170170
if (request.script() != null) {
171-
sizeInBytes += request.script().getScript().length() * 2;
171+
sizeInBytes += request.script().getIdOrCode().length() * 2;
172172
}
173173
return this;
174174
}

core/src/main/java/org/elasticsearch/action/update/UpdateHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected Result prepare(ShardId shardId, UpdateRequest request, final GetResult
116116
if (!"create".equals(scriptOpChoice)) {
117117
if (!"none".equals(scriptOpChoice)) {
118118
logger.warn("Used upsert operation [{}] for script [{}], doing nothing...", scriptOpChoice,
119-
request.script.getScript());
119+
request.script.getIdOrCode());
120120
}
121121
UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(),
122122
getResult.getVersion(), DocWriteResponse.Result.NOOP);
@@ -242,7 +242,7 @@ protected Result prepare(ShardId shardId, UpdateRequest request, final GetResult
242242
update.setGetResult(extractGetResult(request, request.index(), getResult.getVersion(), updatedSourceAsMap, updateSourceContentType, getResult.internalSourceRef()));
243243
return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
244244
} else {
245-
logger.warn("Used update operation [{}] for script [{}], doing nothing...", operation, request.script.getScript());
245+
logger.warn("Used update operation [{}] for script [{}], doing nothing...", operation, request.script.getIdOrCode());
246246
UpdateResponse update = new UpdateResponse(shardId, getResult.getType(), getResult.getId(), getResult.getVersion(), DocWriteResponse.Result.NOOP);
247247
return new Result(update, DocWriteResponse.Result.NOOP, updatedSourceAsMap, updateSourceContentType);
248248
}
@@ -251,7 +251,7 @@ protected Result prepare(ShardId shardId, UpdateRequest request, final GetResult
251251
private Map<String, Object> executeScript(Script script, Map<String, Object> ctx) {
252252
try {
253253
if (scriptService != null) {
254-
ExecutableScript executableScript = scriptService.executable(script, ScriptContext.Standard.UPDATE, Collections.emptyMap());
254+
ExecutableScript executableScript = scriptService.executable(script, ScriptContext.Standard.UPDATE);
255255
executableScript.setNextVar("ctx", ctx);
256256
executableScript.run();
257257
// we need to unwrap the ctx...

core/src/main/java/org/elasticsearch/action/update/UpdateRequest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public UpdateRequest script(Script script) {
229229
*/
230230
@Deprecated
231231
public String scriptString() {
232-
return this.script == null ? null : this.script.getScript();
232+
return this.script == null ? null : this.script.getIdOrCode();
233233
}
234234

235235
/**
@@ -332,13 +332,13 @@ public UpdateRequest scriptParams(Map<String, Object> scriptParams) {
332332
private void updateOrCreateScript(String scriptContent, ScriptType type, String lang, Map<String, Object> params) {
333333
Script script = script();
334334
if (script == null) {
335-
script = new Script(scriptContent == null ? "" : scriptContent, type == null ? ScriptType.INLINE : type, lang, params);
335+
script = new Script(type == null ? ScriptType.INLINE : type, lang, scriptContent == null ? "" : scriptContent, params);
336336
} else {
337-
String newScriptContent = scriptContent == null ? script.getScript() : scriptContent;
337+
String newScriptContent = scriptContent == null ? script.getIdOrCode() : scriptContent;
338338
ScriptType newScriptType = type == null ? script.getType() : type;
339339
String newScriptLang = lang == null ? script.getLang() : lang;
340340
Map<String, Object> newScriptParams = params == null ? script.getParams() : params;
341-
script = new Script(newScriptContent, newScriptType, newScriptLang, newScriptParams);
341+
script = new Script(newScriptType, newScriptLang, newScriptContent, newScriptParams);
342342
}
343343
script(script);
344344
}
@@ -352,7 +352,7 @@ private void updateOrCreateScript(String scriptContent, ScriptType type, String
352352
*/
353353
@Deprecated
354354
public UpdateRequest script(String script, ScriptType scriptType, @Nullable Map<String, Object> scriptParams) {
355-
this.script = new Script(script, scriptType, null, scriptParams);
355+
this.script = new Script(scriptType, Script.DEFAULT_SCRIPT_LANG, script, scriptParams);
356356
return this;
357357
}
358358

@@ -375,7 +375,7 @@ public UpdateRequest script(String script, ScriptType scriptType, @Nullable Map<
375375
@Deprecated
376376
public UpdateRequest script(String script, @Nullable String scriptLang, ScriptType scriptType,
377377
@Nullable Map<String, Object> scriptParams) {
378-
this.script = new Script(script, scriptType, scriptLang, scriptParams);
378+
this.script = new Script(scriptType, scriptLang, script, scriptParams);
379379
return this;
380380
}
381381

core/src/main/java/org/elasticsearch/common/xcontent/ConstructingObjectParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private void queue(Consumer<Value> queueMe) {
297297
}
298298

299299
/**
300-
* Finish parsing the object.
300+
* Finish parsing the object.
301301
*/
302302
private Value finish() {
303303
if (targetObject != null) {

core/src/main/java/org/elasticsearch/common/xcontent/XContentParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ enum NumberType {
131131

132132
Map<String, Object> mapOrdered() throws IOException;
133133

134+
Map<String, String> mapStrings() throws IOException;
135+
136+
Map<String, String> mapStringsOrdered() throws IOException;
137+
134138
List<Object> list() throws IOException;
135139

136140
List<Object> listOrderedMap() throws IOException;

core/src/main/java/org/elasticsearch/common/xcontent/XContentType.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.common.io.stream.StreamInput;
2323
import org.elasticsearch.common.io.stream.StreamOutput;
24+
import org.elasticsearch.common.io.stream.Writeable;
2425
import org.elasticsearch.common.xcontent.cbor.CborXContent;
2526
import org.elasticsearch.common.xcontent.json.JsonXContent;
2627
import org.elasticsearch.common.xcontent.smile.SmileXContent;
@@ -32,7 +33,7 @@
3233
/**
3334
* The content type of {@link org.elasticsearch.common.xcontent.XContent}.
3435
*/
35-
public enum XContentType {
36+
public enum XContentType implements Writeable {
3637

3738
/**
3839
* A JSON based content type.
@@ -168,7 +169,8 @@ public static XContentType readFrom(StreamInput in) throws IOException {
168169
throw new IllegalStateException("Unknown XContentType with index [" + index + "]");
169170
}
170171

171-
public static void writeTo(XContentType contentType, StreamOutput out) throws IOException {
172-
out.writeVInt(contentType.index);
172+
@Override
173+
public void writeTo(StreamOutput out) throws IOException {
174+
out.writeVInt(index);
173175
}
174176
}

core/src/main/java/org/elasticsearch/common/xcontent/support/AbstractXContentParser.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ public Map<String, Object> mapOrdered() throws IOException {
218218
return readOrderedMap(this);
219219
}
220220

221+
@Override
222+
public Map<String, String> mapStrings() throws IOException {
223+
return readMapStrings(this);
224+
}
225+
226+
@Override
227+
public Map<String, String> mapStringsOrdered() throws IOException {
228+
return readOrderedMapStrings(this);
229+
}
230+
221231
@Override
222232
public List<Object> list() throws IOException {
223233
return readList(this);
@@ -232,10 +242,18 @@ interface MapFactory {
232242
Map<String, Object> newMap();
233243
}
234244

245+
interface MapStringsFactory {
246+
Map<String, String> newMap();
247+
}
248+
235249
static final MapFactory SIMPLE_MAP_FACTORY = HashMap::new;
236250

237251
static final MapFactory ORDERED_MAP_FACTORY = LinkedHashMap::new;
238252

253+
static final MapStringsFactory SIMPLE_MAP_STRINGS_FACTORY = HashMap::new;
254+
255+
static final MapStringsFactory ORDERED_MAP_STRINGS_FACTORY = LinkedHashMap::new;
256+
239257
static Map<String, Object> readMap(XContentParser parser) throws IOException {
240258
return readMap(parser, SIMPLE_MAP_FACTORY);
241259
}
@@ -244,6 +262,14 @@ static Map<String, Object> readOrderedMap(XContentParser parser) throws IOExcept
244262
return readMap(parser, ORDERED_MAP_FACTORY);
245263
}
246264

265+
static Map<String, String> readMapStrings(XContentParser parser) throws IOException {
266+
return readMapStrings(parser, SIMPLE_MAP_STRINGS_FACTORY);
267+
}
268+
269+
static Map<String, String> readOrderedMapStrings(XContentParser parser) throws IOException {
270+
return readMapStrings(parser, ORDERED_MAP_STRINGS_FACTORY);
271+
}
272+
247273
static List<Object> readList(XContentParser parser) throws IOException {
248274
return readList(parser, SIMPLE_MAP_FACTORY);
249275
}
@@ -272,6 +298,26 @@ static Map<String, Object> readMap(XContentParser parser, MapFactory mapFactory)
272298
return map;
273299
}
274300

301+
static Map<String, String> readMapStrings(XContentParser parser, MapStringsFactory mapStringsFactory) throws IOException {
302+
Map<String, String> map = mapStringsFactory.newMap();
303+
XContentParser.Token token = parser.currentToken();
304+
if (token == null) {
305+
token = parser.nextToken();
306+
}
307+
if (token == XContentParser.Token.START_OBJECT) {
308+
token = parser.nextToken();
309+
}
310+
for (; token == XContentParser.Token.FIELD_NAME; token = parser.nextToken()) {
311+
// Must point to field name
312+
String fieldName = parser.currentName();
313+
// And then the value...
314+
parser.nextToken();
315+
String value = parser.text();
316+
map.put(fieldName, value);
317+
}
318+
return map;
319+
}
320+
275321
static List<Object> readList(XContentParser parser, MapFactory mapFactory) throws IOException {
276322
XContentParser.Token token = parser.currentToken();
277323
if (token == null) {

core/src/main/java/org/elasticsearch/index/query/InnerHitBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ private void setupInnerHitsContext(QueryShardContext context, InnerHitsContext.B
576576
if (scriptFields != null) {
577577
for (ScriptField field : scriptFields) {
578578
SearchScript searchScript = innerHitsContext.getQueryShardContext().getSearchScript(field.script(),
579-
ScriptContext.Standard.SEARCH, Collections.emptyMap());
579+
ScriptContext.Standard.SEARCH);
580580
innerHitsContext.scriptFields().add(new org.elasticsearch.search.fetch.subphase.ScriptFieldsContext.ScriptField(
581581
field.fieldName(), searchScript, field.ignoreFailure()));
582582
}

core/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ public long nowInMillis() {
127127
}
128128

129129
public BytesReference getTemplateBytes(Script template) {
130-
ExecutableScript executable = scriptService.executable(template,
131-
ScriptContext.Standard.SEARCH, Collections.emptyMap());
130+
ExecutableScript executable = scriptService.executable(template, ScriptContext.Standard.SEARCH);
132131
return (BytesReference) executable.run();
133132
}
134133

core/src/main/java/org/elasticsearch/index/query/QueryShardContext.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -338,38 +338,36 @@ public final Index index() {
338338
* Compiles (or retrieves from cache) and binds the parameters to the
339339
* provided script
340340
*/
341-
public final SearchScript getSearchScript(Script script, ScriptContext context, Map<String, String> params) {
341+
public final SearchScript getSearchScript(Script script, ScriptContext context) {
342342
failIfFrozen();
343-
return scriptService.search(lookup(), script, context, params);
343+
return scriptService.search(lookup(), script, context);
344344
}
345345
/**
346346
* Returns a lazily created {@link SearchScript} that is compiled immediately but can be pulled later once all
347347
* parameters are available.
348348
*/
349-
public final Function<Map<String, Object>, SearchScript> getLazySearchScript(Script script, ScriptContext context,
350-
Map<String, String> params) {
349+
public final Function<Map<String, Object>, SearchScript> getLazySearchScript(Script script, ScriptContext context) {
351350
failIfFrozen();
352-
CompiledScript compile = scriptService.compile(script, context, params);
351+
CompiledScript compile = scriptService.compile(script, context, script.getOptions());
353352
return (p) -> scriptService.search(lookup(), compile, p);
354353
}
355354

356355
/**
357356
* Compiles (or retrieves from cache) and binds the parameters to the
358357
* provided script
359358
*/
360-
public final ExecutableScript getExecutableScript(Script script, ScriptContext context, Map<String, String> params) {
359+
public final ExecutableScript getExecutableScript(Script script, ScriptContext context) {
361360
failIfFrozen();
362-
return scriptService.executable(script, context, params);
361+
return scriptService.executable(script, context);
363362
}
364363

365364
/**
366365
* Returns a lazily created {@link ExecutableScript} that is compiled immediately but can be pulled later once all
367366
* parameters are available.
368367
*/
369-
public final Function<Map<String, Object>, ExecutableScript> getLazyExecutableScript(Script script, ScriptContext context,
370-
Map<String, String> params) {
368+
public final Function<Map<String, Object>, ExecutableScript> getLazyExecutableScript(Script script, ScriptContext context) {
371369
failIfFrozen();
372-
CompiledScript executable = scriptService.compile(script, context, params);
370+
CompiledScript executable = scriptService.compile(script, context, script.getOptions());
373371
return (p) -> scriptService.executable(executable, p);
374372
}
375373

0 commit comments

Comments
 (0)