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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use crate::search::*;
use crate::util::*;
use serde::{Serialize, Serializer};
use serde_json::Value;

/// A multi-bucket aggregation that creates composite buckets from different sources.
///
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html>
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct CompositeAggregation {
    composite: CompositeAggregationInner,

    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    aggs: Aggregations,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
struct CompositeAggregationInner {
    sources: Vec<CompositeSource>,

    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    size: Option<u64>,

    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    after: Option<AfterKey>,
}

impl Aggregation {
    /// Creates an instance of [`CompositeAggregation`]
    ///
    /// - `sources` - A vector of `CompositeSource` which defines the sources for the composite aggregation.
    pub fn composite(sources: Vec<CompositeSource>) -> CompositeAggregation {
        CompositeAggregation {
            composite: CompositeAggregationInner {
                sources,
                size: None,
                after: None,
            },
            aggs: Aggregations::new(),
        }
    }
}

impl CompositeAggregation {
    /// The `size` parameter can be set to define how many composite buckets should be returned.
    ///
    /// - `size` - The maximum number of composite buckets to be returned.
    pub fn size(mut self, size: u64) -> Self {
        self.composite.size = Some(size);
        self
    }

    /// The `after` parameter can be set to paginate composite buckets.
    ///
    /// - `after` - The key to start after for pagination in composite aggregations.
    pub fn after<T>(mut self, after: T) -> Self
    where
        T: Into<AfterKey>,
    {
        self.composite.after = Some(after.into());
        self
    }

    add_aggregate!();
}

/// Represents the `after` key for pagination in composite aggregations.
///
/// The `AfterKey` is used to paginate through the composite aggregation results.
/// It is typically a JSON object containing the values of the composite keys.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct AfterKey(Value);

impl From<Value> for AfterKey {
    fn from(value: Value) -> Self {
        AfterKey(value)
    }
}

impl AfterKey {
    /// Creates a new `AfterKey` instance from a JSON value.
    ///
    /// - `value` - The JSON value representing the `after` key.
    pub fn new(value: Value) -> Self {
        AfterKey(value)
    }
}

/// Represents different types of sources for a composite aggregation.
#[derive(Debug, Clone, PartialEq)]
pub enum CompositeSource {
    /// Terms source for the composite aggregation.
    Terms {
        /// The unique identifier for the terms source.
        name: String,
        /// The terms composite source.
        terms: TermsCompositeSource,
    },
    /// Histogram source for the composite aggregation.
    Histogram {
        /// The unique identifier for the histogram source.
        name: String,
        /// The histogram composite source.
        histogram: HistogramCompositeSource,
    },
    /// Date histogram source for the composite aggregation.
    DateHistogram {
        /// The unique identifier for the date histogram source.
        name: String,
        /// The date histogram composite source.
        date_histogram: DateHistogramCompositeSource,
    },
}

impl Serialize for CompositeSource {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serde_json::Map::new();
        match self {
            CompositeSource::Terms { name, terms } => {
                let _ = map.insert(name.clone(), serde_json::json!({ "terms": terms }));
            }
            CompositeSource::Histogram { name, histogram } => {
                let _ = map.insert(name.clone(), serde_json::json!({ "histogram": histogram }));
            }
            CompositeSource::DateHistogram {
                name,
                date_histogram,
            } => {
                let _ = map.insert(
                    name.clone(),
                    serde_json::json!({ "date_histogram": date_histogram }),
                );
            }
        }
        map.serialize(serializer)
    }
}

/// Represents a terms source in a composite aggregation.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct TermsCompositeSource {
    field: String,
    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    missing_bucket: Option<bool>,
    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    order: Option<SortOrder>,
}

/// Represents a histogram source in a composite aggregation.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct HistogramCompositeSource {
    field: String,
    interval: f64,
    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    missing_bucket: Option<bool>,
    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    order: Option<SortOrder>,
}

/// Represents a date histogram source in a composite aggregation.
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct DateHistogramCompositeSource {
    field: String,
    calendar_interval: String,
    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    missing_bucket: Option<bool>,
    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
    order: Option<SortOrder>,
}

