|
@@ -21,6 +21,7 @@ import java.io.IOException;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
import static org.mockito.Mockito.mock;
|
|
|
import static org.mockito.Mockito.when;
|
|
@@ -246,6 +247,54 @@ public class FlattenedFieldSyntheticWriterHelperTests extends ESTestCase {
|
|
|
assertEquals("{\"a\":{\"b\":{\"c\":\"10\",\"c.d\":\"20\"}}}", baos.toString(StandardCharsets.UTF_8));
|
|
|
}
|
|
|
|
|
|
+ public void testSingleDotPath() throws IOException {
|
|
|
+ // GIVEN
|
|
|
+ final SortedSetDocValues dv = mock(SortedSetDocValues.class);
|
|
|
+ final FlattenedFieldSyntheticWriterHelper writer = new FlattenedFieldSyntheticWriterHelper(new SortedSetSortedKeyedValues(dv));
|
|
|
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ final XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), baos);
|
|
|
+ final List<byte[]> bytes = Stream.of("." + '\0' + "10").map(x -> x.getBytes(StandardCharsets.UTF_8)).toList();
|
|
|
+ when(dv.getValueCount()).thenReturn(Long.valueOf(bytes.size()));
|
|
|
+ when(dv.docValueCount()).thenReturn(bytes.size());
|
|
|
+ for (int i = 0; i < bytes.size(); i++) {
|
|
|
+ when(dv.nextOrd()).thenReturn((long) i);
|
|
|
+ when(dv.lookupOrd(ArgumentMatchers.eq((long) i))).thenReturn(new BytesRef(bytes.get(i), 0, bytes.get(i).length));
|
|
|
+ }
|
|
|
+
|
|
|
+ // WHEN
|
|
|
+ builder.startObject();
|
|
|
+ writer.write(builder);
|
|
|
+ builder.endObject();
|
|
|
+ builder.flush();
|
|
|
+
|
|
|
+ // THEN
|
|
|
+ assertEquals("{\"\":{\"\":\"10\"}}", baos.toString(StandardCharsets.UTF_8));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testTrailingDotsPath() throws IOException {
|
|
|
+ // GIVEN
|
|
|
+ final SortedSetDocValues dv = mock(SortedSetDocValues.class);
|
|
|
+ final FlattenedFieldSyntheticWriterHelper writer = new FlattenedFieldSyntheticWriterHelper(new SortedSetSortedKeyedValues(dv));
|
|
|
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
+ final XContentBuilder builder = new XContentBuilder(XContentType.JSON.xContent(), baos);
|
|
|
+ final List<byte[]> bytes = Stream.of("cat.." + '\0' + "10").map(x -> x.getBytes(StandardCharsets.UTF_8)).toList();
|
|
|
+ when(dv.getValueCount()).thenReturn(Long.valueOf(bytes.size()));
|
|
|
+ when(dv.docValueCount()).thenReturn(bytes.size());
|
|
|
+ for (int i = 0; i < bytes.size(); i++) {
|
|
|
+ when(dv.nextOrd()).thenReturn((long) i);
|
|
|
+ when(dv.lookupOrd(ArgumentMatchers.eq((long) i))).thenReturn(new BytesRef(bytes.get(i), 0, bytes.get(i).length));
|
|
|
+ }
|
|
|
+
|
|
|
+ // WHEN
|
|
|
+ builder.startObject();
|
|
|
+ writer.write(builder);
|
|
|
+ builder.endObject();
|
|
|
+ builder.flush();
|
|
|
+
|
|
|
+ // THEN
|
|
|
+ assertEquals("{\"cat\":{\"\":{\"\":\"10\"}}}", baos.toString(StandardCharsets.UTF_8));
|
|
|
+ }
|
|
|
+
|
|
|
private class SortedSetSortedKeyedValues implements FlattenedFieldSyntheticWriterHelper.SortedKeyedValues {
|
|
|
private final SortedSetDocValues dv;
|
|
|
private int seen = 0;
|