Skip to content

Commit 9203140

Browse files
authored
Check prefixes when constructing synthetic source for flattened fields (#129580) (#129610)
* Check prefixes when constructing synthetic source for flattened fields * Update docs/changelog/129580.yaml
1 parent d448374 commit 9203140

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

docs/changelog/129580.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 129580
2+
summary: Check prefixes when constructing synthetic source for flattened fields
3+
area: Mapping
4+
type: bug
5+
issues:
6+
- 129508

server/src/main/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldSyntheticWriterHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ private static void writeObject(
261261
final List<String> values
262262
) throws IOException {
263263
startObject(b, startPrefix.prefix);
264-
if (currKeyValue.suffix.equals(nextKeyValue.suffix) == false) {
264+
if (currKeyValue.prefix.equals(nextKeyValue.prefix) == false || currKeyValue.suffix.equals(nextKeyValue.suffix) == false) {
265265
writeNestedObject(b, values, currKeyValue.suffix().suffix);
266266
}
267267
endObject(b, endPrefix.prefix);

server/src/test/java/org/elasticsearch/index/mapper/flattened/FlattenedFieldMapperTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,23 @@ public void testSyntheticSourceWithOnlyIgnoredValues() throws IOException {
851851
assertThat(syntheticSource, equalTo("{\"field\":{\"key1\":\"val1\",\"obj1\":{\"key2\":\"val2\",\"key3\":[\"val3\",\"val4\"]}}}"));
852852
}
853853

854+
public void testSyntheticSourceWithCommonLeafField() throws IOException {
855+
DocumentMapper mapper = createSytheticSourceMapperService(
856+
mapping(b -> { b.startObject("field").field("type", "flattened").endObject(); })
857+
).documentMapper();
858+
859+
var syntheticSource = syntheticSource(mapper, b -> {
860+
b.startObject("field");
861+
{
862+
b.startObject("obj1").field("key", "foo").endObject();
863+
b.startObject("obj2").field("key", "bar").endObject();
864+
}
865+
b.endObject();
866+
});
867+
assertThat(syntheticSource, equalTo("""
868+
{"field":{"obj1":{"key":"foo"},"obj2":{"key":"bar"}}}"""));
869+
}
870+
854871
@Override
855872
protected boolean supportsCopyTo() {
856873
return false;

0 commit comments

Comments
 (0)