Skip to content

Commit 4c33e3e

Browse files
authored
Simplify dynamic template parser context creation (#74326)
DocumentParser takes care of parsing documents and applying dynamic mapping updates for each new field it encounters, if applicable, as well as applying the matching dynamic templates. To do this, it needs to be able to parse mappings, which requires a ParserContext that the ParseContext exposes given a DateFormatter. So far, we have carried around a function that allows to create a new ParserContext given a date formatter, and then call a specific method that allows to wrap it to provide a special parser context that is specific for dynamic templates, which is needed to make some decisions down the line based on whether mappings come from dynamic templates or not. This commit tries to simplify this by exposing the specific dynamic template parser context only to the document parse context, as that's all it needs. We recently ended up using the wrong context and with this change it would not be possible. Relates to #74177
1 parent c412aae commit 4c33e3e

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,6 @@ ParserContext createMultiFieldContext(ParserContext in) {
144144
return new MultiFieldParserContext(in);
145145
}
146146

147-
ParserContext createDynamicTemplateFieldContext() {
148-
return new DynamicTemplateParserContext(this);
149-
}
150-
151147
private static class MultiFieldParserContext extends ParserContext {
152148
MultiFieldParserContext(ParserContext in) {
153149
super(in.similarityLookupService, in.typeParsers, in.runtimeFieldParsers, in.indexVersionCreated,
@@ -159,7 +155,7 @@ private static class MultiFieldParserContext extends ParserContext {
159155
public boolean isWithinMultiField() { return true; }
160156
}
161157

162-
private static class DynamicTemplateParserContext extends ParserContext {
158+
static class DynamicTemplateParserContext extends ParserContext {
163159
DynamicTemplateParserContext(ParserContext in) {
164160
super(in.similarityLookupService, in.typeParsers, in.runtimeFieldParsers, in.indexVersionCreated,
165161
in.searchExecutionContextSupplier, in.dateFormatter, in.scriptCompiler, in.indexAnalyzers, in.indexSettings,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ public MapperService(IndexSettings indexSettings, IndexAnalyzers indexAnalyzers,
111111
dateFormatter -> new Mapper.TypeParser.ParserContext(similarityService::getSimilarity, mapperRegistry.getMapperParsers()::get,
112112
mapperRegistry.getRuntimeFieldParsers()::get, indexVersionCreated, searchExecutionContextSupplier, dateFormatter,
113113
scriptCompiler, indexAnalyzers, indexSettings, idFieldDataEnabled);
114-
this.documentParser = new DocumentParser(xContentRegistry, parserContextFunction, indexSettings, indexAnalyzers);
114+
this.documentParser = new DocumentParser(xContentRegistry,
115+
dateFormatter -> new Mapper.TypeParser.ParserContext.DynamicTemplateParserContext(parserContextFunction.apply(dateFormatter)),
116+
indexSettings, indexAnalyzers);
115117
Map<String, MetadataFieldMapper.TypeParser> metadataMapperParsers =
116118
mapperRegistry.getMetadataMapperParsers(indexSettings.getIndexVersionCreated());
117119
this.parserContextSupplier = () -> parserContextFunction.apply(null);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,13 @@ public static class InternalParseContext extends ParseContext {
319319
public InternalParseContext(MappingLookup mappingLookup,
320320
IndexSettings indexSettings,
321321
IndexAnalyzers indexAnalyzers,
322-
Function<DateFormatter, Mapper.TypeParser.ParserContext> parserContextFunction,
322+
Function<DateFormatter, Mapper.TypeParser.ParserContext> parserContext,
323323
SourceToParse source,
324324
XContentParser parser) {
325325
this.mappingLookup = mappingLookup;
326326
this.indexSettings = indexSettings;
327327
this.indexAnalyzers = indexAnalyzers;
328-
this.parserContextFunction = parserContextFunction;
328+
this.parserContextFunction = parserContext;
329329
this.parser = parser;
330330
this.document = new Document();
331331
this.documents.add(document);
@@ -337,7 +337,7 @@ public InternalParseContext(MappingLookup mappingLookup,
337337

338338
@Override
339339
public Mapper.TypeParser.ParserContext dynamicTemplateParserContext(DateFormatter dateFormatter) {
340-
return parserContextFunction.apply(dateFormatter).createDynamicTemplateFieldContext();
340+
return parserContextFunction.apply(dateFormatter);
341341
}
342342

343343
@Override

server/src/test/java/org/elasticsearch/index/mapper/ParametrizedMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private static TestMapper fromMapping(String mapping, Version version, boolean f
208208
throw new UnsupportedOperationException();
209209
});
210210
if (fromDynamicTemplate) {
211-
pc = pc.createDynamicTemplateFieldContext();
211+
pc = new Mapper.TypeParser.ParserContext.DynamicTemplateParserContext(pc);
212212
}
213213
return (TestMapper) new TypeParser()
214214
.parse("field", XContentHelper.convertToMap(JsonXContent.jsonXContent, mapping, true), pc)

0 commit comments

Comments
 (0)