Skip to content
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
5 changes: 5 additions & 0 deletions docs/changelog/105373.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 105373
summary: "Fix parsing of flattened fields within subobjects: false"
area: Mapping
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
{
Expand Down