elasticsearch_dsl/search/aggregations/metrics/rate_aggregation.rs
1use crate::search::*;
2use crate::util::*;
3
4/// A `rate` metrics aggregation can be used only inside a `date_histogram` and calculates a rate of
5/// documents or a field in each `date_histogram` bucket. The field values can be generated extracted
6/// from specific numeric or [histogram fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/histogram.html)
7/// in the documents.
8///
9/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-rate-aggregation.html>
10#[derive(Debug, Clone, Serialize, PartialEq)]
11pub struct RateAggregation {
12 rate: RateAggregationInner,
13}
14
15#[derive(Debug, Clone, Serialize, PartialEq)]
16struct RateAggregationInner {
17 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
18 field: Option<String>,
19 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
20 unit: Option<CalendarInterval>,
21 #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
22 mode: Option<RateMode>,
23}
24
25impl Aggregation {
26 /// Creates an instance of [`RateAggregation`]
27 pub fn rate() -> RateAggregation {
28 RateAggregation {
29 rate: RateAggregationInner {
30 field: None,
31 unit: None,
32 mode: None,
33 },
34 }
35 }
36}
37
38impl RateAggregation {
39 /// Calculate sum or number of values of the `field`
40 pub fn field<T>(mut self, field: T) -> Self
41 where
42 T: ToString,
43 {
44 self.rate.field = Some(field.to_string());
45 self
46 }
47
48 /// The `rate` aggregation supports all rate that can be used [calendar_intervals parameter](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#calendar_intervals)
49 /// of `date_histogram` aggregation. The specified rate should compatible with the date_histogram
50 /// aggregation interval, i.e. it should be possible to convert the bucket size into the rate.
51 /// By default the interval of the `date_histogram` is used.
52 ///
53 /// There is also an additional limitations if the date histogram is not a direct parent of the
54 /// rate histogram. In this case both rate interval and histogram interval have to be in the
55 /// same group: [second, `minute`, hour, day, week] or [month, quarter, year]. For example,
56 /// if the date histogram is month based, only rate intervals of month, quarter or year are
57 /// supported. If the date histogram is `day` based, only `second`, ` minute`, `hour`, `day,
58 /// and `week` rate intervals are supported.
59 pub fn unit(mut self, unit: CalendarInterval) -> Self {
60 self.rate.unit = Some(unit);
61 self
62 }
63
64 /// By default sum mode is used.
65 ///
66 /// By adding the `mode` parameter with the value `value_count`, we can change the calculation from
67 /// `sum` to the number of values of the field.
68 pub fn mode(mut self, mode: RateMode) -> Self {
69 self.rate.mode = Some(mode);
70 self
71 }
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77
78 #[test]
79 fn serialization() {
80 assert_serialize_aggregation(Aggregation::rate(), json!({ "rate": { } }));
81
82 assert_serialize_aggregation(
83 Aggregation::rate()
84 .field("price")
85 .unit(CalendarInterval::Day)
86 .mode(RateMode::ValueCount),
87 json!({
88 "rate": {
89 "field": "price",
90 "unit": "day",
91 "mode": "value_count"
92 }
93 }),
94 );
95 }
96}