From 3fecaecb24cdbb24bf03d59d41d63bad3ff1b019 Mon Sep 17 00:00:00 2001 From: Felix Barnsteiner Date: Mon, 12 Feb 2024 08:34:52 +0100 Subject: [PATCH 1/3] Fix parsing of flattened fields within subobjects: false --- .../index/mapper/DocumentParser.java | 3 +- .../index/mapper/DynamicTemplatesTests.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) 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 fe6b0b2051dc9..917deb178a651 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -470,11 +470,12 @@ private static void parseObject(final DocumentParserContext context, String curr private static void doParseObject(DocumentParserContext context, String currentFieldName, Mapper objectMapper) throws IOException { context.path().add(currentFieldName); + boolean withinLeafObject = context.path().isWithinLeafObject(); if (objectMapper instanceof ObjectMapper objMapper && objMapper.subobjects() == false) { context.path().setWithinLeafObject(true); } parseObjectOrField(context, objectMapper); - context.path().setWithinLeafObject(false); + context.path().setWithinLeafObject(withinLeafObject); context.path().remove(); } diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java index a4b729e9a3170..e732f0def8c87 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java @@ -1797,6 +1797,53 @@ public void testSubobjectsFalseDocWithEmptyObject() throws IOException { assertFalse(leaf.subobjects()); } + public void testSubobjectsFalseFlattened() throws IOException { + String mapping = """ + { + "_doc": { + "properties": { + "attributes": { + "type": "object", + "subobjects": false + } + }, + "dynamic_templates": [ + { + "test": { + "path_match": "attributes.*", + "match_mapping_type": "object", + "mapping": { + "type": "flattened" + } + } + } + ] + } + } + """; + String docJson = """ + { + "attributes": { + "complex.attribute": { + "a": "b" + }, + "foo.bar": "baz" + } + } + """; + + MapperService mapperService = createMapperService(mapping); + ParsedDocument parsedDoc = mapperService.documentMapper().parse(source(docJson)); + merge(mapperService, dynamicMapping(parsedDoc.dynamicMappingsUpdate())); + + Mapper fooBarMapper = mapperService.documentMapper().mappers().getMapper("attributes.foo.bar"); + assertNotNull(fooBarMapper); + assertEquals("text", fooBarMapper.typeName()); + Mapper fooStructuredMapper = mapperService.documentMapper().mappers().getMapper("attributes.complex.attribute"); + assertNotNull(fooStructuredMapper); + assertEquals("flattened", fooStructuredMapper.typeName()); + } + public void testMatchWithArrayOfFieldNames() throws IOException { String mapping = """ { From b549823ebb720c3a1f01fb291aff567222a47df0 Mon Sep 17 00:00:00 2001 From: Felix Barnsteiner Date: Mon, 12 Feb 2024 08:57:16 +0100 Subject: [PATCH 2/3] Add another test case to DocumentParserTests --- .../index/mapper/DocumentParserTests.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) 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 ed2efb4728b8d..d3dd585788867 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java @@ -2273,6 +2273,39 @@ public void testSubobjectsFalseParentDynamicFalse() throws Exception { assertNull(doc.dynamicMappingsUpdate()); } + public void testSubobjectsFalseFlattened() throws Exception { + DocumentMapper mapper = createDocumentMapper(mapping(b -> { + b.startObject("attributes"); + { + b.field("dynamic", false); + b.field("subobjects", false); + b.startObject("properties"); + { + b.startObject("simple.attribute"); + b.field("type", "keyword"); + b.endObject(); + b.startObject("complex.attribute"); + b.field("type", "flattened"); + b.endObject(); + } + b.endObject(); + } + b.endObject(); + })); + ParsedDocument doc = mapper.parse(source(""" + { + "attributes": { + "complex.attribute": { + "foo" : "bar" + }, + "simple.attribute": "foo" + } + } + """)); + assertNotNull(doc.rootDoc().getField("attributes.complex.attribute")); + assertNotNull(doc.rootDoc().getField("attributes.simple.attribute")); + } + public void testWriteToFieldAlias() throws Exception { DocumentMapper mapper = createDocumentMapper(mapping(b -> { b.startObject("alias-field"); From d2a26f5459a2d366388c4a40f51f6ba0911efc3d Mon Sep 17 00:00:00 2001 From: Felix Barnsteiner Date: Mon, 12 Feb 2024 09:04:27 +0100 Subject: [PATCH 3/3] Update docs/changelog/105373.yaml --- docs/changelog/105373.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 docs/changelog/105373.yaml diff --git a/docs/changelog/105373.yaml b/docs/changelog/105373.yaml new file mode 100644 index 0000000000000..f9d3c718f7ae3 --- /dev/null +++ b/docs/changelog/105373.yaml @@ -0,0 +1,5 @@ +pr: 105373 +summary: "Fix parsing of flattened fields within subobjects: false" +area: Mapping +type: bug +issues: []