Skip to content

Commit

Permalink
PassThroughObject nests aliases within objects for fields with dotted…
Browse files Browse the repository at this point in the history
… names (#105298)

* Ignore duplicate FieldAliasMappers during building

* Update docs/changelog/105298.yaml

* revert

* nest alias within objects

* Delete docs/changelog/105298.yaml
  • Loading branch information
kkrik-es authored Feb 9, 2024
1 parent 8c9c24c commit f65312e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ protected static void parseProperties(
throw new MapperParsingException("No handler for type [" + type + "] declared on field [" + fieldName + "]");
}
Mapper.Builder fieldBuilder;
if (objBuilder.subobjects.value() == false || type.equals(FieldAliasMapper.CONTENT_TYPE)) {
if (objBuilder.subobjects.value() == false) {
fieldBuilder = typeParser.parse(fieldName, propNode, parserContext);
} else {
String[] fieldNameParts = fieldName.split("\\.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,34 @@ void getAliasMappers(Map<String, Mapper> mappers, Map<String, Mapper> aliasMappe
);
}
} else {
FieldAliasMapper aliasMapper = new FieldAliasMapper.Builder(fieldMapper.simpleName()).path(
fieldMapper.mappedFieldType.name()
).build(context);
aliasMappers.put(aliasMapper.simpleName(), aliasMapper);
// Check if the field name contains dots, as aliases require nesting within objects in this case.
String[] fieldNameParts = fieldMapper.simpleName().split("\\.");
if (fieldNameParts.length == 0) {
throw new IllegalArgumentException("field name cannot contain only dots");
}
if (fieldNameParts.length == 1) {
// No nesting required, add the alias directly to the root.
FieldAliasMapper aliasMapper = new FieldAliasMapper.Builder(fieldMapper.simpleName()).path(
fieldMapper.mappedFieldType.name()
).build(context);
aliasMappers.put(aliasMapper.simpleName(), aliasMapper);
} else {
// Nest the alias within object(s).
String realFieldName = fieldNameParts[fieldNameParts.length - 1];
Mapper.Builder fieldBuilder = new FieldAliasMapper.Builder(realFieldName).path(
fieldMapper.mappedFieldType.name()
);
for (int i = fieldNameParts.length - 2; i >= 0; --i) {
String intermediateObjectName = fieldNameParts[i];
ObjectMapper.Builder intermediate = new ObjectMapper.Builder(
intermediateObjectName,
ObjectMapper.Defaults.SUBOBJECTS
);
intermediate.add(fieldBuilder);
fieldBuilder = intermediate;
}
aliasMappers.put(fieldNameParts[0], fieldBuilder.build(context));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public void testPassThroughObjectNested() throws IOException {
}));
assertThat(mapperService.mappingLookup().getMapper("dim"), instanceOf(FieldAliasMapper.class));
assertThat(mapperService.mappingLookup().getMapper("resource.attributes.dim"), instanceOf(KeywordFieldMapper.class));
assertThat(mapperService.mappingLookup().getMapper("another.dim"), instanceOf(FieldAliasMapper.class));
assertThat(mapperService.mappingLookup().objectMappers().get("another").getMapper("dim"), instanceOf(FieldAliasMapper.class));
assertThat(mapperService.mappingLookup().getMapper("attributes.another.dim"), instanceOf(KeywordFieldMapper.class));
}

Expand Down

0 comments on commit f65312e

Please sign in to comment.