From 92dc76ee22637747ec8577a89f57f11bc302b9ec Mon Sep 17 00:00:00 2001 From: eyalkoren <41850454+eyalkoren@users.noreply.github.com> Date: Tue, 28 May 2024 17:24:09 +0300 Subject: [PATCH] Raw mapping merge fix for properties field (#108867) --- docs/changelog/108867.yaml | 6 ++ .../common/xcontent/XContentHelper.java | 2 +- .../index/mapper/MapperService.java | 7 +- .../index/mapper/MapperServiceTests.java | 99 ++++++++++++++++++- 4 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 docs/changelog/108867.yaml diff --git a/docs/changelog/108867.yaml b/docs/changelog/108867.yaml new file mode 100644 index 0000000000000..545349dd84aeb --- /dev/null +++ b/docs/changelog/108867.yaml @@ -0,0 +1,6 @@ +pr: 108867 +summary: Fix for raw mapping merge of fields named "properties" +area: Mapping +type: bug +issues: + - 108866 diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index 2227c54871352..28df2fad32cbb 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -415,7 +415,7 @@ public static void mergeDefaults(Map content, Map {})); + mapperService.merge("_doc", List.of(mapping1, mapping2), MergeReason.INDEX_TEMPLATE); + assertEquals(""" + { + "_doc" : { + "properties" : { + "properties" : { + "properties" : { + "child1" : { + "type" : "text", + "fields" : { + "keyword" : { + "type" : "keyword" + } + } + }, + "child2" : { + "type" : "integer" + }, + "child3" : { + "properties" : { + "grandchild" : { + "type" : "long" + } + } + } + } + } + } + } + }""", Strings.toString(mapperService.documentMapper().mapping(), true, true)); + + Mapper propertiesMapper = mapperService.documentMapper().mapping().getRoot().getMapper("properties"); + assertThat(propertiesMapper, instanceOf(ObjectMapper.class)); + Mapper childMapper = ((ObjectMapper) propertiesMapper).getMapper("child1"); + assertThat(childMapper, instanceOf(FieldMapper.class)); + assertEquals("text", childMapper.typeName()); + assertEquals(2, childMapper.getTotalFieldsCount()); + childMapper = ((ObjectMapper) propertiesMapper).getMapper("child2"); + assertThat(childMapper, instanceOf(FieldMapper.class)); + assertEquals("integer", childMapper.typeName()); + assertEquals(1, childMapper.getTotalFieldsCount()); + childMapper = ((ObjectMapper) propertiesMapper).getMapper("child3"); + assertThat(childMapper, instanceOf(ObjectMapper.class)); + Mapper grandchildMapper = ((ObjectMapper) childMapper).getMapper("grandchild"); + assertEquals("long", grandchildMapper.typeName()); + } + public void testMergeUntilLimit() throws IOException { CompressedXContent mapping1 = new CompressedXContent(""" {