Skip to content

Commit 6126468

Browse files
authored
Fix parsing of flattened fields within subobjects: false (#105373)
1 parent 67e36f1 commit 6126468

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed

docs/changelog/105373.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 105373
2+
summary: "Fix parsing of flattened fields within subobjects: false"
3+
area: Mapping
4+
type: bug
5+
issues: []

server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,12 @@ private static void parseObject(final DocumentParserContext context, String curr
455455

456456
private static void doParseObject(DocumentParserContext context, String currentFieldName, Mapper objectMapper) throws IOException {
457457
context.path().add(currentFieldName);
458+
boolean withinLeafObject = context.path().isWithinLeafObject();
458459
if (objectMapper instanceof ObjectMapper objMapper && objMapper.subobjects() == false) {
459460
context.path().setWithinLeafObject(true);
460461
}
461462
parseObjectOrField(context, objectMapper);
462-
context.path().setWithinLeafObject(false);
463+
context.path().setWithinLeafObject(withinLeafObject);
463464
context.path().remove();
464465
}
465466

server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2273,6 +2273,39 @@ public void testSubobjectsFalseParentDynamicFalse() throws Exception {
22732273
assertNull(doc.dynamicMappingsUpdate());
22742274
}
22752275

2276+
public void testSubobjectsFalseFlattened() throws Exception {
2277+
DocumentMapper mapper = createDocumentMapper(mapping(b -> {
2278+
b.startObject("attributes");
2279+
{
2280+
b.field("dynamic", false);
2281+
b.field("subobjects", false);
2282+
b.startObject("properties");
2283+
{
2284+
b.startObject("simple.attribute");
2285+
b.field("type", "keyword");
2286+
b.endObject();
2287+
b.startObject("complex.attribute");
2288+
b.field("type", "flattened");
2289+
b.endObject();
2290+
}
2291+
b.endObject();
2292+
}
2293+
b.endObject();
2294+
}));
2295+
ParsedDocument doc = mapper.parse(source("""
2296+
{
2297+
"attributes": {
2298+
"complex.attribute": {
2299+
"foo" : "bar"
2300+
},
2301+
"simple.attribute": "foo"
2302+
}
2303+
}
2304+
"""));
2305+
assertNotNull(doc.rootDoc().getField("attributes.complex.attribute"));
2306+
assertNotNull(doc.rootDoc().getField("attributes.simple.attribute"));
2307+
}
2308+
22762309
public void testWriteToFieldAlias() throws Exception {
22772310
DocumentMapper mapper = createDocumentMapper(mapping(b -> {
22782311
b.startObject("alias-field");

server/src/test/java/org/elasticsearch/index/mapper/DynamicTemplatesTests.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,53 @@ public void testSubobjectsFalseDocWithEmptyObject() throws IOException {
18071807
assertFalse(leaf.subobjects());
18081808
}
18091809

1810+
public void testSubobjectsFalseFlattened() throws IOException {
1811+
String mapping = """
1812+
{
1813+
"_doc": {
1814+
"properties": {
1815+
"attributes": {
1816+
"type": "object",
1817+
"subobjects": false
1818+
}
1819+
},
1820+
"dynamic_templates": [
1821+
{
1822+
"test": {
1823+
"path_match": "attributes.*",
1824+
"match_mapping_type": "object",
1825+
"mapping": {
1826+
"type": "flattened"
1827+
}
1828+
}
1829+
}
1830+
]
1831+
}
1832+
}
1833+
""";
1834+
String docJson = """
1835+
{
1836+
"attributes": {
1837+
"complex.attribute": {
1838+
"a": "b"
1839+
},
1840+
"foo.bar": "baz"
1841+
}
1842+
}
1843+
""";
1844+
1845+
MapperService mapperService = createMapperService(mapping);
1846+
ParsedDocument parsedDoc = mapperService.documentMapper().parse(source(docJson));
1847+
merge(mapperService, dynamicMapping(parsedDoc.dynamicMappingsUpdate()));
1848+
1849+
Mapper fooBarMapper = mapperService.documentMapper().mappers().getMapper("attributes.foo.bar");
1850+
assertNotNull(fooBarMapper);
1851+
assertEquals("text", fooBarMapper.typeName());
1852+
Mapper fooStructuredMapper = mapperService.documentMapper().mappers().getMapper("attributes.complex.attribute");
1853+
assertNotNull(fooStructuredMapper);
1854+
assertEquals("flattened", fooStructuredMapper.typeName());
1855+
}
1856+
18101857
public void testMatchWithArrayOfFieldNames() throws IOException {
18111858
String mapping = """
18121859
{

0 commit comments

Comments
 (0)