flusso_query/handles/
sort.rs1use serde_json::{Map, Value};
12
13use super::{NumericType, ScriptSortType};
14use crate::query::AsQuery;
15
16#[derive(Debug, Clone, Copy)]
18pub enum SortOrder {
19 Asc,
21 Desc,
23}
24
25impl SortOrder {
26 pub(crate) fn as_str(self) -> &'static str {
27 match self {
28 SortOrder::Asc => "asc",
29 SortOrder::Desc => "desc",
30 }
31 }
32}
33
34#[derive(Debug, Clone, Copy)]
36pub enum SortMode {
37 Min,
39 Max,
41 Avg,
43 Sum,
45 Median,
47}
48
49impl SortMode {
50 fn as_str(self) -> &'static str {
51 match self {
52 SortMode::Min => "min",
53 SortMode::Max => "max",
54 SortMode::Avg => "avg",
55 SortMode::Sum => "sum",
56 SortMode::Median => "median",
57 }
58 }
59}
60
61#[derive(Debug, Clone)]
65pub struct Sort {
66 key: String,
67 body: Map<String, Value>,
68}
69
70impl Sort {
71 pub(crate) fn new(field: &str, order: SortOrder) -> Self {
73 let mut body = Map::new();
74 body.insert(
75 "order".to_string(),
76 Value::String(order.as_str().to_string()),
77 );
78 Self {
79 key: field.to_string(),
80 body,
81 }
82 }
83
84 #[must_use]
86 pub fn score() -> Self {
87 let mut sort = Self {
88 key: "_score".to_string(),
89 body: Map::new(),
90 };
91 sort.body
92 .insert("order".to_string(), Value::String("desc".to_string()));
93 sort
94 }
95
96 #[must_use]
100 pub fn script(
101 script_type: ScriptSortType,
102 source: impl Into<String>,
103 order: SortOrder,
104 ) -> Self {
105 let mut script = Map::new();
106 script.insert("source".to_string(), Value::String(source.into()));
107 let mut body = Map::new();
108 body.insert(
109 "type".to_string(),
110 Value::String(script_type.as_str().to_string()),
111 );
112 body.insert("script".to_string(), Value::Object(script));
113 body.insert(
114 "order".to_string(),
115 Value::String(order.as_str().to_string()),
116 );
117 Self {
118 key: "_script".to_string(),
119 body,
120 }
121 }
122
123 pub(crate) fn from_parts(key: String, body: Map<String, Value>) -> Self {
125 Self { key, body }
126 }
127
128 #[must_use]
130 pub fn asc(mut self) -> Self {
131 self.body
132 .insert("order".to_string(), Value::String("asc".to_string()));
133 self
134 }
135
136 #[must_use]
138 pub fn desc(mut self) -> Self {
139 self.body
140 .insert("order".to_string(), Value::String("desc".to_string()));
141 self
142 }
143
144 #[must_use]
146 pub fn missing_first(mut self) -> Self {
147 self.body
148 .insert("missing".to_string(), Value::String("_first".to_string()));
149 self
150 }
151
152 #[must_use]
154 pub fn missing_last(mut self) -> Self {
155 self.body
156 .insert("missing".to_string(), Value::String("_last".to_string()));
157 self
158 }
159
160 #[must_use]
162 pub fn missing(mut self, value: impl Into<Value>) -> Self {
163 self.body.insert("missing".to_string(), value.into());
164 self
165 }
166
167 #[must_use]
169 pub fn mode(mut self, mode: SortMode) -> Self {
170 self.body
171 .insert("mode".to_string(), Value::String(mode.as_str().to_string()));
172 self
173 }
174
175 #[must_use]
178 pub fn unmapped_type(mut self, unmapped_type: impl Into<String>) -> Self {
179 self.body.insert(
180 "unmapped_type".to_string(),
181 Value::String(unmapped_type.into()),
182 );
183 self
184 }
185
186 #[must_use]
188 pub fn numeric_type(mut self, numeric_type: NumericType) -> Self {
189 self.body.insert(
190 "numeric_type".to_string(),
191 Value::String(numeric_type.as_str().to_string()),
192 );
193 self
194 }
195
196 #[must_use]
198 pub fn format(mut self, format: impl Into<String>) -> Self {
199 self.body
200 .insert("format".to_string(), Value::String(format.into()));
201 self
202 }
203
204 #[must_use]
206 pub fn nested(mut self, path: impl Into<String>) -> Self {
207 let mut nested = Map::new();
208 nested.insert("path".to_string(), Value::String(path.into()));
209 self.body
210 .insert("nested".to_string(), Value::Object(nested));
211 self
212 }
213
214 #[must_use]
217 pub fn nested_filtered<S>(mut self, path: impl Into<String>, filter: impl AsQuery<S>) -> Self {
218 let mut nested = Map::new();
219 nested.insert("path".to_string(), Value::String(path.into()));
220 if let Some(query) = filter.into_query() {
221 nested.insert("filter".to_string(), query.to_value());
222 }
223 self.body
224 .insert("nested".to_string(), Value::Object(nested));
225 self
226 }
227
228 pub(crate) fn to_value(&self) -> Value {
229 let mut outer = Map::new();
230 outer.insert(self.key.clone(), Value::Object(self.body.clone()));
231 Value::Object(outer)
232 }
233}