1use serde_json::{Map, Value};
25
26use crate::query::Query;
27
28mod compound;
29mod extra;
30mod geo;
31mod map;
32mod nested;
33mod object;
34mod params;
35mod scalar;
36mod sort;
37mod string;
38
39pub use compound::{
40 BoostingQuery, ConstantScoreQuery, DisMaxQuery, FunctionScoreQuery, boosting, constant_score,
41 dis_max, function_score,
42};
43pub use extra::{
44 CombinedFieldsQuery, DistanceFeatureQuery, IdsQuery, MoreLikeThisQuery, QueryStringQuery,
45 RankFeatureQuery, ScriptQuery, ScriptScoreQuery, SimpleQueryStringQuery, combined_fields,
46 distance_feature, ids, more_like_this, query_string, rank_feature, script, script_score,
47 simple_query_string,
48};
49pub use geo::{Distance, DistanceUnit, Geo, GeoDistanceQuery, GeoPoint};
50pub use map::{DateMap, KeywordMap, MapSearch, NumberMap, TextMap};
51pub use nested::{Nested, NestedProjection, NestedQuery};
52pub use object::{Binary, Json, Object};
53pub use params::{
54 BoostMode, DistanceType, Fuzziness, MinimumShouldMatch, MultiMatchType, NestedScoreMode,
55 NumericType, Operator, RangeRelation, ScoreMode, ScriptSortType, ValidationMethod,
56 ZeroTermsQuery,
57};
58pub use scalar::{Bool, Date, EqQuery, Number, RangeQuery, TermsQuery};
59pub use sort::{Sort, SortMode, SortOrder};
60pub use string::{
61 FuzzyQuery, Keyword, MatchQuery, MultiMatchQuery, NoSubfields, PrefixQuery, RegexpQuery,
62 TermQuery, Text, WildcardQuery, WithSubfields, multi_match,
63};
64
65fn single<S>(wrapper: &str, path: &str, value: Value) -> Query<S> {
67 let mut inner = Map::new();
68 inner.insert(path.to_string(), value);
69 wrap(wrapper, inner)
70}
71
72fn wrap<S>(wrapper: &str, body: Map<String, Value>) -> Query<S> {
74 let mut outer = Map::new();
75 outer.insert(wrapper.to_string(), Value::Object(body));
76 Query::leaf(Value::Object(outer))
77}
78
79#[derive(Debug, Clone, Default)]
84pub(crate) struct Common {
85 boost: Option<f32>,
86 name: Option<String>,
87}
88
89impl Common {
90 pub(crate) fn set_boost(&mut self, boost: f32) {
91 self.boost = Some(boost);
92 }
93
94 pub(crate) fn set_name(&mut self, name: String) {
95 self.name = Some(name);
96 }
97
98 pub(crate) fn is_empty(&self) -> bool {
100 self.boost.is_none() && self.name.is_none()
101 }
102
103 pub(crate) fn write(&self, map: &mut Map<String, Value>) {
105 if let Some(boost) = self.boost {
106 map.insert("boost".to_string(), Value::from(boost));
107 }
108 if let Some(name) = &self.name {
109 map.insert("_name".to_string(), Value::String(name.clone()));
110 }
111 }
112}
113
114macro_rules! common_opts {
117 ($field:ident) => {
118 #[must_use]
120 pub fn boost(mut self, boost: f32) -> Self {
121 self.$field.set_boost(boost);
122 self
123 }
124
125 #[must_use]
127 pub fn name(mut self, name: impl Into<String>) -> Self {
128 self.$field.set_name(name.into());
129 self
130 }
131 };
132}
133pub(crate) use common_opts;
134
135fn keyed_value_query<S>(
140 wrapper: &str,
141 path: &str,
142 key: &str,
143 value: Value,
144 mut opts: Map<String, Value>,
145) -> Query<S> {
146 if opts.is_empty() {
147 single(wrapper, path, value)
148 } else {
149 opts.insert(key.to_string(), value);
150 single(wrapper, path, Value::Object(opts))
151 }
152}
153
154fn exists_q<S>(path: &str) -> Query<S> {
156 let mut inner = Map::new();
157 inner.insert("field".to_string(), Value::String(path.to_string()));
158 let mut outer = Map::new();
159 outer.insert("exists".to_string(), Value::Object(inner));
160 Query::leaf(Value::Object(outer))
161}
162
163pub(crate) fn match_all_value() -> Value {
165 let mut outer = Map::new();
166 outer.insert("match_all".to_string(), Value::Object(Map::new()));
167 Value::Object(outer)
168}
169
170pub mod kind {
175 #[derive(Debug)]
177 pub enum Keyword {}
178 #[derive(Debug)]
180 pub enum Text {}
181 #[derive(Debug)]
183 pub enum Bool {}
184 #[derive(Debug)]
186 pub enum Byte {}
187 #[derive(Debug)]
189 pub enum Short {}
190 #[derive(Debug)]
192 pub enum Integer {}
193 #[derive(Debug)]
195 pub enum Long {}
196 #[derive(Debug)]
198 pub enum Float {}
199 #[derive(Debug)]
201 pub enum Double {}
202 #[derive(Debug)]
204 pub enum Decimal {}
205 #[derive(Debug)]
207 pub enum Date {}
208}
209
210#[diagnostic::on_unimplemented(
223 message = "`{Self}` is not a valid value for a `{K}` field",
224 label = "unsupported field type",
225 note = "use a built-in leaf type, or add `#[derive(FlussoValue)]` (with the matching kind) to `{Self}`"
226)]
227pub trait FlussoValue<K>: serde::Serialize {}
228
229impl FlussoValue<kind::Keyword> for String {}
230impl FlussoValue<kind::Keyword> for &str {}
231#[cfg(feature = "uuid")]
232impl FlussoValue<kind::Keyword> for uuid::Uuid {}
233#[cfg(feature = "uuid")]
234impl FlussoValue<kind::Keyword> for &uuid::Uuid {}
235
236impl FlussoValue<kind::Text> for String {}
237impl FlussoValue<kind::Text> for &str {}
238
239impl FlussoValue<kind::Bool> for bool {}
240
241macro_rules! number_values {
247 ($kind:path: $($ty:ty),+ $(,)?) => { $(impl FlussoValue<$kind> for $ty {})+ };
248}
249number_values!(kind::Byte: i8);
250number_values!(kind::Short: i8, i16);
251number_values!(kind::Integer: i8, i16, i32);
252number_values!(kind::Long: i8, i16, i32, i64);
253number_values!(kind::Float: i8, i16, f32);
254number_values!(kind::Double: i8, i16, i32, f32, f64);
255number_values!(kind::Decimal: i8, i16, i32, i64);
256#[cfg(feature = "decimal")]
257impl FlussoValue<kind::Decimal> for crate::Decimal {}
258
259impl FlussoValue<kind::Date> for String {}
260impl FlussoValue<kind::Date> for &str {}
261#[cfg(feature = "chrono")]
262impl FlussoValue<kind::Date> for chrono::NaiveDate {}
263#[cfg(feature = "chrono")]
264impl FlussoValue<kind::Date> for chrono::NaiveDateTime {}
265#[cfg(feature = "chrono")]
266impl FlussoValue<kind::Date> for chrono::DateTime<chrono::Utc> {}
267
268#[diagnostic::on_unimplemented(
279 message = "`{Self}` is not a valid map for a `{K}` field",
280 label = "unsupported map type",
281 note = "use `HashMap<String, V>` with a `{K}` value type, or add `#[derive(FlussoMap)]` (with the matching kind) to `{Self}`"
282)]
283pub trait FlussoMap<K> {}
284
285impl<K, V: FlussoValue<K>> FlussoMap<K> for std::collections::HashMap<String, V> {}