Skip to content

Commit 2368c11

Browse files
committed
iter
1 parent 15330c0 commit 2368c11

File tree

8 files changed

+112
-49
lines changed

8 files changed

+112
-49
lines changed

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapperTests.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ public SyntheticSourceExample example(int maxVals) {
377377
return new SyntheticSourceExample(
378378
example.expectedForSyntheticSource(),
379379
example.expectedForSyntheticSource(),
380-
example.expectedForBlockLoader(),
381380
example.mapping()
382381
);
383382
}
@@ -400,7 +399,7 @@ public SyntheticSourceExample example(int maxValues) {
400399
if (v.malformedOutput == null) {
401400
return new SyntheticSourceExample(v.input, v.output, this::mapping);
402401
}
403-
return new SyntheticSourceExample(v.input, v.malformedOutput, null, this::mapping);
402+
return new SyntheticSourceExample(v.input, v.malformedOutput, this::mapping);
404403
}
405404
List<Value> values = randomList(1, maxValues, this::generateValue);
406405
List<Object> in = values.stream().map(Value::input).toList();
@@ -479,11 +478,6 @@ public List<SyntheticSourceInvalidExample> invalidExample() throws IOException {
479478
}
480479
}
481480

482-
protected BlockReaderSupport getSupportedReaders(MapperService mapper, String loaderFieldName) {
483-
assumeTrue("Disabled, tested by ScaledFloatFieldBlockLoaderTests instead", false);
484-
return null;
485-
}
486-
487481
@Override
488482
protected IngestScriptSupport ingestScriptSupport() {
489483
throw new AssumptionViolatedException("not supported");

server/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,9 +1032,13 @@ protected String delegatingTo() {
10321032
return new BlockStoredFieldsReader.BytesFromStringsBlockLoader(name());
10331033
}
10341034

1035-
// _ignored_source field will only be present if text field is not stored
1036-
// and there is no syntheticSourceDelegate
1037-
if (isSyntheticSource && syntheticSourceDelegate == null) {
1035+
// _ignored_source field will contain entries for this field if it is not stored
1036+
// and there is no syntheticSourceDelegate.
1037+
// See #syntheticSourceSupport().
1038+
// But if a text field is a multi field it won't have an entry in _ignored_source.
1039+
// The parent might, but we don't have enough context here to figure this out.
1040+
// So we bail.
1041+
if (isSyntheticSource && syntheticSourceDelegate == null && parentField == null) {
10381042
return fallbackSyntheticSourceBlockLoader();
10391043
}
10401044

server/src/test/java/org/elasticsearch/index/mapper/TextFieldMapperTests.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,35 +1314,4 @@ public void testEmpty() throws Exception {
13141314
assertFalse(dv.advanceExact(3));
13151315
});
13161316
}
1317-
1318-
// public void testBlockLoaderFromParentColumnReader() throws IOException {
1319-
// testBlockLoaderFromParent(true, randomBoolean());
1320-
// }
1321-
//
1322-
// public void testBlockLoaderParentFromRowStrideReader() throws IOException {
1323-
// testBlockLoaderFromParent(false, randomBoolean());
1324-
// }
1325-
//
1326-
// private void testBlockLoaderFromParent(boolean columnReader, boolean syntheticSource) throws IOException {
1327-
// boolean storeParent = randomBoolean();
1328-
// KeywordFieldSyntheticSourceSupport kwdSupport = new KeywordFieldSyntheticSourceSupport(null, storeParent, null, false);
1329-
// SyntheticSourceExample example = kwdSupport.example(5);
1330-
// CheckedConsumer<XContentBuilder, IOException> buildFields = b -> {
1331-
// b.startObject("field");
1332-
// {
1333-
// example.mapping().accept(b);
1334-
// b.startObject("fields").startObject("sub");
1335-
// {
1336-
// b.field("type", "text");
1337-
// }
1338-
// b.endObject().endObject();
1339-
// }
1340-
// b.endObject();
1341-
// };
1342-
// XContentBuilder mapping = mapping(buildFields);
1343-
// MapperService mapper = syntheticSource ? createSytheticSourceMapperService(mapping) : createMapperService(mapping);
1344-
// BlockReaderSupport blockReaderSupport = getSupportedReaders(mapper, "field.sub");
1345-
// var sourceLoader = mapper.mappingLookup().newSourceLoader(null, SourceFieldMetrics.NOOP);
1346-
// testBlockLoader(columnReader, example, blockReaderSupport, sourceLoader);
1347-
// }
13481317
}

