elasticsearch_dsl/search/aggregations/bucket/
children_aggregation.rs

1use crate::search::*;
2use crate::util::*;
3use serde::Serialize;
4
5#[derive(Debug, Clone, Serialize, PartialEq)]
6/// A single bucket aggregation that selects child documents that have the specified type,
7/// then executes a sub-aggregation for the children documents.
8///
9/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html>
10pub struct ChildrenAggregation {
11    children: ChildrenAggregationInner,
12
13    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
14    aggs: Aggregations,
15}
16
17#[derive(Debug, Clone, Serialize, PartialEq)]
18struct ChildrenAggregationInner {
19    #[serde(rename = "type")]
20    type_: String,
21}
22
23impl Aggregation {
24    /// Creates an instance of [`ChildrenAggregation`]
25    ///
26    /// - `type_` - type of the child documents
27    pub fn children<T>(type_: T) -> ChildrenAggregation
28    where
29        T: ToString,
30    {
31        ChildrenAggregation {
32            children: ChildrenAggregationInner {
33                type_: type_.to_string(),
34            },
35            aggs: Aggregations::new(),
36        }
37    }
38}
39
40impl ChildrenAggregation {
41    add_aggregate!();
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn serialization() {
50        assert_serialize_aggregation(
51            Aggregation::children("answer"),
52            json!({ "children": { "type": "answer" } }),
53        );
54
55        assert_serialize_aggregation(
56            Aggregation::children("answer").aggregate("avg_score", Aggregation::avg("score")),
57            json!({
58                "children": {
59                    "type": "answer"
60                },
61                "aggs": {
62                    "avg_score": {
63                        "avg": {
64                            "field": "score"
65                        }
66                    }
67                }
68            }),
69        );
70    }
71}