|
|
@@ -9,6 +9,8 @@
|
|
|
|
|
|
package org.elasticsearch.index.mapper;
|
|
|
|
|
|
+import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
|
|
|
+
|
|
|
import org.apache.lucene.index.DirectoryReader;
|
|
|
import org.apache.lucene.index.LeafReaderContext;
|
|
|
import org.apache.lucene.store.Directory;
|
|
|
@@ -40,13 +42,33 @@ import java.util.Set;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
public abstract class BlockLoaderTestCase extends MapperServiceTestCase {
|
|
|
+ private static final MappedFieldType.FieldExtractPreference[] PREFERENCES = new MappedFieldType.FieldExtractPreference[] {
|
|
|
+ MappedFieldType.FieldExtractPreference.NONE,
|
|
|
+ MappedFieldType.FieldExtractPreference.STORED };
|
|
|
+
|
|
|
+ @ParametersFactory(argumentFormatting = "preference=%s")
|
|
|
+ public static List<Object[]> args() {
|
|
|
+ List<Object[]> args = new ArrayList<>();
|
|
|
+ for (boolean syntheticSource : new boolean[] { false, true }) {
|
|
|
+ for (MappedFieldType.FieldExtractPreference preference : PREFERENCES) {
|
|
|
+ args.add(new Object[] { new Params(syntheticSource, preference) });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return args;
|
|
|
+ }
|
|
|
+
|
|
|
+ public record Params(boolean syntheticSource, MappedFieldType.FieldExtractPreference preference) {}
|
|
|
+
|
|
|
private final FieldType fieldType;
|
|
|
+ protected final Params params;
|
|
|
+
|
|
|
private final String fieldName;
|
|
|
private final MappingGenerator mappingGenerator;
|
|
|
private final DocumentGenerator documentGenerator;
|
|
|
|
|
|
- protected BlockLoaderTestCase(FieldType fieldType) {
|
|
|
+ protected BlockLoaderTestCase(FieldType fieldType, Params params) {
|
|
|
this.fieldType = fieldType;
|
|
|
+ this.params = params;
|
|
|
this.fieldName = randomAlphaOfLengthBetween(5, 10);
|
|
|
|
|
|
var specification = DataGeneratorSpecification.builder()
|
|
|
@@ -83,10 +105,9 @@ public abstract class BlockLoaderTestCase extends MapperServiceTestCase {
|
|
|
|
|
|
public void testBlockLoader() throws IOException {
|
|
|
var template = new Template(Map.of(fieldName, new Template.Leaf(fieldName, fieldType)));
|
|
|
- var syntheticSource = randomBoolean();
|
|
|
var mapping = mappingGenerator.generate(template);
|
|
|
|
|
|
- runTest(template, mapping, syntheticSource, fieldName);
|
|
|
+ runTest(template, mapping, fieldName);
|
|
|
}
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
@@ -110,34 +131,34 @@ public abstract class BlockLoaderTestCase extends MapperServiceTestCase {
|
|
|
currentLevel.put(fieldName, new Template.Leaf(fieldName, fieldType));
|
|
|
var template = new Template(top);
|
|
|
|
|
|
- var syntheticSource = randomBoolean();
|
|
|
-
|
|
|
var mapping = mappingGenerator.generate(template);
|
|
|
|
|
|
- if (syntheticSource && randomBoolean()) {
|
|
|
+ if (params.syntheticSource && randomBoolean()) {
|
|
|
// force fallback synthetic source in the hierarchy
|
|
|
var docMapping = (Map<String, Object>) mapping.raw().get("_doc");
|
|
|
var topLevelMapping = (Map<String, Object>) ((Map<String, Object>) docMapping.get("properties")).get("top");
|
|
|
topLevelMapping.put("synthetic_source_keep", "all");
|
|
|
}
|
|
|
|
|
|
- runTest(template, mapping, syntheticSource, fullFieldName.toString());
|
|
|
+ runTest(template, mapping, fullFieldName.toString());
|
|
|
}
|
|
|
|
|
|
- private void runTest(Template template, Mapping mapping, boolean syntheticSource, String fieldName) throws IOException {
|
|
|
+ private void runTest(Template template, Mapping mapping, String fieldName) throws IOException {
|
|
|
var mappingXContent = XContentBuilder.builder(XContentType.JSON.xContent()).map(mapping.raw());
|
|
|
|
|
|
- var mapperService = syntheticSource ? createSytheticSourceMapperService(mappingXContent) : createMapperService(mappingXContent);
|
|
|
+ var mapperService = params.syntheticSource
|
|
|
+ ? createSytheticSourceMapperService(mappingXContent)
|
|
|
+ : createMapperService(mappingXContent);
|
|
|
|
|
|
var document = documentGenerator.generate(template, mapping);
|
|
|
var documentXContent = XContentBuilder.builder(XContentType.JSON.xContent()).map(document);
|
|
|
|
|
|
Object blockLoaderResult = setupAndInvokeBlockLoader(mapperService, documentXContent, fieldName);
|
|
|
- Object expected = expected(mapping.lookup().get(fieldName), getFieldValue(document, fieldName), syntheticSource);
|
|
|
+ Object expected = expected(mapping.lookup().get(fieldName), getFieldValue(document, fieldName));
|
|
|
assertEquals(expected, blockLoaderResult);
|
|
|
}
|
|
|
|
|
|
- protected abstract Object expected(Map<String, Object> fieldMapping, Object value, boolean syntheticSource);
|
|
|
+ protected abstract Object expected(Map<String, Object> fieldMapping, Object value);
|
|
|
|
|
|
private Object getFieldValue(Map<String, Object> document, String fieldName) {
|
|
|
var rawValues = new ArrayList<>();
|
|
|
@@ -258,8 +279,7 @@ public abstract class BlockLoaderTestCase extends MapperServiceTestCase {
|
|
|
|
|
|
@Override
|
|
|
public MappedFieldType.FieldExtractPreference fieldExtractPreference() {
|
|
|
- // TODO randomize when adding support for fields that care about this
|
|
|
- return MappedFieldType.FieldExtractPreference.NONE;
|
|
|
+ return params.preference;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -283,4 +303,8 @@ public abstract class BlockLoaderTestCase extends MapperServiceTestCase {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ protected static boolean hasDocValues(Map<String, Object> fieldMapping, boolean defaultValue) {
|
|
|
+ return (boolean) fieldMapping.getOrDefault("doc_values", defaultValue);
|
|
|
+ }
|
|
|
}
|