qdrant_client/
expressions.rs

1//! Provides utility constructors and functions for creating Qdrant Expression instances.
2//!
3//! This module offers a more ergonomic way to create Expression instances for use in
4//! scoring formulas and other Qdrant operations.
5
6use crate::qdrant::*;
7
8impl Expression {
9    /// Creates a new Expression with a constant value.
10    pub fn constant(value: f32) -> Self {
11        Self {
12            variant: Some(expression::Variant::Constant(value)),
13        }
14    }
15
16    /// Creates a new Expression with a variable (payload key or reference to score).
17    pub fn variable<S: Into<String>>(name: S) -> Self {
18        Self {
19            variant: Some(expression::Variant::Variable(name.into())),
20        }
21    }
22
23    /// Creates a new Expression with a reference to the score of the prefetch.
24    pub fn score() -> Self {
25        Self {
26            variant: Some(expression::Variant::Variable("$score".to_string())),
27        }
28    }
29
30    /// Creates a new Expression with a reference to the score of a specific prefetch, when there are multiple prefetches.
31    pub fn score_idx(idx: usize) -> Self {
32        Self {
33            variant: Some(expression::Variant::Variable(format!("$score[{idx}]"))),
34        }
35    }
36
37    /// Creates a new Expression with a condition. If true, becomes 1.0; otherwise 0.0.
38    pub fn condition<C: Into<Condition>>(condition: C) -> Self {
39        Self {
40            variant: Some(expression::Variant::Condition(condition.into())),
41        }
42    }
43
44    /// Creates a new Expression with a geographic distance in meters.
45    pub fn geo_distance<G: Into<GeoDistance>>(geo_distance: G) -> Self {
46        Self {
47            variant: Some(expression::Variant::GeoDistance(geo_distance.into())),
48        }
49    }
50
51    /// Creates a new Expression with a date-time constant.
52    pub fn datetime<S: Into<String>>(datetime: S) -> Self {
53        Self {
54            variant: Some(expression::Variant::Datetime(datetime.into())),
55        }
56    }
57
58    /// Creates a new Expression with a payload key with date-time values.
59    pub fn datetime_key<S: Into<String>>(key: S) -> Self {
60        Self {
61            variant: Some(expression::Variant::DatetimeKey(key.into())),
62        }
63    }
64
65    /// Creates a new Expression with a multiplication expression.
66    pub fn mult<M: Into<MultExpression>>(mult: M) -> Self {
67        Self {
68            variant: Some(expression::Variant::Mult(mult.into())),
69        }
70    }
71
72    /// Creates a new Expression with a sum expression.
73    pub fn sum<S: Into<SumExpression>>(sum: S) -> Self {
74        Self {
75            variant: Some(expression::Variant::Sum(sum.into())),
76        }
77    }
78
79    /// Creates a new Expression with a division expression.
80    pub fn div<D: Into<DivExpression>>(div: D) -> Self {
81        Self {
82            variant: Some(expression::Variant::Div(Box::new(div.into()))),
83        }
84    }
85
86    /// Creates a new Expression with a negation expression.
87    pub fn neg<E: Into<Expression>>(expr: E) -> Self {
88        Self {
89            variant: Some(expression::Variant::Neg(Box::new(expr.into()))),
90        }
91    }
92
93    /// Creates a new Expression with an absolute value expression.
94    pub fn abs<E: Into<Expression>>(expr: E) -> Self {
95        Self {
96            variant: Some(expression::Variant::Abs(Box::new(expr.into()))),
97        }
98    }
99
100    /// Creates a new Expression with a square root expression.
101    pub fn sqrt<E: Into<Expression>>(expr: E) -> Self {
102        Self {
103            variant: Some(expression::Variant::Sqrt(Box::new(expr.into()))),
104        }
105    }
106
107    /// Creates a new Expression with a power expression.
108    pub fn pow<P: Into<PowExpression>>(pow: P) -> Self {
109        Self {
110            variant: Some(expression::Variant::Pow(Box::new(pow.into()))),
111        }
112    }
113
114    /// Creates a new Expression with an exponential expression.
115    pub fn exp<E: Into<Expression>>(expr: E) -> Self {
116        Self {
117            variant: Some(expression::Variant::Exp(Box::new(expr.into()))),
118        }
119    }
120
121    /// Creates a new Expression with a log10 expression.
122    pub fn log10<E: Into<Expression>>(expr: E) -> Self {
123        Self {
124            variant: Some(expression::Variant::Log10(Box::new(expr.into()))),
125        }
126    }
127
128    /// Creates a new Expression with a natural logarithm expression.
129    pub fn ln<E: Into<Expression>>(expr: E) -> Self {
130        Self {
131            variant: Some(expression::Variant::Ln(Box::new(expr.into()))),
132        }
133    }
134
135    /// Creates a new Expression with a geo distance expression.
136    pub fn geo_distance_with<G: Into<GeoPoint>, S: Into<String>>(origin: G, to: S) -> Self {
137        let geo_distance = GeoDistance {
138            origin: Some(origin.into()),
139            to: to.into(),
140        };
141        Self {
142            variant: Some(expression::Variant::GeoDistance(geo_distance)),
143        }
144    }
145
146    /// Creates a new Expression with an exponential decay expression.
147    pub fn exp_decay<D: Into<DecayParamsExpression>>(decay: D) -> Self {
148        Self {
149            variant: Some(expression::Variant::ExpDecay(Box::new(decay.into()))),
150        }
151    }
152
153    /// Creates a new Expression with a Gaussian decay expression.
154    pub fn gauss_decay<D: Into<DecayParamsExpression>>(decay: D) -> Self {
155        Self {
156            variant: Some(expression::Variant::GaussDecay(Box::new(decay.into()))),
157        }
158    }
159
160    /// Creates a new Expression with a linear decay expression.
161    pub fn lin_decay<D: Into<DecayParamsExpression>>(decay: D) -> Self {
162        Self {
163            variant: Some(expression::Variant::LinDecay(Box::new(decay.into()))),
164        }
165    }
166
167    /// Helper method to create a multiplication expression with multiple sub-expressions.
168    pub fn mult_with<E: Into<Expression>, I: IntoIterator<Item = E>>(expressions: I) -> Self {
169        let exprs: Vec<Expression> = expressions.into_iter().map(|e| e.into()).collect();
170        Self::mult(MultExpression { mult: exprs })
171    }
172
173    /// Helper method to create a sum expression with multiple sub-expressions.
174    pub fn sum_with<E: Into<Expression>, I: IntoIterator<Item = E>>(expressions: I) -> Self {
175        let exprs: Vec<Expression> = expressions.into_iter().map(|e| e.into()).collect();
176        Self::sum(SumExpression { sum: exprs })
177    }
178
179    /// Helper method to create a division expression with left and right operands.
180    pub fn div_with<L: Into<Expression>, R: Into<Expression>>(
181        left: L,
182        right: R,
183        by_zero_default: Option<f32>,
184    ) -> Self {
185        Self::div(DivExpression {
186            left: Some(Box::new(left.into())),
187            right: Some(Box::new(right.into())),
188            by_zero_default,
189        })
190    }
191
192    /// Helper method to create a power expression with base and exponent.
193    pub fn pow_with<B: Into<Expression>, E: Into<Expression>>(base: B, exponent: E) -> Self {
194        Self::pow(PowExpression {
195            base: Some(Box::new(base.into())),
196            exponent: Some(Box::new(exponent.into())),
197        })
198    }
199}
200
201impl From<String> for Expression {
202    fn from(value: String) -> Self {
203        Self::variable(value)
204    }
205}
206
207impl From<f32> for Expression {
208    fn from(value: f32) -> Self {
209        Self::constant(value)
210    }
211}