Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ScriptService to the field parser config #60933

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void setup() {
return indexService.newQueryShardContext(0, null, () -> { throw new UnsupportedOperationException(); }, null);
};
parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), indexService.xContentRegistry(),
indexService.similarityService(), mapperRegistry, queryShardContext);
indexService.similarityService(), mapperRegistry, queryShardContext, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ public class MetadataIndexUpgradeService {
private final NamedXContentRegistry xContentRegistry;
private final MapperRegistry mapperRegistry;
private final IndexScopedSettings indexScopedSettings;
private final ScriptService scriptService;

public MetadataIndexUpgradeService(Settings settings, NamedXContentRegistry xContentRegistry, MapperRegistry mapperRegistry,
IndexScopedSettings indexScopedSettings) {
IndexScopedSettings indexScopedSettings, ScriptService scriptService) {
this.settings = settings;
this.xContentRegistry = xContentRegistry;
this.mapperRegistry = mapperRegistry;
this.indexScopedSettings = indexScopedSettings;
this.scriptService = scriptService;
}

/**
Expand Down Expand Up @@ -181,7 +183,7 @@ public Set<Entry<String, NamedAnalyzer>> entrySet() {
try (IndexAnalyzers fakeIndexAnalzyers =
new IndexAnalyzers(analyzerMap, analyzerMap, analyzerMap)) {
MapperService mapperService = new MapperService(indexSettings, fakeIndexAnalzyers, xContentRegistry, similarityService,
mapperRegistry, () -> null, () -> false);
mapperRegistry, () -> null, () -> false, scriptService);
mapperService.merge(indexMetadata, MapperService.MergeReason.MAPPING_RECOVERY);
}
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public MapperService newIndexMapperService(NamedXContentRegistry xContentRegistr
ScriptService scriptService) throws IOException {
return new MapperService(indexSettings, analysisRegistry.build(indexSettings), xContentRegistry,
new SimilarityService(indexSettings, scriptService, similarities), mapperRegistry,
() -> { throw new UnsupportedOperationException("no index query shard context available"); }, () -> false);
() -> { throw new UnsupportedOperationException("no index query shard context available"); }, () -> false, scriptService);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public IndexService(
assert indexAnalyzers != null;
this.mapperService = new MapperService(indexSettings, indexAnalyzers, xContentRegistry, similarityService, mapperRegistry,
// we parse all percolator queries as they would be parsed on shard 0
() -> newQueryShardContext(0, null, System::currentTimeMillis, null), idFieldDataEnabled);
() -> newQueryShardContext(0, null, System::currentTimeMillis, null), idFieldDataEnabled, scriptService);
this.indexFieldData = new IndexFieldDataService(indexSettings, indicesFieldDataCache, circuitBreakerService, mapperService);
if (indexSettings.getIndexSortConfig().hasIndexSort()) {
// we delay the actual creation of the sort order for this index because the mapping has not been merged yet.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.similarity.SimilarityService;
import org.elasticsearch.indices.mapper.MapperRegistry;
import org.elasticsearch.script.ScriptService;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -53,26 +54,29 @@ public class DocumentMapperParser {

private final Map<String, Mapper.TypeParser> typeParsers;
private final Map<String, MetadataFieldMapper.TypeParser> rootTypeParsers;
private final ScriptService scriptService;

public DocumentMapperParser(IndexSettings indexSettings, MapperService mapperService, NamedXContentRegistry xContentRegistry,
SimilarityService similarityService, MapperRegistry mapperRegistry, Supplier<QueryShardContext> queryShardContextSupplier) {
SimilarityService similarityService, MapperRegistry mapperRegistry,
Supplier<QueryShardContext> queryShardContextSupplier, ScriptService scriptService) {
this.mapperService = mapperService;
this.xContentRegistry = xContentRegistry;
this.similarityService = similarityService;
this.queryShardContextSupplier = queryShardContextSupplier;
this.scriptService = scriptService;
this.typeParsers = mapperRegistry.getMapperParsers();
this.indexVersionCreated = indexSettings.getIndexVersionCreated();
this.rootTypeParsers = mapperRegistry.getMetadataMapperParsers(indexVersionCreated);
}

public Mapper.TypeParser.ParserContext parserContext() {
return new Mapper.TypeParser.ParserContext(similarityService::getSimilarity, mapperService,
typeParsers::get, indexVersionCreated, queryShardContextSupplier, null);
typeParsers::get, indexVersionCreated, queryShardContextSupplier, null, scriptService);
}

public Mapper.TypeParser.ParserContext parserContext(DateFormatter dateFormatter) {
return new Mapper.TypeParser.ParserContext(similarityService::getSimilarity, mapperService,
typeParsers::get, indexVersionCreated, queryShardContextSupplier, dateFormatter);
typeParsers::get, indexVersionCreated, queryShardContextSupplier, dateFormatter, scriptService);
}

public DocumentMapper parse(@Nullable String type, CompressedXContent source) throws MapperParsingException {
Expand Down
15 changes: 13 additions & 2 deletions server/src/main/java/org/elasticsearch/index/mapper/Mapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.index.analysis.IndexAnalyzers;
import org.elasticsearch.index.query.QueryShardContext;
import org.elasticsearch.index.similarity.SimilarityProvider;
import org.elasticsearch.script.ScriptService;

import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -91,16 +92,19 @@ class ParserContext {

private final DateFormatter dateFormatter;

private final ScriptService scriptService;

public ParserContext(Function<String, SimilarityProvider> similarityLookupService,
MapperService mapperService, Function<String, TypeParser> typeParsers,
Version indexVersionCreated, Supplier<QueryShardContext> queryShardContextSupplier,
DateFormatter dateFormatter) {
DateFormatter dateFormatter, ScriptService scriptService) {
this.similarityLookupService = similarityLookupService;
this.mapperService = mapperService;
this.typeParsers = typeParsers;
this.indexVersionCreated = indexVersionCreated;
this.queryShardContextSupplier = queryShardContextSupplier;
this.dateFormatter = dateFormatter;
this.scriptService = scriptService;
}

public IndexAnalyzers getIndexAnalyzers() {
Expand Down Expand Up @@ -146,14 +150,21 @@ public DateFormatter getDateFormatter() {

protected Function<String, SimilarityProvider> similarityLookupService() { return similarityLookupService; }

/**
* The {@linkplain ScriptService} to compile scripts needed by the {@linkplain Mapper}.
*/
public ScriptService scriptService() {
return scriptService;
}

public ParserContext createMultiFieldContext(ParserContext in) {
return new MultiFieldParserContext(in);
}

static class MultiFieldParserContext extends ParserContext {
MultiFieldParserContext(ParserContext in) {
super(in.similarityLookupService(), in.mapperService(), in.typeParsers(),
in.indexVersionCreated(), in.queryShardContextSupplier(), in.getDateFormatter());
in.indexVersionCreated(), in.queryShardContextSupplier(), in.getDateFormatter(), in.scriptService());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.elasticsearch.index.similarity.SimilarityService;
import org.elasticsearch.indices.IndicesModule;
import org.elasticsearch.indices.mapper.MapperRegistry;
import org.elasticsearch.script.ScriptService;

import java.io.Closeable;
import java.io.IOException;
Expand Down Expand Up @@ -125,12 +126,13 @@ public enum MergeReason {

public MapperService(IndexSettings indexSettings, IndexAnalyzers indexAnalyzers, NamedXContentRegistry xContentRegistry,
SimilarityService similarityService, MapperRegistry mapperRegistry,
Supplier<QueryShardContext> queryShardContextSupplier, BooleanSupplier idFieldDataEnabled) {
Supplier<QueryShardContext> queryShardContextSupplier, BooleanSupplier idFieldDataEnabled,
ScriptService scriptService) {
super(indexSettings);
this.indexVersionCreated = indexSettings.getIndexVersionCreated();
this.indexAnalyzers = indexAnalyzers;
this.documentParser = new DocumentMapperParser(indexSettings, this, xContentRegistry, similarityService, mapperRegistry,
queryShardContextSupplier);
queryShardContextSupplier, scriptService);
this.indexAnalyzer = new MapperAnalyzerWrapper(indexAnalyzers.getDefaultIndexAnalyzer(), MappedFieldType::indexAnalyzer);
this.searchAnalyzer = new MapperAnalyzerWrapper(indexAnalyzers.getDefaultSearchAnalyzer(),
p -> p.getTextSearchInfo().getSearchAnalyzer());
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/org/elasticsearch/node/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ protected Node(final Environment initialEnvironment,
.collect(Collectors.toList());
final MetadataUpgrader metadataUpgrader = new MetadataUpgrader(indexTemplateMetadataUpgraders);
final MetadataIndexUpgradeService metadataIndexUpgradeService = new MetadataIndexUpgradeService(settings, xContentRegistry,
indicesModule.getMapperRegistry(), settingsModule.getIndexScopedSettings());
indicesModule.getMapperRegistry(), settingsModule.getIndexScopedSettings(), scriptService);
new TemplateUpgradeService(client, clusterService, threadPool, indexTemplateMetadataUpgraders);
final Transport transport = networkModule.getTransportSupplier().get();
Set<String> taskHeaders = Stream.concat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private MetadataIndexUpgradeService getMetadataIndexUpgradeService() {
Settings.EMPTY,
xContentRegistry(),
new MapperRegistry(Collections.emptyMap(), Collections.emptyMap(), MapperPlugin.NOOP_FIELD_FILTER),
IndexScopedSettings.DEFAULT_SCOPED_SETTINGS);
IndexScopedSettings.DEFAULT_SCOPED_SETTINGS, null);
}

public static IndexMetadata newIndexMeta(String name, Settings indexSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ private static class MockMetadataIndexUpgradeService extends MetadataIndexUpgrad
private final boolean upgrade;

MockMetadataIndexUpgradeService(boolean upgrade) {
super(Settings.EMPTY, null, null, null);
super(Settings.EMPTY, null, null, null, null);
this.upgrade = upgrade;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private CodecService createCodecService() throws IOException {
IndexAnalyzers indexAnalyzers = createTestAnalysis(settings, nodeSettings).indexAnalyzers;
MapperRegistry mapperRegistry = new MapperRegistry(Collections.emptyMap(), Collections.emptyMap(), MapperPlugin.NOOP_FIELD_FILTER);
MapperService service = new MapperService(settings, indexAnalyzers, xContentRegistry(), similarityService, mapperRegistry,
() -> null, () -> false);
() -> null, () -> false, null);
return new CodecService(service, LogManager.getLogger("test"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testExternalValues() throws Exception {
return indexService.newQueryShardContext(0, null, () -> { throw new UnsupportedOperationException(); }, null);
};
DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(),
indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext);
indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext, null);
DocumentMapper documentMapper = parser.parse("type", new CompressedXContent(
Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject(ExternalMetadataMapper.CONTENT_TYPE)
Expand Down Expand Up @@ -120,7 +120,7 @@ public void testExternalValuesWithMultifield() throws Exception {
return indexService.newQueryShardContext(0, null, () -> { throw new UnsupportedOperationException(); }, null);
};
DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(),
indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext);
indexService.xContentRegistry(), indexService.similarityService(), mapperRegistry, queryShardContext, null);

DocumentMapper documentMapper = parser.parse("type", new CompressedXContent(
Strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private static TestMapper fromMapping(String mapping, Version version) {
return BinaryFieldMapper.PARSER;
}
return null;
}, version, () -> null, null);
}, version, () -> null, null, null);
return (TestMapper) new TypeParser()
.parse("field", XContentHelper.convertToMap(JsonXContent.jsonXContent, mapping, true), pc)
.build(new Mapper.BuilderContext(Settings.EMPTY, new ContentPath(0)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void testMultiFieldWithinMultiField() throws IOException {

Version olderVersion = VersionUtils.randomPreviousCompatibleVersion(random(), Version.V_8_0_0);
Mapper.TypeParser.ParserContext olderContext = new Mapper.TypeParser.ParserContext(
null, null, type -> typeParser, olderVersion, null, null);
null, null, type -> typeParser, olderVersion, null, null, null);

TypeParsers.parseField(builder, "some-field", fieldNode, olderContext);
assertWarnings("At least one multi-field, [sub-field], " +
Expand All @@ -207,7 +207,7 @@ public void testMultiFieldWithinMultiField() throws IOException {

Version version = VersionUtils.randomVersionBetween(random(), Version.V_8_0_0, Version.CURRENT);
Mapper.TypeParser.ParserContext context = new Mapper.TypeParser.ParserContext(
null, null, type -> typeParser, version, null, null);
null, null, type -> typeParser, version, null, null, null);

IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> TypeParsers.parseField(builder, "some-field", fieldNodeCopy, context));
Expand All @@ -233,7 +233,7 @@ public TokenStream create(TokenStream tokenStream) {

public void testParseMeta() {
FieldMapper.Builder<?> builder = new KeywordFieldMapper.Builder("foo");
Mapper.TypeParser.ParserContext parserContext = new Mapper.TypeParser.ParserContext(null, null, null, null, null, null);
Mapper.TypeParser.ParserContext parserContext = new Mapper.TypeParser.ParserContext(null, null, null, null, null, null, null);

{
Map<String, Object> mapping = new HashMap<>(Map.of("meta", 3));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ public ClusterStateChanges(NamedXContentRegistry xContentRegistry, ThreadPool th
TransportService.NOOP_TRANSPORT_INTERCEPTOR,
boundAddress -> DiscoveryNode.createLocal(SETTINGS, boundAddress.publishAddress(), UUIDs.randomBase64UUID()), clusterSettings,
Collections.emptySet());
MetadataIndexUpgradeService metadataIndexUpgradeService = new MetadataIndexUpgradeService(SETTINGS, xContentRegistry, null, null) {
MetadataIndexUpgradeService metadataIndexUpgradeService = new MetadataIndexUpgradeService(
SETTINGS,
xContentRegistry,
null,
null,
null
) {
// metadata upgrader should do nothing
@Override
public IndexMetadata upgradeIndexMetadata(IndexMetadata indexMetadata, Version minimumIndexCompatibilityVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ clusterService, indicesService, threadPool, shardStateAction, mappingUpdatedActi
new MetadataIndexUpgradeService(
settings, namedXContentRegistry,
mapperRegistry,
indexScopedSettings),
indexScopedSettings, null),
clusterSettings,
shardLimitValidator
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static MapperService newMapperService(NamedXContentRegistry xContentRegis
xContentRegistry,
similarityService,
mapperRegistry,
() -> null, () -> false);
() -> null, () -> false, null);
}

public static void assertConflicts(String mapping1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public TranslogHandler(NamedXContentRegistry xContentRegistry, IndexSettings ind
SimilarityService similarityService = new SimilarityService(indexSettings, null, emptyMap());
MapperRegistry mapperRegistry = new IndicesModule(emptyList()).getMapperRegistry();
mapperService = new MapperService(indexSettings, indexAnalyzers, xContentRegistry, similarityService, mapperRegistry,
() -> null, () -> false);
() -> null, () -> false, null);
}

private DocumentMapperForType docMapper(String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ private void writeTestDoc(MappedFieldType fieldType, String fieldName, RandomInd

private class MockParserContext extends Mapper.TypeParser.ParserContext {
MockParserContext() {
super(null, null, null, null, null, null);
super(null, null, null, null, null, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ private static class ServiceHolder implements Closeable {
similarityService = new SimilarityService(idxSettings, null, Collections.emptyMap());
MapperRegistry mapperRegistry = indicesModule.getMapperRegistry();
mapperService = new MapperService(idxSettings, indexAnalyzers, xContentRegistry, similarityService, mapperRegistry,
() -> createShardContext(null), () -> false);
() -> createShardContext(null), () -> false, null);
IndicesFieldDataCache indicesFieldDataCache = new IndicesFieldDataCache(nodeSettings, new IndexFieldDataCache.Listener() {
});
indexFieldDataService = new IndexFieldDataService(idxSettings, indicesFieldDataCache,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class RuntimeScriptFieldMapper extends ParametrizedFieldMapper {
public static final TypeParser PARSER = new TypeParser((name, parserContext) -> new Builder(name, new ScriptCompiler() {
@Override
public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
return parserContext.queryShardContextSupplier().get().compile(script, context);
return parserContext.scriptService().compile(script, context);
}
}));

Expand Down
Loading