Skip to content

Commit cf11cc0

Browse files
authored
[TEST] Handle ignore_above in KeywordFieldBlockLoaderTests (#119800)
This is a follow-up to #119415 with #119416 landing previously.
1 parent b367f70 commit cf11cc0

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
import org.elasticsearch.index.mapper.BlockLoaderTestCase;
1414
import org.elasticsearch.logsdb.datageneration.FieldType;
1515

16+
import java.util.HashSet;
1617
import java.util.List;
1718
import java.util.Map;
1819
import java.util.Objects;
19-
import java.util.stream.Collectors;
20+
import java.util.function.Function;
21+
import java.util.stream.Stream;
2022

2123
public class KeywordFieldBlockLoaderTests extends BlockLoaderTestCase {
2224
public KeywordFieldBlockLoaderTests() {
@@ -29,25 +31,30 @@ protected Object expected(Map<String, Object> fieldMapping, Object value, boolea
2931
if (value == null) {
3032
return null;
3133
}
34+
35+
var ignoreAbove = fieldMapping.get("ignore_above") == null
36+
? Integer.MAX_VALUE
37+
: ((Number) fieldMapping.get("ignore_above")).intValue();
38+
3239
if (value instanceof String s) {
33-
return convert(s);
40+
return convert(s, ignoreAbove);
3441
}
3542

36-
var nonNullStream = ((List<String>) value).stream().filter(Objects::nonNull);
43+
Function<Stream<String>, Stream<BytesRef>> convertValues = s -> s.map(v -> convert(v, ignoreAbove)).filter(Objects::nonNull);
3744

3845
if ((boolean) fieldMapping.getOrDefault("doc_values", false)) {
3946
// Sorted and no duplicates
40-
return maybeFoldList(nonNullStream.collect(Collectors.toSet()).stream().sorted().map(this::convert).toList());
41-
}
4247

43-
if ((boolean) fieldMapping.getOrDefault("store", false)) {
44-
return maybeFoldList(nonNullStream.map(this::convert).toList());
48+
var values = new HashSet<>((List<String>) value);
49+
var resultList = convertValues.compose(s -> values.stream().filter(Objects::nonNull).sorted())
50+
.andThen(Stream::toList)
51+
.apply(values.stream());
52+
return maybeFoldList(resultList);
4553
}
4654

47-
// Using source (either stored or synthetic).
48-
// Original order is preserved and values longer than ignore_above are returned.
49-
// TODO actual ignore_above support in data generation
50-
return maybeFoldList(nonNullStream.map(this::convert).toList());
55+
// store: "true" and source
56+
var resultList = convertValues.andThen(Stream::toList).apply(((List<String>) value).stream());
57+
return maybeFoldList(resultList);
5158
}
5259

5360
private Object maybeFoldList(List<?> list) {
@@ -62,7 +69,11 @@ private Object maybeFoldList(List<?> list) {
6269
return list;
6370
}
6471

65-
private BytesRef convert(String value) {
66-
return new BytesRef(value);
72+
private BytesRef convert(String value, int ignoreAbove) {
73+
if (value == null) {
74+
return null;
75+
}
76+
77+
return value.length() <= ignoreAbove ? new BytesRef(value) : null;
6778
}
6879
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private Supplier<Map<String, Object>> keywordMapping(
6666
}
6767

6868
if (ESTestCase.randomDouble() <= 0.2) {
69-
injected.put("ignore_above", ESTestCase.randomIntBetween(1, 10000));
69+
injected.put("ignore_above", ESTestCase.randomIntBetween(1, 100));
7070
}
7171

7272
return injected;

0 commit comments

Comments
 (0)