-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement metrics aggregations (#29)
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
src/search/aggregations/metrics/cardinality_aggregation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
}) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters