elasticsearch_dsl/search/aggregations/bucket/
sampler_aggregation.rs

1use crate::search::*;
2use crate::util::*;
3
4/// A filtering aggregation used to limit any sub aggregations' processing to a sample of the top-scoring documents.
5///
6/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html>
7#[derive(Debug, Clone, Serialize, PartialEq)]
8pub struct SamplerAggregation {
9    sampler: SamplerAggregationInner,
10
11    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
12    aggs: Aggregations,
13}
14
15#[derive(Debug, Clone, Serialize, PartialEq)]
16struct SamplerAggregationInner {
17    #[serde(skip_serializing_if = "ShouldSkip::should_skip")]
18    shard_size: Option<u64>,
19}
20
21impl Aggregation {
22    /// Creates an instance of [`SamplerAggregation`]
23    pub fn sampler() -> SamplerAggregation {
24        SamplerAggregation {
25            sampler: SamplerAggregationInner { shard_size: None },
26            aggs: Aggregations::new(),
27        }
28    }
29}
30
31impl SamplerAggregation {
32    /// The shard_size parameter limits how many top-scoring documents are
33    /// collected in the sample processed on each shard. The default value is 100.
34    pub fn shard_size(mut self, shard_size: u64) -> Self {
35        self.sampler.shard_size = Some(shard_size);
36        self
37    }
38
39    add_aggregate!();
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45
46    #[test]
47    fn serialization() {
48        assert_serialize_aggregation(Aggregation::sampler(), json!({ "sampler": {} }));
49
50        assert_serialize_aggregation(
51            Aggregation::sampler().shard_size(100),
52            json!({ "sampler": { "shard_size": 100 } }),
53        );
54
55        assert_serialize_aggregation(
56            Aggregation::sampler()
57                .shard_size(50)
58                .aggregate("catalog", Aggregation::terms("catalog_id"))
59                .aggregate("brand", Aggregation::terms("brand_id")),
60            json!({
61                "sampler": { "shard_size": 50 },
62                "aggs": {
63                    "catalog": {
64                        "terms": {
65                            "field": "catalog_id"
66                        }
67                    },
68                    "brand": {
69                        "terms": {
70                            "field": "brand_id"
71                        }
72                    }
73                }
74            }),
75        );
76    }
77}