|
@@ -15,6 +15,7 @@ import org.elasticsearch.common.bytes.BytesArray;
|
|
|
import org.elasticsearch.common.bytes.BytesReference;
|
|
|
import org.elasticsearch.common.compress.CompressorFactory;
|
|
|
import org.elasticsearch.common.io.stream.BytesStreamOutput;
|
|
|
+import org.elasticsearch.core.Tuple;
|
|
|
import org.elasticsearch.xcontent.XContentBuilder;
|
|
|
import org.junit.AssumptionViolatedException;
|
|
|
|
|
@@ -22,8 +23,11 @@ import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Base64;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static org.hamcrest.Matchers.empty;
|
|
|
+import static org.hamcrest.Matchers.equalTo;
|
|
|
import static org.hamcrest.Matchers.instanceOf;
|
|
|
|
|
|
public class BinaryFieldMapperTests extends MapperTestCase {
|
|
@@ -152,9 +156,78 @@ public class BinaryFieldMapperTests extends MapperTestCase {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ protected boolean supportsCopyTo() {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed) {
|
|
|
- throw new AssumptionViolatedException("not supported");
|
|
|
+ return new SyntheticSourceSupport() {
|
|
|
+ @Override
|
|
|
+ public SyntheticSourceExample example(int maxValues) throws IOException {
|
|
|
+ if (randomBoolean()) {
|
|
|
+ var value = generateValue();
|
|
|
+ return new SyntheticSourceExample(value.v1(), value.v2(), this::mapping);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Tuple<String, byte[]>> values = randomList(1, maxValues, this::generateValue);
|
|
|
+
|
|
|
+ var in = values.stream().map(Tuple::v1).toList();
|
|
|
+
|
|
|
+ var outList = values.stream()
|
|
|
+ .map(Tuple::v2)
|
|
|
+ .map(BytesCompareUnsigned::new)
|
|
|
+ .collect(Collectors.toSet())
|
|
|
+ .stream()
|
|
|
+ .sorted()
|
|
|
+ .map(b -> encode(b.bytes))
|
|
|
+ .toList();
|
|
|
+ Object out = outList.size() == 1 ? outList.get(0) : outList;
|
|
|
+
|
|
|
+ return new SyntheticSourceExample(in, out, this::mapping);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<SyntheticSourceInvalidExample> invalidExample() throws IOException {
|
|
|
+ return List.of(
|
|
|
+ new SyntheticSourceInvalidExample(
|
|
|
+ equalTo("field [field] of type [binary] doesn't support synthetic source because it doesn't have doc values"),
|
|
|
+ b -> b.field("type", "binary")
|
|
|
+ ),
|
|
|
+ new SyntheticSourceInvalidExample(
|
|
|
+ equalTo("field [field] of type [binary] doesn't support synthetic source because it doesn't have doc values"),
|
|
|
+ b -> b.field("type", "binary").field("doc_values", false)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private Tuple<String, byte[]> generateValue() {
|
|
|
+ var len = randomIntBetween(1, 256);
|
|
|
+ var bytes = randomByteArrayOfLength(len);
|
|
|
+
|
|
|
+ return Tuple.tuple(encode(bytes), bytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String encode(byte[] bytes) {
|
|
|
+ return Base64.getEncoder().encodeToString(bytes);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void mapping(XContentBuilder b) throws IOException {
|
|
|
+ b.field("type", "binary").field("doc_values", "true");
|
|
|
+
|
|
|
+ if (rarely()) {
|
|
|
+ b.field("store", true);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private record BytesCompareUnsigned(byte[] bytes) implements Comparable<BytesCompareUnsigned> {
|
|
|
+ @Override
|
|
|
+ public int compareTo(BytesCompareUnsigned o) {
|
|
|
+ return Arrays.compareUnsigned(bytes, o.bytes);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
@Override
|