Skip to content

Commit bfaa0db

Browse files
committed
Add ScriptCompiler interface (#70657)
TypeParser.ParserContext exposes a ScriptService allowing type parsers to compile scripts from runtime fields (and in future, index-time scripts). However, ScriptService itself is a fairly heavyweight object, and you can only add script parsers to it via the Plugin mechanism. To make testing easier, this commit extracts a ScriptCompiler interface and makes that available from ParserContext instead.
1 parent b5f7350 commit bfaa0db

20 files changed

+302
-593
lines changed

server/src/main/java/org/elasticsearch/index/mapper/Mapper.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.elasticsearch.index.analysis.IndexAnalyzers;
1717
import org.elasticsearch.index.query.SearchExecutionContext;
1818
import org.elasticsearch.index.similarity.SimilarityProvider;
19-
import org.elasticsearch.script.ScriptService;
19+
import org.elasticsearch.script.ScriptCompiler;
2020

2121
import java.util.Map;
2222
import java.util.Objects;
@@ -52,7 +52,7 @@ class ParserContext {
5252
private final Version indexVersionCreated;
5353
private final Supplier<SearchExecutionContext> searchExecutionContextSupplier;
5454
private final DateFormatter dateFormatter;
55-
private final ScriptService scriptService;
55+
private final ScriptCompiler scriptCompiler;
5656
private final IndexAnalyzers indexAnalyzers;
5757
private final IndexSettings indexSettings;
5858
private final BooleanSupplier idFieldDataEnabled;
@@ -63,7 +63,7 @@ public ParserContext(Function<String, SimilarityProvider> similarityLookupServic
6363
Version indexVersionCreated,
6464
Supplier<SearchExecutionContext> searchExecutionContextSupplier,
6565
DateFormatter dateFormatter,
66-
ScriptService scriptService,
66+
ScriptCompiler scriptCompiler,
6767
IndexAnalyzers indexAnalyzers,
6868
IndexSettings indexSettings,
6969
BooleanSupplier idFieldDataEnabled) {
@@ -73,7 +73,7 @@ public ParserContext(Function<String, SimilarityProvider> similarityLookupServic
7373
this.indexVersionCreated = indexVersionCreated;
7474
this.searchExecutionContextSupplier = searchExecutionContextSupplier;
7575
this.dateFormatter = dateFormatter;
76-
this.scriptService = scriptService;
76+
this.scriptCompiler = scriptCompiler;
7777
this.indexAnalyzers = indexAnalyzers;
7878
this.indexSettings = indexSettings;
7979
this.idFieldDataEnabled = idFieldDataEnabled;
@@ -134,10 +134,10 @@ public DateFormatter getDateFormatter() {
134134
protected Function<String, SimilarityProvider> similarityLookupService() { return similarityLookupService; }
135135

136136
/**
137-
* The {@linkplain ScriptService} to compile scripts needed by the {@linkplain Mapper}.
137+
* The {@linkplain ScriptCompiler} to compile scripts needed by the {@linkplain Mapper}.
138138
*/
139-
public ScriptService scriptService() {
140-
return scriptService;
139+
public ScriptCompiler scriptCompiler() {
140+
return scriptCompiler;
141141
}
142142

143143
ParserContext createMultiFieldContext(ParserContext in) {
@@ -151,7 +151,7 @@ ParserContext createDynamicTemplateFieldContext(ParserContext in) {
151151
private static class MultiFieldParserContext extends ParserContext {
152152
MultiFieldParserContext(ParserContext in) {
153153
super(in.similarityLookupService, in.typeParsers, in.runtimeTypeParsers, in.indexVersionCreated,
154-
in.searchExecutionContextSupplier, in.dateFormatter, in.scriptService, in.indexAnalyzers, in.indexSettings,
154+
in.searchExecutionContextSupplier, in.dateFormatter, in.scriptCompiler, in.indexAnalyzers, in.indexSettings,
155155
in.idFieldDataEnabled);
156156
}
157157

@@ -162,7 +162,7 @@ private static class MultiFieldParserContext extends ParserContext {
162162
private static class DynamicTemplateParserContext extends ParserContext {
163163
DynamicTemplateParserContext(ParserContext in) {
164164
super(in.similarityLookupService, in.typeParsers, in.runtimeTypeParsers, in.indexVersionCreated,
165-
in.searchExecutionContextSupplier, in.dateFormatter, in.scriptService, in.indexAnalyzers, in.indexSettings,
165+
in.searchExecutionContextSupplier, in.dateFormatter, in.scriptCompiler, in.indexAnalyzers, in.indexSettings,
166166
in.idFieldDataEnabled);
167167
}
168168

server/src/main/java/org/elasticsearch/index/mapper/MapperService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import org.elasticsearch.index.query.SearchExecutionContext;
4242
import org.elasticsearch.index.similarity.SimilarityService;
4343
import org.elasticsearch.indices.InvalidTypeNameException;
44-
import org.elasticsearch.script.ScriptService;
44+
import org.elasticsearch.script.ScriptCompiler;
4545

4646
import java.io.Closeable;
4747
import java.io.IOException;
@@ -126,15 +126,15 @@ public enum MergeReason {
126126
public MapperService(IndexSettings indexSettings, IndexAnalyzers indexAnalyzers, NamedXContentRegistry xContentRegistry,
127127
SimilarityService similarityService, MapperRegistry mapperRegistry,
128128
Supplier<SearchExecutionContext> searchExecutionContextSupplier, BooleanSupplier idFieldDataEnabled,
129-
ScriptService scriptService) {
129+
ScriptCompiler scriptCompiler) {
130130
super(indexSettings);
131131
this.indexVersionCreated = indexSettings.getIndexVersionCreated();
132132
this.indexAnalyzers = indexAnalyzers;
133133
this.mapperRegistry = mapperRegistry;
134134
Function<DateFormatter, Mapper.TypeParser.ParserContext> parserContextFunction =
135135
dateFormatter -> new Mapper.TypeParser.ParserContext(similarityService::getSimilarity, mapperRegistry.getMapperParsers()::get,
136136
mapperRegistry.getRuntimeFieldTypeParsers()::get, indexVersionCreated, searchExecutionContextSupplier, dateFormatter,
137-
scriptService, indexAnalyzers, indexSettings, idFieldDataEnabled);
137+
scriptCompiler, indexAnalyzers, indexSettings, idFieldDataEnabled);
138138
this.documentParser = new DocumentParser(xContentRegistry, parserContextFunction);
139139
Map<String, MetadataFieldMapper.TypeParser> metadataMapperParsers =
140140
mapperRegistry.getMetadataMapperParsers(indexSettings.getIndexVersionCreated());

server/src/main/java/org/elasticsearch/runtimefields/mapper/BooleanScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected AbstractScriptFieldType<?> buildFieldType() {
4242
if (script.get() == null) {
4343
return new BooleanScriptFieldType(name, BooleanFieldScript.PARSE_FROM_SOURCE, this);
4444
}
45-
BooleanFieldScript.Factory factory = parserContext.scriptService().compile(script.getValue(), BooleanFieldScript.CONTEXT);
45+
BooleanFieldScript.Factory factory = parserContext.scriptCompiler().compile(script.getValue(), BooleanFieldScript.CONTEXT);
4646
return new BooleanScriptFieldType(name, factory, this);
4747
}
4848
});

server/src/main/java/org/elasticsearch/runtimefields/mapper/DateScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected AbstractScriptFieldType<?> buildFieldType() {
8888
if (script.get() == null) {
8989
return new DateScriptFieldType(name, DateFieldScript.PARSE_FROM_SOURCE, dateTimeFormatter, this);
9090
}
91-
DateFieldScript.Factory factory = parserContext.scriptService().compile(script.getValue(), DateFieldScript.CONTEXT);
91+
DateFieldScript.Factory factory = parserContext.scriptCompiler().compile(script.getValue(), DateFieldScript.CONTEXT);
9292
return new DateScriptFieldType(name, factory, dateTimeFormatter, this);
9393
}
9494
});

server/src/main/java/org/elasticsearch/runtimefields/mapper/DoubleScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected AbstractScriptFieldType<?> buildFieldType() {
4242
if (script.get() == null) {
4343
return new DoubleScriptFieldType(name, DoubleFieldScript.PARSE_FROM_SOURCE, this);
4444
}
45-
DoubleFieldScript.Factory factory = parserContext.scriptService().compile(script.getValue(), DoubleFieldScript.CONTEXT);
45+
DoubleFieldScript.Factory factory = parserContext.scriptCompiler().compile(script.getValue(), DoubleFieldScript.CONTEXT);
4646
return new DoubleScriptFieldType(name, factory, this);
4747
}
4848
});

server/src/main/java/org/elasticsearch/runtimefields/mapper/GeoPointScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected AbstractScriptFieldType<?> buildFieldType() {
4646
if (script.get() == null) {
4747
return new GeoPointScriptFieldType(name, GeoPointFieldScript.PARSE_FROM_SOURCE, this);
4848
}
49-
GeoPointFieldScript.Factory factory = parserContext.scriptService().compile(script.getValue(), GeoPointFieldScript.CONTEXT);
49+
GeoPointFieldScript.Factory factory = parserContext.scriptCompiler().compile(script.getValue(), GeoPointFieldScript.CONTEXT);
5050
return new GeoPointScriptFieldType(name, factory, this);
5151
}
5252
});

server/src/main/java/org/elasticsearch/runtimefields/mapper/IpScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected AbstractScriptFieldType<?> buildFieldType() {
5151
if (script.get() == null) {
5252
return new IpScriptFieldType(name, IpFieldScript.PARSE_FROM_SOURCE, this);
5353
}
54-
IpFieldScript.Factory factory = parserContext.scriptService().compile(script.getValue(), IpFieldScript.CONTEXT);
54+
IpFieldScript.Factory factory = parserContext.scriptCompiler().compile(script.getValue(), IpFieldScript.CONTEXT);
5555
return new IpScriptFieldType(name, factory, this);
5656
}
5757
});

server/src/main/java/org/elasticsearch/runtimefields/mapper/KeywordScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected AbstractScriptFieldType<?> buildFieldType() {
5050
if (script.get() == null) {
5151
return new KeywordScriptFieldType(name, StringFieldScript.PARSE_FROM_SOURCE, this);
5252
}
53-
StringFieldScript.Factory factory = parserContext.scriptService().compile(script.getValue(), StringFieldScript.CONTEXT);
53+
StringFieldScript.Factory factory = parserContext.scriptCompiler().compile(script.getValue(), StringFieldScript.CONTEXT);
5454
return new KeywordScriptFieldType(name, factory, this);
5555
}
5656
});

server/src/main/java/org/elasticsearch/runtimefields/mapper/LongScriptFieldType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected AbstractScriptFieldType<?> buildFieldType() {
4242
if (script.get() == null) {
4343
return new LongScriptFieldType(name, LongFieldScript.PARSE_FROM_SOURCE, this);
4444
}
45-
LongFieldScript.Factory factory = parserContext.scriptService().compile(script.getValue(), LongFieldScript.CONTEXT);
45+
LongFieldScript.Factory factory = parserContext.scriptCompiler().compile(script.getValue(), LongFieldScript.CONTEXT);
4646
return new LongScriptFieldType(name, factory, this);
4747
}
4848
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.script;
10+
11+
/**
12+
* Takes a Script definition and returns a compiled script factory
13+
*/
14+
public interface ScriptCompiler {
15+
16+
/**
17+
* Takes a Script definition and returns a compiled script factory
18+
* @param script the Script to compile
19+
* @param scriptContext the ScriptContext defining how to compile the script
20+
* @param <T> the class of the compiled Script factory
21+
* @return a Script factory
22+
*/
23+
<T> T compile(Script script, ScriptContext<T> scriptContext);
24+
25+
}

0 commit comments

Comments
 (0)