From b29c09b798c8f73c01bf72ad68b7a43d11ad4b76 Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Tue, 5 Jan 2021 10:00:58 +0100 Subject: [PATCH] Don't require DocumentMapper as an argument when parsing a document (#66780) Currently, an incoming document is parsed through `DocumentMapper#parse`, which in turns calls `DocumentParser#parseDocument` providing `this` among other arguments. As part of the effort to reduce usages of `DocumentMapper` when possible, as it represents the mutable side of mappings (through mappings updates) and involves complexity, we can carry around only the needed components. This does add some required arguments to `DocumentParser#parseDocument` , though it makes dependencies clearer. This change does not affect end consumers as they all go through DocumentMapper anyways, but by not needed to provide DocumentMapper to parseDocument, we may be able to unblock further improvements down the line. Relates to #66295 --- .../percolator/CandidateQueryTests.java | 3 +- .../PercolatorFieldMapperTests.java | 18 ++++--- .../index/mapper/DocumentMapper.java | 17 ++---- .../index/mapper/DocumentParser.java | 54 ++++++++++++------- .../index/mapper/ParseContext.java | 15 +++--- .../index/mapper/DocumentParserTests.java | 4 +- 6 files changed, 66 insertions(+), 45 deletions(-) diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java index a6525a9d321f5..62ea9a56d9135 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/CandidateQueryTests.java @@ -1119,7 +1119,8 @@ private void duelRun(PercolateQuery.QueryStore queryStore, MemoryIndex memoryInd } private void addQuery(Query query, List docs) { - ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper, null, null, null, null); + ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper.mapping(), + documentMapper.mappers(), mapperService.getIndexSettings(), mapperService.getIndexAnalyzers(), null, null, null, null); fieldMapper.processQuery(query, parseContext); ParseContext.Document queryDocument = parseContext.doc(); // Add to string representation of the query to make debugging easier: diff --git a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java index 4072713403923..53f983459fbdc 100644 --- a/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java +++ b/modules/percolator/src/test/java/org/elasticsearch/percolator/PercolatorFieldMapperTests.java @@ -181,7 +181,8 @@ public void testExtractTerms() throws Exception { DocumentMapper documentMapper = mapperService.documentMapper("doc"); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); - ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper, null, null, null, null); + ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper.mapping(), + documentMapper.mappers(), mapperService.getIndexSettings(), mapperService.getIndexAnalyzers(), null, null, null, null); fieldMapper.processQuery(bq.build(), parseContext); ParseContext.Document document = parseContext.doc(); @@ -202,7 +203,8 @@ public void testExtractTerms() throws Exception { bq.add(termQuery1, Occur.MUST); bq.add(termQuery2, Occur.MUST); - parseContext = new ParseContext.InternalParseContext(documentMapper, null, null, null, null); + parseContext = new ParseContext.InternalParseContext(documentMapper.mapping(), + documentMapper.mappers(), mapperService.getIndexSettings(), mapperService.getIndexAnalyzers(), null, null, null, null); fieldMapper.processQuery(bq.build(), parseContext); document = parseContext.doc(); @@ -231,7 +233,8 @@ public void testExtractRanges() throws Exception { DocumentMapper documentMapper = mapperService.documentMapper("doc"); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); - ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper, null, null, null, null); + ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper.mapping(), + documentMapper.mappers(), mapperService.getIndexSettings(), mapperService.getIndexAnalyzers(), null, null, null, null); fieldMapper.processQuery(bq.build(), parseContext); ParseContext.Document document = parseContext.doc(); @@ -256,7 +259,8 @@ public void testExtractRanges() throws Exception { .rangeQuery(15, 20, true, true, null, null, null, context); bq.add(rangeQuery2, Occur.MUST); - parseContext = new ParseContext.InternalParseContext(documentMapper, null, null, null, null); + parseContext = new ParseContext.InternalParseContext(documentMapper.mapping(), + documentMapper.mappers(), mapperService.getIndexSettings(), mapperService.getIndexAnalyzers(), null, null, null, null); fieldMapper.processQuery(bq.build(), parseContext); document = parseContext.doc(); @@ -279,7 +283,8 @@ public void testExtractTermsAndRanges_failed() throws Exception { TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true); DocumentMapper documentMapper = mapperService.documentMapper("doc"); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); - ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper, null, null, null, null); + ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper.mapping(), + documentMapper.mappers(), mapperService.getIndexSettings(), mapperService.getIndexAnalyzers(), null, null, null, null); fieldMapper.processQuery(query, parseContext); ParseContext.Document document = parseContext.doc(); @@ -293,7 +298,8 @@ public void testExtractTermsAndRanges_partial() throws Exception { PhraseQuery phraseQuery = new PhraseQuery("field", "term"); DocumentMapper documentMapper = mapperService.documentMapper("doc"); PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName); - ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper, null, null, null, null); + ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(documentMapper.mapping(), + documentMapper.mappers(), mapperService.getIndexSettings(), mapperService.getIndexAnalyzers(), null, null, null, null); fieldMapper.processQuery(phraseQuery, parseContext); ParseContext.Document document = parseContext.doc(); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index a58d0c4268d5e..f506938953fc0 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -43,7 +43,6 @@ import java.util.function.Function; import java.util.stream.Stream; - public class DocumentMapper implements ToXContentFragment { public static class Builder { @@ -134,14 +133,6 @@ private DocumentMapper(IndexSettings indexSettings, .filter(field -> noopTombstoneMetadataFields.contains(field.name())).toArray(MetadataFieldMapper[]::new); } - IndexSettings indexSettings() { - return indexSettings; - } - - IndexAnalyzers indexAnalyzers() { - return indexAnalyzers; - } - public Mapping mapping() { return mapping; } @@ -203,18 +194,20 @@ public MappingLookup mappers() { } public ParsedDocument parse(SourceToParse source) throws MapperParsingException { - return documentParser.parseDocument(source, mapping.metadataMappers, this); + return documentParser.parseDocument(source, mapping, fieldMappers, indexSettings, indexAnalyzers); } public ParsedDocument createDeleteTombstoneDoc(String index, String type, String id) throws MapperParsingException { final SourceToParse emptySource = new SourceToParse(index, type, id, new BytesArray("{}"), XContentType.JSON); - return documentParser.parseDocument(emptySource, deleteTombstoneMetadataFieldMappers, this).toTombstone(); + return documentParser.parseDocument(emptySource, mapping, deleteTombstoneMetadataFieldMappers, fieldMappers, + indexSettings, indexAnalyzers).toTombstone(); } public ParsedDocument createNoopTombstoneDoc(String index, String reason) throws MapperParsingException { final String id = ""; // _id won't be used. final SourceToParse sourceToParse = new SourceToParse(index, type, id, new BytesArray("{}"), XContentType.JSON); - final ParsedDocument parsedDoc = documentParser.parseDocument(sourceToParse, noopTombstoneMetadataFieldMappers, this).toTombstone(); + final ParsedDocument parsedDoc = documentParser.parseDocument(sourceToParse, mapping, noopTombstoneMetadataFieldMappers, + fieldMappers, indexSettings, indexAnalyzers).toTombstone(); // Store the reason of a noop as a raw string in the _source field final BytesRef byteRef = new BytesRef(reason); parsedDoc.rootDoc().add(new StoredField(SourceFieldMapper.NAME, byteRef.bytes, byteRef.offset, byteRef.length)); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java b/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java index c9bd86099f4b8..4c8445a5f03ab 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -40,6 +40,8 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.IndexSettings; +import org.elasticsearch.index.analysis.IndexAnalyzers; import org.elasticsearch.index.query.QueryShardContext; /** A parser for documents, given mappings from a DocumentMapper */ @@ -58,19 +60,34 @@ final class DocumentParser { } ParsedDocument parseDocument(SourceToParse source, - MetadataFieldMapper[] metadataFieldsMappers, - DocumentMapper docMapper) throws MapperParsingException { - validateType(source, docMapper); + Mapping mapping, + MappingLookup mappingLookup, + IndexSettings indexSettings, + IndexAnalyzers indexAnalyzers) throws MapperParsingException { + return parseDocument(source, mapping, mapping.metadataMappers, mappingLookup, indexSettings, indexAnalyzers); + } - final Mapping mapping = docMapper.mapping(); + ParsedDocument parseDocument(SourceToParse source, + Mapping mapping, + MetadataFieldMapper[] metadataFieldsMappers, + MappingLookup mappingLookup, + IndexSettings indexSettings, + IndexAnalyzers indexAnalyzers) throws MapperParsingException { + validateType(source, mappingLookup.getType()); final ParseContext.InternalParseContext context; final XContentType xContentType = source.getXContentType(); - try (XContentParser parser = XContentHelper.createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, source.source(), xContentType)) { - context = new ParseContext.InternalParseContext(docMapper, dateParserContext, dynamicRuntimeFieldsBuilder, source, parser); + context = new ParseContext.InternalParseContext(mapping, + mappingLookup, + indexSettings, + indexAnalyzers, + dateParserContext, + dynamicRuntimeFieldsBuilder, + source, + parser); validateStart(parser); - internalParseDocument(mapping, metadataFieldsMappers, context, parser); + internalParseDocument(mapping.root(), metadataFieldsMappers, context, parser); validateEnd(parser); } catch (Exception e) { throw wrapInMapperParsingException(source, e); @@ -82,7 +99,7 @@ ParsedDocument parseDocument(SourceToParse source, context.postParse(); - return parsedDocument(source, context, createDynamicUpdate(mapping, docMapper.mappers(), + return parsedDocument(source, context, createDynamicUpdate(mapping, mappingLookup, context.getDynamicMappers(), context.getDynamicRuntimeFields())); } @@ -100,19 +117,19 @@ private static boolean containsDisabledObjectMapper(ObjectMapper objectMapper, S return false; } - private static void internalParseDocument(Mapping mapping, MetadataFieldMapper[] metadataFieldsMappers, + private static void internalParseDocument(RootObjectMapper root, MetadataFieldMapper[] metadataFieldsMappers, ParseContext context, XContentParser parser) throws IOException { - final boolean emptyDoc = isEmptyDoc(mapping, parser); + final boolean emptyDoc = isEmptyDoc(root, parser); for (MetadataFieldMapper metadataMapper : metadataFieldsMappers) { metadataMapper.preParse(context); } - if (mapping.root.isEnabled() == false) { + if (root.isEnabled() == false) { // entire type is disabled parser.skipChildren(); } else if (emptyDoc == false) { - parseObjectOrNested(context, mapping.root); + parseObjectOrNested(context, root); } for (MetadataFieldMapper metadataMapper : metadataFieldsMappers) { @@ -120,15 +137,14 @@ private static void internalParseDocument(Mapping mapping, MetadataFieldMapper[] } } - private void validateType(SourceToParse source, DocumentMapper docMapper) { - if (docMapper.type().equals(MapperService.DEFAULT_MAPPING)) { + private void validateType(SourceToParse source, String type) { + if (type.equals(MapperService.DEFAULT_MAPPING)) { throw new IllegalArgumentException("It is forbidden to index into the default mapping [" + MapperService.DEFAULT_MAPPING + "]"); } - if (Objects.equals(source.type(), docMapper.type()) == false && + if (Objects.equals(source.type(), type) == false && MapperService.SINGLE_MAPPING_NAME.equals(source.type()) == false) { // used by typeless APIs - throw new MapperParsingException("Type mismatch, provide type [" + source.type() + "] but mapper is of type [" - + docMapper.type() + "]"); + throw new MapperParsingException("Type mismatch, provide type [" + source.type() + "] but mapper is of type [" + type + "]"); } } @@ -150,8 +166,8 @@ private static void validateEnd(XContentParser parser) throws IOException { } } - private static boolean isEmptyDoc(Mapping mapping, XContentParser parser) throws IOException { - if (mapping.root.isEnabled()) { + private static boolean isEmptyDoc(RootObjectMapper root, XContentParser parser) throws IOException { + if (root.isEnabled()) { final XContentParser.Token token = parser.nextToken(); if (token == XContentParser.Token.END_OBJECT) { // empty doc, we can handle it... diff --git a/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java b/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java index 48910a42b285f..0fa55a16ccfce 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/ParseContext.java @@ -342,15 +342,18 @@ public static class InternalParseContext extends ParseContext { private long numNestedDocs; private boolean docsReversed = false; - public InternalParseContext(DocumentMapper docMapper, + public InternalParseContext(Mapping mapping, + MappingLookup mappingLookup, + IndexSettings indexSettings, + IndexAnalyzers indexAnalyzers, Function parserContextFunction, DynamicRuntimeFieldsBuilder dynamicRuntimeFieldsBuilder, SourceToParse source, XContentParser parser) { - this.mapping = docMapper.mapping(); - this.mappingLookup = docMapper.mappers(); - this.indexSettings = docMapper.indexSettings(); - this.indexAnalyzers = docMapper.indexAnalyzers(); + this.mapping = mapping; + this.mappingLookup = mappingLookup; + this.indexSettings = indexSettings; + this.indexAnalyzers = indexAnalyzers; this.parserContextFunction = parserContextFunction; this.dynamicRuntimeFieldsBuilder = dynamicRuntimeFieldsBuilder; this.parser = parser; @@ -358,7 +361,7 @@ public InternalParseContext(DocumentMapper docMapper, this.documents.add(document); this.version = null; this.sourceToParse = source; - this.maxAllowedNumNestedDocs = docMapper.indexSettings().getMappingNestedDocsLimit(); + this.maxAllowedNumNestedDocs = indexSettings.getMappingNestedDocsLimit(); this.numNestedDocs = 0L; } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java index f491d605d11cb..b930ef6d5fa82 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java @@ -617,7 +617,9 @@ MapperService createMapperService() throws Exception { // creates an object mapper, which is about 100x harder than it should be.... ObjectMapper createObjectMapper(MapperService mapperService, String name) { - ParseContext context = new ParseContext.InternalParseContext(mapperService.documentMapper(), null, null, null, null); + DocumentMapper docMapper = mapperService.documentMapper(); + ParseContext context = new ParseContext.InternalParseContext(docMapper.mapping(), docMapper.mappers(), + mapperService.getIndexSettings(), mapperService.getIndexAnalyzers(), null, null, null, null); String[] nameParts = name.split("\\."); for (int i = 0; i < nameParts.length - 1; ++i) { context.path().add(nameParts[i]);