Skip to content

Commit 9c9e6e7

Browse files
committed
add agg test for nested JSON
1 parent b60d862 commit 9c9e6e7

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/aggregation/agg_tests.rs

+61
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,67 @@ fn test_aggregation_on_json_object() {
624624
);
625625
}
626626

627+
#[test]
628+
fn test_aggregation_on_nested_json_object() {
629+
let mut schema_builder = Schema::builder();
630+
let json = schema_builder.add_json_field("json.blub", FAST);
631+
let schema = schema_builder.build();
632+
let index = Index::create_in_ram(schema);
633+
let mut index_writer: IndexWriter = index.writer_for_tests().unwrap();
634+
index_writer
635+
.add_document(doc!(json => json!({"color.dot": "red", "color": {"nested":"red"} })))
636+
.unwrap();
637+
index_writer
638+
.add_document(doc!(json => json!({"color.dot": "blue", "color": {"nested":"blue"} })))
639+
.unwrap();
640+
index_writer.commit().unwrap();
641+
let reader = index.reader().unwrap();
642+
let searcher = reader.searcher();
643+
644+
let agg: Aggregations = serde_json::from_value(json!({
645+
"jsonagg1": {
646+
"terms": {
647+
// I think "json.blub" should be "json\\.blub" to be consistent
648+
"field": "json.blub.color\\.dot",
649+
}
650+
},
651+
"jsonagg2": {
652+
"terms": {
653+
// I think "json.blub" should be "json\\.blub" to be consistent
654+
"field": "json.blub.color.nested",
655+
}
656+
}
657+
658+
}))
659+
.unwrap();
660+
661+
let aggregation_collector = get_collector(agg);
662+
let aggregation_results = searcher.search(&AllQuery, &aggregation_collector).unwrap();
663+
let aggregation_res_json = serde_json::to_value(aggregation_results).unwrap();
664+
assert_eq!(
665+
&aggregation_res_json,
666+
&serde_json::json!({
667+
"jsonagg1": {
668+
"buckets": [
669+
{"doc_count": 1, "key": "blue"},
670+
{"doc_count": 1, "key": "red"}
671+
],
672+
"doc_count_error_upper_bound": 0,
673+
"sum_other_doc_count": 0
674+
},
675+
"jsonagg2": {
676+
"buckets": [
677+
{"doc_count": 1, "key": "blue"},
678+
{"doc_count": 1, "key": "red"}
679+
],
680+
"doc_count_error_upper_bound": 0,
681+
"sum_other_doc_count": 0
682+
}
683+
684+
})
685+
);
686+
}
687+
627688
#[test]
628689
fn test_aggregation_on_json_object_empty_columns() {
629690
let mut schema_builder = Schema::builder();

0 commit comments

Comments
 (0)