server/src/test/java/org/elasticsearch/index/mapper/blockloader/TextFieldBlockLoaderTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ public TextFieldBlockLoaderTests(Params params) {
2525
super(FieldType.TEXT.toString(), params);
2626
}
2727

28-
@SuppressWarnings("unchecked")
2928
@Override
3029
protected Object expected(Map<String, Object> fieldMapping, Object value, TestContext testContext) {
30+
return expectedValue(fieldMapping, value, params, testContext);
31+
}
32+
33+
@SuppressWarnings("unchecked")
34+
public static Object expectedValue(Map<String, Object> fieldMapping, Object value, Params params, TestContext testContext) {
3135
if (fieldMapping.getOrDefault("store", false).equals(true)) {
3236
return valuesInSourceOrder(value);
3337
}
@@ -116,7 +120,7 @@ protected Object expected(Map<String, Object> fieldMapping, Object value, TestCo
116120
}
117121

118122
@SuppressWarnings("unchecked")
119-
private Object valuesInSourceOrder(Object value) {
123+
private static Object valuesInSourceOrder(Object value) {
120124
if (value == null) {
121125
return null;
122126
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.blockloader;
11+
12+
import org.elasticsearch.datageneration.FieldType;
13+
import org.elasticsearch.datageneration.datasource.DataSourceHandler;
14+
import org.elasticsearch.datageneration.datasource.DataSourceRequest;
15+
import org.elasticsearch.datageneration.datasource.DataSourceResponse;
16+
import org.elasticsearch.datageneration.datasource.DefaultMappingParametersHandler;
17+
import org.elasticsearch.index.mapper.BlockLoaderTestCase;
18+
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
23+
public class TextFieldWithParentBlockLoaderTests extends BlockLoaderTestCase {
24+
public TextFieldWithParentBlockLoaderTests(Params params) {
25+
// keyword because we need a keyword parent field
26+
super(FieldType.KEYWORD.toString(), List.of(new DataSourceHandler() {
27+
@Override
28+
public DataSourceResponse.LeafMappingParametersGenerator handle(DataSourceRequest.LeafMappingParametersGenerator request) {
29+
assert request.fieldType().equals(FieldType.KEYWORD.toString());
30+
31+
// We need to force multi field generation
32+
return new DataSourceResponse.LeafMappingParametersGenerator(() -> {
33+
var defaultSupplier = DefaultMappingParametersHandler.keywordMapping(
34+
request,
35+
DefaultMappingParametersHandler.commonMappingParameters()
36+
);
37+
var mapping = defaultSupplier.get();
38+
// we don't need this here
39+
mapping.remove("copy_to");
40+
41+
var textMultiFieldMappingSupplier = DefaultMappingParametersHandler.textMapping(request, new HashMap<>());
42+
var textMultiFieldMapping = textMultiFieldMappingSupplier.get();
43+
textMultiFieldMapping.put("type", "text");
44+
textMultiFieldMapping.remove("fields");
45+
46+
mapping.put("fields", Map.of("txt", textMultiFieldMapping));
47+
48+
return mapping;
49+
});
50+
}
51+
}), params);
52+
}
53+
54+
@Override
55+
@SuppressWarnings("unchecked")
56+
protected Object expected(Map<String, Object> fieldMapping, Object value, TestContext testContext) {
57+
assert fieldMapping.containsKey("fields");
58+
59+
Object normalizer = fieldMapping.get("normalizer");
60+
boolean docValues = hasDocValues(fieldMapping, true);
61+
boolean store = fieldMapping.getOrDefault("store", false).equals(true);
62+
63+
if (normalizer == null && (docValues || store)) {
64+
// we are using block loader of the parent field
65+
return KeywordFieldBlockLoaderTests.expectedValue(fieldMapping, value, params, testContext);
66+
}
67+
68+
// we are using block loader of the text field itself
69+
var textFieldMapping = (Map<String, Object>) ((Map<String, Object>) fieldMapping.get("fields")).get("txt");
70+
return TextFieldBlockLoaderTests.expectedValue(textFieldMapping, value, params, testContext);
71+
}
72+
73+
@Override
74+
protected String blockLoaderFieldName(String originalName) {
75+
return originalName + ".txt";
76+
}
77+
}

test/framework/src/main/java/org/elasticsearch/datageneration/datasource/DefaultMappingParametersHandler.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private Supplier<Map<String, Object>> numberMapping(Map<String, Object> injected
8484
};
8585
}
8686

87-
private Supplier<Map<String, Object>> keywordMapping(
87+
public static Supplier<Map<String, Object>> keywordMapping(
8888
DataSourceRequest.LeafMappingParametersGenerator request,
8989
Map<String, Object> injected
9090
) {
@@ -111,6 +111,14 @@ private Supplier<Map<String, Object>> keywordMapping(
111111
injected.put("null_value", ESTestCase.randomAlphaOfLengthBetween(0, 10));
112112
}
113113

114+
if (ESTestCase.randomDouble() <= 0.1) {
115+
var textMultiFieldMapping = textMapping(request, new HashMap<>()).get();
116+
textMultiFieldMapping.put("type", "text");
117+
textMultiFieldMapping.remove("fields");
118+
119+
injected.put("fields", Map.of("txt", textMultiFieldMapping));
120+
}
121+
114122
return injected;
115123
};
116124
}
@@ -192,7 +200,7 @@ private Supplier<Map<String, Object>> geoPointMapping(Map<String, Object> inject
192200
};
193201
}
194202

195-
private Supplier<Map<String, Object>> textMapping(
203+
public static Supplier<Map<String, Object>> textMapping(
196204
DataSourceRequest.LeafMappingParametersGenerator request,
197205
Map<String, Object> injected
198206
) {
@@ -206,7 +214,6 @@ private Supplier<Map<String, Object>> textMapping(
206214
keywordMultiFieldMapping.remove("copy_to");
207215

208216
injected.put("fields", Map.of("kwd", keywordMultiFieldMapping));
209-
210217
}
211218

212219
return injected;
@@ -250,7 +257,7 @@ private Supplier<Map<String, Object>> wildcardMapping(Map<String, Object> inject
250257
};
251258
}
252259

253-
private static HashMap<String, Object> commonMappingParameters() {
260+
public static HashMap<String, Object> commonMappingParameters() {
254261
var map = new HashMap<String, Object>();
255262
map.put("store", ESTestCase.randomBoolean());
256263
map.put("index", ESTestCase.randomBoolean());

test/framework/src/main/java/org/elasticsearch/index/mapper/BlockLoaderTestCase.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ private void runTest(Template template, Mapping mapping, String fieldName, TestC
166166
var document = documentGenerator.generate(template, mapping);
167167
var documentXContent = XContentBuilder.builder(XContentType.JSON.xContent()).map(document);
168168

169-
Object blockLoaderResult = setupAndInvokeBlockLoader(mapperService, documentXContent, fieldName);
170169
Object expected = expected(mapping.lookup().get(fieldName), getFieldValue(document, fieldName), testContext);
170+
Object blockLoaderResult = setupAndInvokeBlockLoader(mapperService, documentXContent, blockLoaderFieldName(fieldName));
171171
assertEquals(expected, blockLoaderResult);
172172
}
173173

@@ -216,6 +216,14 @@ protected static Object maybeFoldList(List<?> list) {
216216
return list;
217217
}
218218

219+
/**
220+
Allows to change the field name used to obtain a block loader.
221+
Useful f.e. to test block loaders of multi fields.
222+
*/
223+
protected String blockLoaderFieldName(String originalName) {
224+
return originalName;
225+
}
226+
219227
private Object setupAndInvokeBlockLoader(MapperService mapperService, XContentBuilder document, String fieldName) throws IOException {
220228
try (Directory directory = newDirectory()) {
221229
RandomIndexWriter iw = new RandomIndexWriter(random(), directory);

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/mapper/OffsetSourceFieldMapperTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ protected SyntheticSourceSupport syntheticSourceSupport(boolean ignoreMalformed)
106106
return new SyntheticSourceSupport() {
107107
@Override
108108
public SyntheticSourceExample example(int maxValues) {
109-
return new SyntheticSourceExample(getSampleValueForDocument(), getSampleValueForDocument(), null, b -> minimalMapping(b));
109+
return new SyntheticSourceExample(getSampleValueForDocument(), getSampleValueForDocument(), b -> minimalMapping(b));
110110
}
111111

112112
@Override

0 commit comments

Comments
 (0)