elasticsearch_dsl/search/params/units.rs
1use serde::ser::{Serialize, Serializer};
2
3/// Whenever durations need to be specified, e.g. for a `timeout` parameter,
4/// the duration must specify the unit, like `2d` for 2 days.
5///
6/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#time-units>
7#[derive(Debug, PartialEq, Eq, Clone, Copy)]
8#[allow(missing_docs)]
9pub enum Time {
10 Days(u64),
11 Hours(u64),
12 Minutes(u64),
13 Seconds(u64),
14 Milliseconds(u64),
15 Microseconds(u64),
16 Nanoseconds(u64),
17}
18
19impl Serialize for Time {
20 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
21 where
22 S: Serializer,
23 {
24 match self {
25 Self::Days(u) => format!("{u}d"),
26 Self::Hours(u) => format!("{u}h"),
27 Self::Minutes(u) => format!("{u}m"),
28 Self::Seconds(u) => format!("{u}s"),
29 Self::Milliseconds(u) => format!("{u}ms"),
30 Self::Microseconds(u) => format!("{u}micros"),
31 Self::Nanoseconds(u) => format!("{u}nanos"),
32 }
33 .serialize(serializer)
34 }
35}
36
37/// Calendar-aware intervals are configured with the `calendar_interval` parameter. You can specify
38/// calendar intervals using the unit name, such as `month`, or as a single unit quantity, such as
39/// `1M`. For example,`day` and `1d` are equivalent. Multiple quantities, such as `2d`, are not supported.
40///
41/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html#calendar_intervals>
42#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Serialize)]
43#[serde(rename_all = "snake_case")]
44pub enum CalendarInterval {
45 /// All minutes begin at 00 seconds. One minute is the interval between 00 seconds of the first
46 /// minute and 00 seconds of the following minute in the specified time zone, compensating for
47 /// any intervening leap seconds, so that the number of minutes and seconds past the hour is the
48 /// same at the start and end.
49 Minute,
50 /// All hours begin at 00 minutes and 00 seconds. One hour (1h) is the interval between 00:00
51 /// minutes of the first hour and 00:00 minutes of the following hour in the specified time zone,
52 /// compensating for any intervening leap seconds, so that the number of minutes and seconds past
53 /// the hour is the same at the start and end.
54 Hour,
55 /// All days begin at the earliest possible time, which is usually 00:00:00 (midnight). One day
56 /// (1d) is the interval between the start of the day and the start of the following day in the
57 /// specified time zone, compensating for any intervening time changes.
58 Day,
59 /// One week is the interval between the start day_of_week:hour:minute:second and the same day
60 /// of the week and time of the following week in the specified time zone.
61 Week,
62 /// One month is the interval between the start day of the month and time of day and the same
63 /// day of the month and time of the following month in the specified time zone, so that the day
64 /// of the month and time of day are the same at the start and end.
65 Month,
66 /// One quarter is the interval between the start day of the month and time of day and the same
67 /// day of the month and time of day three months later, so that the day of the month and time
68 /// of day are the same at the start and end.
69 Quarter,
70 /// One year is the interval between the start day of the month and time of day and the same day
71 /// of the month and time of day the following year in the specified time zone, so that the date
72 /// and time are the same at the start and end.
73 Year,
74}
75
76/// Whenever the byte size of data needs to be specified, e.g. when setting a
77/// buffer size parameter, the value must specify the unit,
78/// like `10kb` for 10 kilobytes.
79/// Note that these units use powers of 1024, so `1kb` means 1024 bytes.
80///
81/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#byte-units>
82#[derive(Debug, PartialEq, Eq, Clone, Copy)]
83#[allow(missing_docs)]
84pub enum Byte {
85 Bytes(u64),
86 Kilobytes(u64),
87 Megabytes(u64),
88 Gigabytes(u64),
89 Terabytes(u64),
90 Petabytes(u64),
91}
92
93impl Serialize for Byte {
94 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
95 where
96 S: Serializer,
97 {
98 match self {
99 Self::Bytes(u) => format!("{u}b"),
100 Self::Kilobytes(u) => format!("{u}kb"),
101 Self::Megabytes(u) => format!("{u}mb"),
102 Self::Gigabytes(u) => format!("{u}gb"),
103 Self::Terabytes(u) => format!("{u}tb"),
104 Self::Petabytes(u) => format!("{u}pb"),
105 }
106 .serialize(serializer)
107 }
108}
109
110/// Unit-less quantities means that they don’t have a "unit"
111/// like "bytes" or "Hertz" or "meter" or "long tonne".
112///
113/// If one of these quantities is large we’ll print it out like 10m for
114/// 10,000,000 or 7k for 7,000. We’ll still print 87 when we mean 87 though.
115///
116/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#size-units>
117#[derive(Debug, PartialEq, Eq, Clone, Copy)]
118#[allow(missing_docs)]
119pub enum Size {
120 Kilo(u64),
121 Mega(u64),
122 Giga(u64),
123 Tera(u64),
124 Peta(u64),
125}
126
127impl Serialize for Size {
128 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
129 where
130 S: Serializer,
131 {
132 match self {
133 Self::Kilo(u) => format!("{u}k"),
134 Self::Mega(u) => format!("{u}m"),
135 Self::Giga(u) => format!("{u}g"),
136 Self::Tera(u) => format!("{u}t"),
137 Self::Peta(u) => format!("{u}p"),
138 }
139 .serialize(serializer)
140 }
141}
142
143/// Wherever distances need to be specified, such as the `distance` parameter
144/// in the
145/// [Geo-distance](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html)
146/// ), the default unit is meters if none is specified.
147/// Distances can be specified in other units,
148/// such as `"1km"` or `"2mi"` (2 miles).
149///
150/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html>
151#[derive(Debug, PartialEq, Eq, Clone, Copy)]
152#[allow(missing_docs)]
153pub enum Distance {
154 Miles(u64),
155 Yards(u64),
156 Feet(u64),
157 Inches(u64),
158 Kilometers(u64),
159 Meters(u64),
160 Centimeter(u64),
161 Millimeters(u64),
162 NauticalMiles(u64),
163}
164
165impl Serialize for Distance {
166 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
167 where
168 S: Serializer,
169 {
170 match self {
171 Self::Miles(u) => format!("{u}mi"),
172 Self::Yards(u) => format!("{u}yd"),
173 Self::Feet(u) => format!("{u}ft"),
174 Self::Inches(u) => format!("{u}in"),
175 Self::Kilometers(u) => format!("{u}km"),
176 Self::Meters(u) => format!("{u}m"),
177 Self::Centimeter(u) => format!("{u}cm"),
178 Self::Millimeters(u) => format!("{u}mm"),
179 Self::NauticalMiles(u) => format!("{u}nmi"),
180 }
181 .serialize(serializer)
182 }
183}
184
185/// Wherever distances need to be specified, such as the `distance` parameter
186/// in the
187/// [Geo-distance](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html)
188/// ), the default unit is meters if none is specified.
189/// Distances can be specified in other units,
190/// such as `"1km"` or `"2mi"` (2 miles).
191///
192/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html>
193#[derive(Debug, PartialEq, Eq, Clone, Copy)]
194#[allow(missing_docs)]
195pub enum DistanceUnit {
196 Miles,
197 Yards,
198 Feet,
199 Inches,
200 Kilometers,
201 Meters,
202 Centimeter,
203 Millimeters,
204 NauticalMiles,
205}
206
207impl Serialize for DistanceUnit {
208 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
209 where
210 S: Serializer,
211 {
212 match self {
213 Self::Miles => "mi",
214 Self::Yards => "yd",
215 Self::Feet => "ft",
216 Self::Inches => "in",
217 Self::Kilometers => "km",
218 Self::Meters => "m",
219 Self::Centimeter => "cm",
220 Self::Millimeters => "mm",
221 Self::NauticalMiles => "nmi",
222 }
223 .serialize(serializer)
224 }
225}