Skip to content

Commit 53097ab

Browse files
committed
Add tests for keyed buckets
1 parent 4a35711 commit 53097ab

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

src/aggregation/agg_req.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ mod tests {
271271
(7f64..20f64).into(),
272272
(20f64..f64::MAX).into(),
273273
],
274-
..Default::default()
274+
keyed: Some(true),
275275
}),
276276
sub_aggregation: Default::default(),
277277
}),
@@ -298,7 +298,8 @@ mod tests {
298298
{
299299
"from": 20.0
300300
}
301-
]
301+
],
302+
"keyed": true
302303
}
303304
}
304305
}"#;

src/aggregation/bucket/histogram/histogram.rs

+42
Original file line numberDiff line numberDiff line change
@@ -1398,4 +1398,46 @@ mod tests {
13981398

13991399
Ok(())
14001400
}
1401+
1402+
#[test]
1403+
fn histogram_keyed_buckets_test() -> crate::Result<()> {
1404+
let index = get_test_index_with_num_docs(false, 100)?;
1405+
1406+
let agg_req: Aggregations = vec![(
1407+
"histogram".to_string(),
1408+
Aggregation::Bucket(BucketAggregation {
1409+
bucket_agg: BucketAggregationType::Histogram(HistogramAggregation {
1410+
field: "score_f64".to_string(),
1411+
interval: 50.0,
1412+
keyed: Some(true),
1413+
..Default::default()
1414+
}),
1415+
sub_aggregation: Default::default(),
1416+
}),
1417+
)]
1418+
.into_iter()
1419+
.collect();
1420+
1421+
let res = exec_request(agg_req, &index)?;
1422+
1423+
assert_eq!(
1424+
res,
1425+
json!({
1426+
"histogram": {
1427+
"buckets": {
1428+
"0": {
1429+
"key": 0.0,
1430+
"doc_count": 50
1431+
},
1432+
"50": {
1433+
"key": 50.0,
1434+
"doc_count": 50
1435+
}
1436+
}
1437+
}
1438+
})
1439+
);
1440+
1441+
Ok(())
1442+
}
14011443
}

src/aggregation/bucket/range.rs

+43
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,49 @@ mod tests {
457457
Ok(())
458458
}
459459

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: Some(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+
460503
#[test]
461504
fn bucket_test_extend_range_hole() {
462505
let buckets = vec![(10f64..20f64).into(), (30f64..40f64).into()];

0 commit comments

Comments
 (0)