Check prefixes when constructing synthetic source for flattened fields (#129580)

* Check prefixes when constructing synthetic source for flattened fields

* Update docs/changelog/129580.yaml
This commit is contained in:
Kostas Krikellas 2025-06-18 11:13:53 +03:00 committed by GitHub
parent 9e40bb3845
commit b25d7b9471
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View file

@ -0,0 +1,6 @@
pr: 129580
summary: Check prefixes when constructing synthetic source for flattened fields
area: Mapping
type: bug
issues:
- 129508

View file

@ -261,7 +261,7 @@ public class FlattenedFieldSyntheticWriterHelper {
final List<String> values final List<String> values
) throws IOException { ) throws IOException {
startObject(b, startPrefix.prefix); startObject(b, startPrefix.prefix);
if (currKeyValue.suffix.equals(nextKeyValue.suffix) == false) { if (currKeyValue.prefix.equals(nextKeyValue.prefix) == false || currKeyValue.suffix.equals(nextKeyValue.suffix) == false) {
writeNestedObject(b, values, currKeyValue.suffix().suffix); writeNestedObject(b, values, currKeyValue.suffix().suffix);
} }
endObject(b, endPrefix.prefix); endObject(b, endPrefix.prefix);

View file

@ -851,6 +851,23 @@ public class FlattenedFieldMapperTests extends MapperTestCase {
assertThat(syntheticSource, equalTo("{\"field\":{\"key1\":\"val1\",\"obj1\":{\"key2\":\"val2\",\"key3\":[\"val3\",\"val4\"]}}}")); assertThat(syntheticSource, equalTo("{\"field\":{\"key1\":\"val1\",\"obj1\":{\"key2\":\"val2\",\"key3\":[\"val3\",\"val4\"]}}}"));
} }
public void testSyntheticSourceWithCommonLeafField() throws IOException {
DocumentMapper mapper = createSytheticSourceMapperService(
mapping(b -> { b.startObject("field").field("type", "flattened").endObject(); })
).documentMapper();
var syntheticSource = syntheticSource(mapper, b -> {
b.startObject("field");
{
b.startObject("obj1").field("key", "foo").endObject();
b.startObject("obj2").field("key", "bar").endObject();
}
b.endObject();
});
assertThat(syntheticSource, equalTo("""
{"field":{"obj1":{"key":"foo"},"obj2":{"key":"bar"}}}"""));
}
@Override @Override
protected boolean supportsCopyTo() { protected boolean supportsCopyTo() {
return false; return false;