2828import org .elasticsearch .index .mapper .vectors .DenseVectorFieldMapper ;
2929import org .elasticsearch .index .query .SearchExecutionContext ;
3030import org .elasticsearch .indices .breaker .NoneCircuitBreakerService ;
31- import org .elasticsearch .plugins .internal .XContentMeteringParserDecorator ;
31+ import org .elasticsearch .plugins .internal .XContentParserDecorator ;
3232import org .elasticsearch .search .lookup .LeafFieldLookupProvider ;
3333import org .elasticsearch .search .lookup .SearchLookup ;
3434import org .elasticsearch .search .lookup .Source ;
3737import org .elasticsearch .xcontent .XContentParseException ;
3838import org .elasticsearch .xcontent .XContentParser ;
3939import org .elasticsearch .xcontent .XContentParserConfiguration ;
40- import org .elasticsearch .xcontent .XContentType ;
4140
4241import java .io .IOException ;
4342import java .util .ArrayList ;
@@ -85,19 +84,22 @@ public ParsedDocument parseDocument(SourceToParse source, MappingLookup mappingL
8584 throw new DocumentParsingException (new XContentLocation (0 , 0 ), "failed to parse, document is empty" );
8685 }
8786 final RootDocumentParserContext context ;
88- final XContentType xContentType = source .getXContentType ();
8987
90- XContentMeteringParserDecorator meteringParserDecorator = source .getMeteringParserDecorator ();
9188 try (
92- XContentParser parser = meteringParserDecorator .decorate (
89+ // the context needs to be closed after parsing to make sure the actual parser is properly closed;
90+ // closing won't impact other state of the context
91+ RootDocumentParserContext ctx = new RootDocumentParserContext (
92+ mappingLookup ,
93+ mappingParserContext ,
94+ source ,
9395 XContentHelper .createParser (
9496 parserConfiguration .withIncludeSourceOnError (source .getIncludeSourceOnError ()),
9597 source .source (),
96- xContentType
98+ source . getXContentType ()
9799 )
98100 )
99101 ) {
100- context = new RootDocumentParserContext ( mappingLookup , mappingParserContext , source , parser ) ;
102+ context = ctx ;
101103 validateStart (context .parser ());
102104 MetadataFieldMapper [] metadataFieldsMappers = mappingLookup .getMapping ().getSortedMetadataMappers ();
103105 internalParseDocument (metadataFieldsMappers , context );
@@ -121,7 +123,7 @@ public ParsedDocument parseDocument(SourceToParse source, MappingLookup mappingL
121123 context .sourceToParse ().source (),
122124 context .sourceToParse ().getXContentType (),
123125 dynamicUpdate ,
124- meteringParserDecorator .meteredDocumentSize ()
126+ source . getMeteringParserDecorator () .meteredDocumentSize ()
125127 ) {
126128 @ Override
127129 public String documentDescription () {
@@ -1085,7 +1087,7 @@ public ObjectMapper merge(Mapper mergeWith, MapperMergeContext mapperMergeContex
10851087 * Internal version of {@link DocumentParserContext} that is aware of implementation details like nested documents
10861088 * and how they are stored in the lucene index.
10871089 */
1088- private static class RootDocumentParserContext extends DocumentParserContext {
1090+ private static class RootDocumentParserContext extends DocumentParserContext implements AutoCloseable {
10891091 private final ContentPath path = new ContentPath ();
10901092 private final XContentParser parser ;
10911093 private final LuceneDocument document ;
@@ -1120,17 +1122,23 @@ private static class RootDocumentParserContext extends DocumentParserContext {
11201122 this .tsid = tsid ;
11211123 assert this .tsid == null || indexSettings .getMode () == IndexMode .TIME_SERIES
11221124 : "tsid should only be set for time series indices" ;
1125+ XContentParserDecorator parserDecorator = source .getMeteringParserDecorator ();
11231126 if (mappingLookup .getMapping ().getRoot ().subobjects () == ObjectMapper .Subobjects .ENABLED ) {
1124- this .parser = DotExpandingXContentParser .expandDots (parser , this .path );
1127+ this .parser = parserDecorator . decorate ( DotExpandingXContentParser .expandDots (parser , this .path ) );
11251128 } else {
1126- this .parser = parser ;
1129+ this .parser = parserDecorator . decorate ( parser ) ;
11271130 }
11281131 this .document = new LuceneDocument ();
11291132 this .documents .add (document );
11301133 this .maxAllowedNumNestedDocs = indexSettings ().getMappingNestedDocsLimit ();
11311134 this .numNestedDocs = 0L ;
11321135 }
11331136
1137+ @ Override
1138+ public void close () throws IOException {
1139+ parser .close ();
1140+ }
1141+
11341142 @ Override
11351143 public Mapper getMapper (String name ) {
11361144 Mapper mapper = getMetadataMapper (name );
0 commit comments