use chrono::{DateTime, Utc};
use crate::{search::*, util::*};
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct DateHistogramAggregation {
date_histogram: DateHistogramAggregationInner,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
aggs: Aggregations,
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
struct DateHistogramAggregationInner {
field: String,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
calendar_interval: Option<CalendarInterval>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
fixed_interval: Option<Time>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
min_doc_count: Option<u32>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
missing: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
offset: Option<String>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
time_zone: Option<String>,
#[serde(default, skip_serializing_if = "ShouldSkip::should_skip")]
order: TermsOrderCollection,
}
impl Aggregation {
pub fn date_histogram<T>(field: T) -> DateHistogramAggregation
where
T: ToString,
{
DateHistogramAggregation {
date_histogram: DateHistogramAggregationInner {
field: field.to_string(),
calendar_interval: None,
fixed_interval: None,
min_doc_count: None,
missing: None,
offset: None,
time_zone: None,
order: Default::default(),
},
aggs: Aggregations::new(),
}
}
}
impl DateHistogramAggregation {
add_aggregate!();
pub fn calendar_interval(mut self, calendar_interval: CalendarInterval) -> Self {
self.date_histogram.calendar_interval = Some(calendar_interval);
self
}
pub fn fixed_interval(mut self, fixed_interval: Time) -> Self {
self.date_histogram.fixed_interval = Some(fixed_interval);
self
}
pub fn min_doc_count(mut self, min_doc_count: u32) -> Self {
self.date_histogram.min_doc_count = Some(min_doc_count);
self
}
pub fn missing(mut self, missing: DateTime<Utc>) -> Self {
self.date_histogram.missing = Some(missing);
self
}
pub fn offset<T>(mut self, offset: T) -> Self
where
T: ToString,
{
self.date_histogram.offset = Some(offset.to_string());
self
}
pub fn time_zone<T>(mut self, time_zone: T) -> Self
where
T: ToString,
{
self.date_histogram.time_zone = Some(time_zone.to_string());
self
}
pub fn order<T>(mut self, order: T) -> Self
where
T: Into<TermsOrderCollection>,
{
self.date_histogram.order = order.into();
self
}
}
#[cfg(test)]
mod tests {
use chrono::prelude::*;
use super::*;
#[test]
fn serialization() {
assert_serialize_aggregation(
Aggregation::date_histogram("test_field"),
json!({ "date_histogram": { "field": "test_field" } }),
);
assert_serialize_aggregation(
Aggregation::date_histogram("test_field")
.calendar_interval(CalendarInterval::Day)
.fixed_interval(Time::Hours(1))
.min_doc_count(2)
.missing(
Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 4)
.single()
.unwrap(),
)
.order(TermsOrder::new("test_order", SortOrder::Asc))
.offset("+6h")
.time_zone("-01:00"),
json!({
"date_histogram": {
"field": "test_field",
"calendar_interval": "day",
"fixed_interval": "1h",
"min_doc_count": 2,
"missing": "2014-11-28T12:00:04Z",
"order": [
{ "test_order": "asc" }
],
"offset": "+6h",
"time_zone": "-01:00"
}
}),
);
assert_serialize_aggregation(
Aggregation::date_histogram("test_field")
.aggregate("test_sub_agg", Aggregation::terms("test_field2")),
json!({
"date_histogram": {
"field": "test_field",
},
"aggs": {
"test_sub_agg": {
"terms": {
"field": "test_field2"
}
}
}
}),
);
}
}