elasticsearch_dsl/search/aggregations/params/
terms_include.rs

1use crate::util::ShouldSkip;
2
3/// Filter the values for which buckets will be created.
4#[derive(Debug, Clone, Serialize, PartialEq)]
5#[serde(untagged)]
6pub enum TermsInclude {
7    /// Filter buckets by their regular expression pattern.
8    ///
9    /// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_regular_expressions_2>
10    Regex(String),
11
12    /// Filter buckets by their exact value.
13    ///
14    /// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_exact_values_2>
15    Exact(Vec<String>),
16
17    /// A number of partitions at query-time and processing only one partition in each request.
18    ///
19    /// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_filtering_values_with_partitions>
20    Partitions {
21        /// The partition number to return.
22        partition: u32,
23        /// The number of partitions to create.
24        num_partitions: u32,
25    },
26}
27
28impl ShouldSkip for TermsInclude {
29    fn should_skip(&self) -> bool {
30        match self {
31            Self::Regex(ref s) => s.is_empty(),
32            Self::Exact(ref v) => v.is_empty(),
33            Self::Partitions { .. } => false,
34        }
35    }
36}
37
38impl From<String> for TermsInclude {
39    fn from(s: String) -> Self {
40        Self::Regex(s)
41    }
42}
43
44impl From<&str> for TermsInclude {
45    fn from(s: &str) -> Self {
46        Self::Regex(s.to_string())
47    }
48}
49
50impl From<Vec<String>> for TermsInclude {
51    fn from(v: Vec<String>) -> Self {
52        Self::Exact(v)
53    }
54}
55
56impl From<Vec<&str>> for TermsInclude {
57    fn from(v: Vec<&str>) -> Self {
58        Self::Exact(v.iter().map(|s| s.to_string()).collect())
59    }
60}
61
62impl From<&[&str]> for TermsInclude {
63    fn from(v: &[&str]) -> Self {
64        Self::Exact(v.iter().map(|s| s.to_string()).collect())
65    }
66}
67
68impl<const N: usize> From<[&str; N]> for TermsInclude {
69    fn from(value: [&str; N]) -> Self {
70        Self::Exact(value.iter().map(|s| s.to_string()).collect())
71    }
72}
73
74impl From<(u32, u32)> for TermsInclude {
75    fn from(value: (u32, u32)) -> Self {
76        Self::Partitions {
77            partition: value.0,
78            num_partitions: value.1,
79        }
80    }
81}
82
83impl From<[u32; 2]> for TermsInclude {
84    fn from(value: [u32; 2]) -> Self {
85        Self::Partitions {
86            partition: value[0],
87            num_partitions: value[1],
88        }
89    }
90}