@@ -35,8 +35,6 @@ use crate::{DocId, TantivyError};
35
35
/// # Limitations/Compatibility
36
36
/// Overlapping ranges are not yet supported.
37
37
///
38
- /// The keyed parameter (elasticsearch) is not yet supported.
39
- ///
40
38
/// # Request JSON Format
41
39
/// ```json
42
40
/// {
@@ -51,13 +49,16 @@ use crate::{DocId, TantivyError};
51
49
/// }
52
50
/// }
53
51
/// ```
54
- #[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
52
+ #[ derive( Clone , Debug , Default , PartialEq , Serialize , Deserialize ) ]
55
53
pub struct RangeAggregation {
56
54
/// The field to aggregate on.
57
55
pub field : String ,
58
56
/// Note that this aggregation includes the from value and excludes the to value for each
59
57
/// range. Extra buckets will be created until the first to, and last from, if necessary.
60
58
pub ranges : Vec < RangeAggregationRange > ,
59
+ /// Whether to return the buckets as a hash map
60
+ #[ serde( default ) ]
61
+ pub keyed : bool ,
61
62
}
62
63
63
64
#[ derive( Clone , Debug , PartialEq , Serialize , Deserialize ) ]
@@ -406,6 +407,7 @@ mod tests {
406
407
let req = RangeAggregation {
407
408
field : "dummy" . to_string ( ) ,
408
409
ranges,
410
+ ..Default :: default ( )
409
411
} ;
410
412
411
413
SegmentRangeCollector :: from_req_and_validate (
@@ -427,6 +429,7 @@ mod tests {
427
429
bucket_agg: BucketAggregationType :: Range ( RangeAggregation {
428
430
field: "fraction_f64" . to_string( ) ,
429
431
ranges: vec![ ( 0f64 ..0.1f64 ) . into( ) , ( 0.1f64 ..0.2f64 ) . into( ) ] ,
432
+ ..Default :: default ( )
430
433
} ) ,
431
434
sub_aggregation: Default :: default ( ) ,
432
435
} ) ,
@@ -454,6 +457,49 @@ mod tests {
454
457
Ok ( ( ) )
455
458
}
456
459
460
+ #[ test]
461
+ fn range_keyed_buckets_test ( ) -> crate :: Result < ( ) > {
462
+ let index = get_test_index_with_num_docs ( false , 100 ) ?;
463
+
464
+ let agg_req: Aggregations = vec ! [ (
465
+ "range" . to_string( ) ,
466
+ Aggregation :: Bucket ( BucketAggregation {
467
+ bucket_agg: BucketAggregationType :: Range ( RangeAggregation {
468
+ field: "fraction_f64" . to_string( ) ,
469
+ ranges: vec![ ( 0f64 ..0.1f64 ) . into( ) , ( 0.1f64 ..0.2f64 ) . into( ) ] ,
470
+ keyed: true ,
471
+ } ) ,
472
+ sub_aggregation: Default :: default ( ) ,
473
+ } ) ,
474
+ ) ]
475
+ . into_iter ( )
476
+ . collect ( ) ;
477
+
478
+ let collector = AggregationCollector :: from_aggs ( agg_req, None ) ;
479
+
480
+ let reader = index. reader ( ) ?;
481
+ let searcher = reader. searcher ( ) ;
482
+ let agg_res = searcher. search ( & AllQuery , & collector) . unwrap ( ) ;
483
+
484
+ let res: Value = serde_json:: from_str ( & serde_json:: to_string ( & agg_res) ?) ?;
485
+
486
+ assert_eq ! (
487
+ res,
488
+ json!( {
489
+ "range" : {
490
+ "buckets" : {
491
+ "*-0" : { "key" : "*-0" , "doc_count" : 0 , "to" : 0.0 } ,
492
+ "0-0.1" : { "key" : "0-0.1" , "doc_count" : 10 , "from" : 0.0 , "to" : 0.1 } ,
493
+ "0.1-0.2" : { "key" : "0.1-0.2" , "doc_count" : 10 , "from" : 0.1 , "to" : 0.2 } ,
494
+ "0.2-*" : { "key" : "0.2-*" , "doc_count" : 80 , "from" : 0.2 } ,
495
+ }
496
+ }
497
+ } )
498
+ ) ;
499
+
500
+ Ok ( ( ) )
501
+ }
502
+
457
503
#[ test]
458
504
fn bucket_test_extend_range_hole ( ) {
459
505
let buckets = vec ! [ ( 10f64 ..20f64 ) . into( ) , ( 30f64 ..40f64 ) . into( ) ] ;
0 commit comments