@@ -499,46 +499,6 @@ public static FieldSortBuilder getPrimaryFieldSortOrNull(SearchSourceBuilder sou
499
499
return source .sorts ().get (0 ) instanceof FieldSortBuilder ? (FieldSortBuilder ) source .sorts ().get (0 ) : null ;
500
500
}
501
501
502
- /**
503
- * Return a {@link Function} that converts a serialized point into a {@link Number} according to the provided
504
- * {@link SortField}. This is needed for {@link SortField} that converts values from one type to another using
505
- * {@link FieldSortBuilder#setNumericType(String)} )} (e.g.: long to double).
506
- */
507
- private static Function <byte [], Comparable > numericPointConverter (SortField sortField , NumberFieldType numberFieldType ) {
508
- switch (IndexSortConfig .getSortFieldType (sortField )) {
509
- case LONG :
510
- return v -> numberFieldType .parsePoint (v ).longValue ();
511
-
512
- case INT :
513
- return v -> numberFieldType .parsePoint (v ).intValue ();
514
-
515
- case DOUBLE :
516
- return v -> numberFieldType .parsePoint (v ).doubleValue ();
517
-
518
- case FLOAT :
519
- return v -> numberFieldType .parsePoint (v ).floatValue ();
520
-
521
- default :
522
- return v -> null ;
523
- }
524
- }
525
-
526
- /**
527
- * Return a {@link Function} that converts a serialized date point into a {@link Long} according to the provided
528
- * {@link NumericType}.
529
- */
530
- private static Function <byte [], Comparable > datePointConverter (DateFieldType dateFieldType , String numericTypeStr ) {
531
- if (numericTypeStr != null ) {
532
- NumericType numericType = resolveNumericType (numericTypeStr );
533
- if (dateFieldType .resolution () == MILLISECONDS && numericType == NumericType .DATE_NANOSECONDS ) {
534
- return v -> DateUtils .toNanoSeconds (LongPoint .decodeDimension (v , 0 ));
535
- } else if (dateFieldType .resolution () == NANOSECONDS && numericType == NumericType .DATE ) {
536
- return v -> DateUtils .toMilliSeconds (LongPoint .decodeDimension (v , 0 ));
537
- }
538
- }
539
- return v -> LongPoint .decodeDimension (v , 0 );
540
- }
541
-
542
502
/**
543
503
* Return the {@link MinAndMax} indexed value from the provided {@link FieldSortBuilder} or <code>null</code> if unknown.
544
504
* The value can be extracted on non-nested indexed mapped fields of type keyword, numeric or date, other fields
@@ -555,41 +515,73 @@ public static MinAndMax<?> getMinMaxOrNull(QueryShardContext context, FieldSortB
555
515
if (reader == null || (fieldType == null || fieldType .indexOptions () == IndexOptions .NONE )) {
556
516
return null ;
557
517
}
558
- String fieldName = fieldType .name ();
559
518
switch (IndexSortConfig .getSortFieldType (sortField )) {
560
519
case LONG :
561
520
case INT :
562
521
case DOUBLE :
563
522
case FLOAT :
564
- final Function <byte [], Comparable > converter ;
565
- if (fieldType instanceof NumberFieldType ) {
566
- converter = numericPointConverter (sortField , (NumberFieldType ) fieldType );
567
- } else if (fieldType instanceof DateFieldType ) {
568
- converter = datePointConverter ((DateFieldType ) fieldType , sortBuilder .getNumericType ());
569
- } else {
570
- return null ;
571
- }
572
- if (PointValues .size (reader , fieldName ) == 0 ) {
573
- return null ;
574
- }
575
- final Comparable min = converter .apply (PointValues .getMinPackedValue (reader , fieldName ));
576
- final Comparable max = converter .apply (PointValues .getMaxPackedValue (reader , fieldName ));
577
- return MinAndMax .newMinMax (min , max );
578
-
523
+ return extractNumericMinAndMax (reader , sortField , fieldType , sortBuilder );
579
524
case STRING :
580
525
case STRING_VAL :
581
526
if (fieldType instanceof KeywordFieldMapper .KeywordFieldType ) {
582
- Terms terms = MultiTerms .getTerms (reader , fieldName );
527
+ Terms terms = MultiTerms .getTerms (reader , fieldType . name () );
583
528
if (terms == null ) {
584
529
return null ;
585
530
}
586
- return terms .getMin () != null ? MinAndMax . newMinMax (terms .getMin (), terms .getMax ()) : null ;
531
+ return terms .getMin () != null ? new MinAndMax <> (terms .getMin (), terms .getMax ()) : null ;
587
532
}
588
533
break ;
589
534
}
590
535
return null ;
591
536
}
592
537
538
+ private static MinAndMax <?> extractNumericMinAndMax (IndexReader reader ,
539
+ SortField sortField ,
540
+ MappedFieldType fieldType ,
541
+ FieldSortBuilder sortBuilder ) throws IOException {
542
+ String fieldName = fieldType .name ();
543
+ if (PointValues .size (reader , fieldName ) == 0 ) {
544
+ return null ;
545
+ }
546
+ if (fieldType instanceof NumberFieldType ) {
547
+ NumberFieldType numberFieldType = (NumberFieldType ) fieldType ;
548
+ Number minPoint = numberFieldType .parsePoint (PointValues .getMinPackedValue (reader , fieldName ));
549
+ Number maxPoint = numberFieldType .parsePoint (PointValues .getMaxPackedValue (reader , fieldName ));
550
+ switch (IndexSortConfig .getSortFieldType (sortField )) {
551
+ case LONG :
552
+ return new MinAndMax <>(minPoint .longValue (), maxPoint .longValue ());
553
+ case INT :
554
+ return new MinAndMax <>(minPoint .intValue (), maxPoint .intValue ());
555
+ case DOUBLE :
556
+ return new MinAndMax <>(minPoint .doubleValue (), maxPoint .doubleValue ());
557
+ case FLOAT :
558
+ return new MinAndMax <>(minPoint .floatValue (), maxPoint .floatValue ());
559
+ default :
560
+ return null ;
561
+ }
562
+ } else if (fieldType instanceof DateFieldType ) {
563
+ DateFieldType dateFieldType = (DateFieldType ) fieldType ;
564
+ Function <byte [], Long > dateConverter = createDateConverter (sortBuilder , dateFieldType );
565
+ Long min = dateConverter .apply (PointValues .getMinPackedValue (reader , fieldName ));
566
+ Long max = dateConverter .apply (PointValues .getMaxPackedValue (reader , fieldName ));
567
+ return new MinAndMax <>(min , max );
568
+ }
569
+ return null ;
570
+ }
571
+
572
+ private static Function <byte [], Long > createDateConverter (FieldSortBuilder sortBuilder , DateFieldType dateFieldType ) {
573
+ String numericTypeStr = sortBuilder .getNumericType ();
574
+ if (numericTypeStr != null ) {
575
+ NumericType numericType = resolveNumericType (numericTypeStr );
576
+ if (dateFieldType .resolution () == MILLISECONDS && numericType == NumericType .DATE_NANOSECONDS ) {
577
+ return v -> DateUtils .toNanoSeconds (LongPoint .decodeDimension (v , 0 ));
578
+ } else if (dateFieldType .resolution () == NANOSECONDS && numericType == NumericType .DATE ) {
579
+ return v -> DateUtils .toMilliSeconds (LongPoint .decodeDimension (v , 0 ));
580
+ }
581
+ }
582
+ return v -> LongPoint .decodeDimension (v , 0 );
583
+ }
584
+
593
585
/**
594
586
* Throws an exception if max children is not located at top level nested sort.
595
587
*/
@@ -647,7 +639,7 @@ public static FieldSortBuilder fromXContent(XContentParser parser, String fieldN
647
639
private static final ObjectParser <FieldSortBuilder , Void > PARSER = new ObjectParser <>(NAME );
648
640
649
641
static {
650
- PARSER .declareField (FieldSortBuilder ::missing , p -> p . objectText () , MISSING , ValueType .VALUE );
642
+ PARSER .declareField (FieldSortBuilder ::missing , XContentParser :: objectText , MISSING , ValueType .VALUE );
651
643
PARSER .declareString ((fieldSortBuilder , nestedPath ) -> {
652
644
deprecationLogger .deprecated ("[nested_path] has been deprecated in favor of the [nested] parameter" );
653
645
fieldSortBuilder .setNestedPath (nestedPath );
@@ -660,7 +652,7 @@ public static FieldSortBuilder fromXContent(XContentParser parser, String fieldN
660
652
return SortBuilder .parseNestedFilter (p );
661
653
}, NESTED_FILTER_FIELD );
662
654
PARSER .declareObject (FieldSortBuilder ::setNestedSort , (p , c ) -> NestedSortBuilder .fromXContent (p ), NESTED_FIELD );
663
- PARSER .declareString (( b , v ) -> b . setNumericType ( v ) , NUMERIC_TYPE );
655
+ PARSER .declareString (FieldSortBuilder :: setNumericType , NUMERIC_TYPE );
664
656
}
665
657
666
658
@ Override
0 commit comments