1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::aggregation::AggregationTrait;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use serde_json::{json, Value};

#[derive(Default)]
pub struct NestedAggregation {
    name: String,
    value: NestedValue,
    aggregation: Value,
}

#[derive(Default)]
struct NestedValue {
    path: String,
}

impl NestedAggregation {
    pub fn new(name: &str) -> Self {
        let  term = NestedAggregation {
            name: name.to_string(),
            ..Default::default()
        };
        term
    }
    pub fn set_path(mut self, path: &str) -> Self {
        self.value.path = path.to_string();
        self
    }
    pub fn set_aggregation<T>(mut self, aggregation: T) -> Self
    where
        T: AggregationTrait,
    {
        self.aggregation = aggregation.build();
        self
    }
}

impl Serialize for NestedValue {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("TermsBuilder", 0)?;


        if !self.path.is_empty() {
            state.serialize_field("path", &self.path)?;
        }
        state.end()
    }
}

impl Serialize for NestedAggregation {
    fn serialize<S>(&self, serializer: S) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("BoolQuery", 0)?;
        state.serialize_field("nested", &self.value)?;
        if !(self.aggregation.is_null() || self.aggregation.to_string().is_empty()) {
            state.serialize_field("aggs", &self.aggregation)?;
        }
        // }
        state.end()
    }
}



impl AggregationTrait for NestedAggregation {
    fn name(&self) -> &str {
        self.name.as_str()
    }

    fn build(&self) -> Value {
        let name = self.name.to_string();
        json!({ name: self })
    }

    fn query_name(&self) -> String {
        "nested".to_string()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::aggregation::AggregationTrait;
    use crate::aggregation::terms_aggregation::TermsAggregation;

    #[test]
    fn test_nested_aggregation() {
        let agg = NestedAggregation::new("hoge")
            .set_path("path")
            .set_aggregation(NestedAggregation::new("agg"));

        let json = agg.build();
        println!("{}", json);
        let agg = NestedAggregation::new("hoge")
            .set_path("path")
            .set_aggregation(TermsAggregation::new("agg").set_field("key"));

        let json = agg.build();
        println!("{}", json);
        // assert_eq!(json["test"]["nested"]["field"], "test");
        // assert_eq!(json["test"]["nested"]["size"], 1);
    }
}