Skip to content

Commit

Permalink
Implement metrics aggregations (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamazy authored Oct 28, 2021
1 parent 3c33c95 commit 702fd37
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/search/aggregations/metrics/cardinality_aggregation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use crate::util::*;
use crate::Aggregation;

/// A `single-value` metrics aggregation that calculates an approximate count of distinct values.
///
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html>
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct CardinalityAggregation {
#[serde(skip_serializing)]
pub(crate) name: String,
cardinality: CardinalityAggregationInner,
}

#[derive(Debug, Clone, Serialize, PartialEq)]
struct CardinalityAggregationInner {
field: String,

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

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

impl Aggregation {
/// Creates an instance of [`CardinalityAggregation`]
///
/// - `name` - name of the aggregation
pub fn cardinality(
name: impl Into<String>,
field: impl Into<String>,
) -> CardinalityAggregation {
CardinalityAggregation {
name: name.into(),
cardinality: CardinalityAggregationInner {
field: field.into(),
precision_threshold: None,
missing: None,
},
}
}
}

impl CardinalityAggregation {
/// The `precision_threshold` options allows to trade memory for accuracy, and defines a unique count below
/// which counts are expected to be close to accurate. Above this value, counts might become a bit more fuzzy.
/// The maximum supported value is 40000, thresholds above this number will have the same effect as a threshold
/// of 40000. The default value is 3000
pub fn precision_threshold(mut self, precision_threshold: impl Into<u16>) -> Self {
self.cardinality.precision_threshold = Some(precision_threshold.into());
self
}

/// The `missing` parameter defines how documents that are missing a value should be treated. By default they will
/// be ignored but it is also possible to treat them as if they had a value.
pub fn missing(mut self, missing: impl Into<String>) -> Self {
self.cardinality.missing = Some(missing.into());
self
}
}

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

test_serialization! {
with_required_fields(
Aggregation::cardinality("test_cardinality", "test_field"),
json!({ "cardinality": { "field": "test_field" } })
);

with_all_fields(
Aggregation::cardinality("test_cardinality", "test_field")
.precision_threshold(100u16)
.missing("N/A"),
json!({
"cardinality": {
"field": "test_field",
"precision_threshold": 100,
"missing": "N/A"
}
})
);
}
}
2 changes: 2 additions & 0 deletions src/search/aggregations/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//!
//! <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html>
mod cardinality_aggregation;
mod top_hits_aggregation;

pub use self::cardinality_aggregation::*;
pub use self::top_hits_aggregation::*;
1 change: 1 addition & 0 deletions src/search/aggregations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ macro_rules! aggregation {
aggregation!(Aggregation {
Terms(TermsAggregation),
TopHits(TopHitsAggregation),
Cardinality(CardinalityAggregation)
});

/// Type alias for a collection of aggregations
Expand Down

0 comments on commit 702fd37

Please sign in to comment.