1313import org .elasticsearch .index .mapper .BlockLoaderTestCase ;
1414import org .elasticsearch .logsdb .datageneration .FieldType ;
1515
16+ import java .util .HashSet ;
1617import java .util .List ;
1718import java .util .Map ;
1819import java .util .Objects ;
19- import java .util .stream .Collectors ;
20+ import java .util .function .Function ;
21+ import java .util .stream .Stream ;
2022
2123public 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}
0 commit comments