impl CompositeSource {
    /// Creates a terms source for the composite aggregation.
    ///
    /// - `name` - The unique identifier for the terms source.
    /// - `field` - The field to perform the terms aggregation on.
    pub fn terms(name: &str, field: &str) -> CompositeSource {
        CompositeSource::Terms {
            name: name.to_string(),
            terms: TermsCompositeSource {
                field: field.to_string(),
                missing_bucket: None,
                order: None,
            },
        }
    }

    /// Creates a histogram source for the composite aggregation.
    ///
    /// - `name` - The unique identifier for the histogram source.
    /// - `field` - The field to perform the histogram aggregation on.
    /// - `interval` - The interval for the histogram buckets.
    pub fn histogram(name: &str, field: &str, interval: f64) -> CompositeSource {
        CompositeSource::Histogram {
            name: name.to_string(),
            histogram: HistogramCompositeSource {
                field: field.to_string(),
                interval,
                missing_bucket: None,
                order: None,
            },
        }
    }

    /// Creates a date histogram source for the composite aggregation.
    ///
    /// - `name` - The unique identifier for the date histogram source.
    /// - `field` - The field to perform the date histogram aggregation on.
    /// - `calendar_interval` - The calendar interval for the date histogram buckets.
    pub fn date_histogram(name: &str, field: &str, calendar_interval: &str) -> CompositeSource {
        CompositeSource::DateHistogram {
            name: name.to_string(),
            date_histogram: DateHistogramCompositeSource {
                field: field.to_string(),
                calendar_interval: calendar_interval.to_string(),
                missing_bucket: None,
                order: None,
            },
        }
    }
}

impl TermsCompositeSource {
    /// Sets the `missing_bucket` parameter for the terms source.
    ///
    /// - `missing_bucket` - Whether to include documents with missing values in the bucket.
    pub fn missing_bucket(mut self, missing_bucket: bool) -> Self {
        self.missing_bucket = Some(missing_bucket);
        self
    }

    /// Sets the `order` parameter for the terms source.
    ///
    /// - `order` - The order of the terms in the bucket.
    pub fn order(mut self, order: SortOrder) -> Self {
        self.order = Some(order);
        self
    }
}

impl HistogramCompositeSource {
    /// Sets the `missing_bucket` parameter for the histogram source.
    ///
    /// - `missing_bucket` - Whether to include documents with missing values in the bucket.
    pub fn missing_bucket(mut self, missing_bucket: bool) -> Self {
        self.missing_bucket = Some(missing_bucket);
        self
    }

    /// Sets the `order` parameter for the histogram source.
    ///
    /// - `order` - The order of the histogram buckets.
    pub fn order(mut self, order: SortOrder) -> Self {
        self.order = Some(order);
        self
    }
}

impl DateHistogramCompositeSource {
    /// Sets the `missing_bucket` parameter for the date histogram source.
    ///
    /// - `missing_bucket` - Whether to include documents with missing values in the bucket.
    pub fn missing_bucket(mut self, missing_bucket: bool) -> Self {
        self.missing_bucket = Some(missing_bucket);
        self
    }

    /// Sets the `order` parameter for the date histogram source.
    ///
    /// - `order` - The order of the date histogram buckets.
    pub fn order(mut self, order: SortOrder) -> Self {
        self.order = Some(order);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn serialization() {
        assert_serialize_aggregation(
            Aggregation::composite(vec![CompositeSource::terms("test_field", "field_name")]),
            json!({ "composite": { "sources": [{ "test_field": { "terms": { "field": "field_name" } } }] } }),
        );

        assert_serialize_aggregation(
            Aggregation::composite(vec![CompositeSource::terms("test_field", "field_name")])
                .size(10)
                .after(serde_json::json!({"test_field": "after_key"})),
            json!({
                "composite": {
                    "sources": [{ "test_field": { "terms": { "field": "field_name" } } }],
                    "size": 10,
                    "after": { "test_field": "after_key" }
                }
            }),
        );

        assert_serialize_aggregation(
            Aggregation::composite(vec![CompositeSource::terms("test_field", "field_name")]),
            json!({
                "composite": {
                    "sources": [{ "test_field": { "terms": { "field": "field_name" } } }],
                }
            }),
        );
    }
}