|
@@ -33,7 +33,11 @@ import java.io.IOException;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
@@ -196,7 +200,66 @@ public class TokenCountFieldMapperTests extends MapperTestCase {
|
|
|
|
|
|
@Override
|
|
|
protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed) {
|
|
|
- throw new AssumptionViolatedException("not supported");
|
|
|
+ assertFalse(ignoreMalformed);
|
|
|
+
|
|
|
+ var nullValue = usually() ? null : randomNonNegativeInt();
|
|
|
+ return new SyntheticSourceSupport() {
|
|
|
+ @Override
|
|
|
+ public boolean preservesExactSource() {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public SyntheticSourceExample example(int maxValues) {
|
|
|
+ if (randomBoolean()) {
|
|
|
+ var value = generateValue();
|
|
|
+ return new SyntheticSourceExample(value.text, value.text, value.tokenCount, this::mapping);
|
|
|
+ }
|
|
|
+
|
|
|
+ var values = randomList(1, 5, this::generateValue);
|
|
|
+
|
|
|
+ var textArray = values.stream().map(Value::text).toList();
|
|
|
+
|
|
|
+ var blockExpectedList = values.stream().map(Value::tokenCount).filter(Objects::nonNull).toList();
|
|
|
+ var blockExpected = blockExpectedList.size() == 1 ? blockExpectedList.get(0) : blockExpectedList;
|
|
|
+
|
|
|
+ return new SyntheticSourceExample(textArray, textArray, blockExpected, this::mapping);
|
|
|
+ }
|
|
|
+
|
|
|
+ private record Value(String text, Integer tokenCount) {}
|
|
|
+
|
|
|
+ private Value generateValue() {
|
|
|
+ if (rarely()) {
|
|
|
+ return new Value(null, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ var text = randomList(0, 10, () -> randomAlphaOfLengthBetween(0, 10)).stream().collect(Collectors.joining(" "));
|
|
|
+ // with keyword analyzer token count is always 1
|
|
|
+ return new Value(text, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void mapping(XContentBuilder b) throws IOException {
|
|
|
+ b.field("type", "token_count").field("analyzer", "keyword");
|
|
|
+ if (rarely()) {
|
|
|
+ b.field("index", false);
|
|
|
+ }
|
|
|
+ if (rarely()) {
|
|
|
+ b.field("store", true);
|
|
|
+ }
|
|
|
+ if (nullValue != null) {
|
|
|
+ b.field("null_value", nullValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<SyntheticSourceInvalidExample> invalidExample() throws IOException {
|
|
|
+ return List.of();
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ protected Function<Object, Object> loadBlockExpected() {
|
|
|
+ // we can get either a number from doc values or null
|
|
|
+ return v -> v != null ? (Number) v : null;
|
|
|
}
|
|
|
|
|
|
@Override
|