Skip to content

Commit

Permalink
Fix parsing of flattened fields within subobjects: false (elastic#105373
Browse files Browse the repository at this point in the history
)
  • Loading branch information
felixbarny committed Feb 20, 2024
1 parent b1d7b2e commit b485247
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/changelog/105373.yaml
@@ -0,0 +1,5 @@
pr: 105373
summary: "Fix parsing of flattened fields within subobjects: false"
area: Mapping
type: bug
issues: []
Expand Up @@ -455,11 +455,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
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
Expand Up @@ -1807,6 +1807,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

0 comments on commit b485247

Please sign in to comment.