|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.mapper; |
| 11 | + |
| 12 | +import org.elasticsearch.logsdb.datageneration.FieldType; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +public abstract class NumberFieldBlockLoaderTestCase<T extends Number> extends BlockLoaderTestCase { |
| 19 | + public NumberFieldBlockLoaderTestCase(FieldType fieldType) { |
| 20 | + super(fieldType); |
| 21 | + } |
| 22 | + |
| 23 | + @Override |
| 24 | + @SuppressWarnings("unchecked") |
| 25 | + protected Object expected(Map<String, Object> fieldMapping, Object value, boolean syntheticSource) { |
| 26 | + var nullValue = fieldMapping.get("null_value") != null ? convertNullValue((Number) fieldMapping.get("null_value")) : null; |
| 27 | + |
| 28 | + if (value instanceof List<?> == false) { |
| 29 | + return convert(value, nullValue); |
| 30 | + } |
| 31 | + |
| 32 | + if ((boolean) fieldMapping.getOrDefault("doc_values", false)) { |
| 33 | + // Sorted and no duplicates |
| 34 | + var resultList = ((List<Object>) value).stream().map(v -> convert(v, nullValue)).filter(Objects::nonNull).sorted().toList(); |
| 35 | + return maybeFoldList(resultList); |
| 36 | + } |
| 37 | + |
| 38 | + // parsing from source |
| 39 | + var resultList = ((List<Object>) value).stream().map(v -> convert(v, nullValue)).filter(Objects::nonNull).toList(); |
| 40 | + return maybeFoldList(resultList); |
| 41 | + } |
| 42 | + |
| 43 | + @SuppressWarnings("unchecked") |
| 44 | + private T convert(Object value, T nullValue) { |
| 45 | + if (value == null) { |
| 46 | + return nullValue; |
| 47 | + } |
| 48 | + // String coercion is true by default |
| 49 | + if (value instanceof String s && s.isEmpty()) { |
| 50 | + return nullValue; |
| 51 | + } |
| 52 | + if (value instanceof Number n) { |
| 53 | + return convert(n); |
| 54 | + } |
| 55 | + |
| 56 | + // Malformed values are excluded |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + protected T convertNullValue(Number nullValue) { |
| 61 | + return convert(nullValue); |
| 62 | + } |
| 63 | + |
| 64 | + protected abstract T convert(Number value); |
| 65 | +} |
0 commit comments