Skip to main content

qdrant_client/builders/
recommend_points_builder.rs

1use crate::grpc_macros::convert_option;
2use crate::qdrant::*;
3
4#[derive(Clone)]
5pub struct RecommendPointsBuilder {
6    /// name of the collection
7    pub(crate) collection_name: Option<String>,
8    /// Look for vectors closest to the vectors from these points
9    pub(crate) positive: Option<Vec<PointId>>,
10    /// Try to avoid vectors like the vector from these points
11    pub(crate) negative: Option<Vec<PointId>>,
12    /// Filter conditions - return only those points that satisfy the specified conditions
13    pub(crate) filter: Option<Option<Filter>>,
14    /// Max number of result
15    pub(crate) limit: Option<u64>,
16    /// Options for specifying which payload to include or not
17    with_payload: Option<with_payload_selector::SelectorOptions>,
18    /// Search config
19    pub(crate) params: Option<Option<SearchParams>>,
20    /// If provided - cut off results with worse scores
21    pub(crate) score_threshold: Option<Option<f32>>,
22    /// Offset of the result
23    pub(crate) offset: Option<Option<u64>>,
24    /// Define which vector to use for recommendation, if not specified - default vector
25    pub(crate) using: Option<Option<String>>,
26    /// Options for specifying which vectors to include into response
27    with_vectors: Option<with_vectors_selector::SelectorOptions>,
28    /// Name of the collection to use for points lookup, if not specified - use current collection
29    pub(crate) lookup_from: Option<Option<LookupLocation>>,
30    /// Options for specifying read consistency guarantees
31    read_consistency: Option<read_consistency::Value>,
32    /// How to use the example vectors to find the results
33    pub(crate) strategy: Option<Option<i32>>,
34    /// Look for vectors closest to those
35    pub(crate) positive_vectors: Option<Vec<Vector>>,
36    /// Try to avoid vectors like this
37    pub(crate) negative_vectors: Option<Vec<Vector>>,
38    /// If set, overrides global timeout setting for this request. Unit is seconds.
39    pub(crate) timeout: Option<Option<u64>>,
40    /// Specify in which shards to look for the points, if not specified - look in all shards
41    pub(crate) shard_key_selector: Option<Option<ShardKeySelector>>,
42}
43
44impl RecommendPointsBuilder {
45    /// name of the collection
46    pub fn collection_name(self, value: String) -> Self {
47        let mut new = self;
48        new.collection_name = Option::Some(value);
49        new
50    }
51    /// Filter conditions - return only those points that satisfy the specified conditions
52    pub fn filter<VALUE: core::convert::Into<Filter>>(self, value: VALUE) -> Self {
53        let mut new = self;
54        new.filter = Option::Some(Option::Some(value.into()));
55        new
56    }
57    /// Max number of result
58    pub fn limit(self, value: u64) -> Self {
59        let mut new = self;
60        new.limit = Option::Some(value);
61        new
62    }
63    /// Options for specifying which payload to include or not
64    pub fn with_payload<VALUE: core::convert::Into<with_payload_selector::SelectorOptions>>(
65        self,
66        value: VALUE,
67    ) -> Self {
68        let mut new = self;
69        new.with_payload = Option::Some(value.into());
70        new
71    }
72    /// Search config
73    pub fn params<VALUE: core::convert::Into<SearchParams>>(self, value: VALUE) -> Self {
74        let mut new = self;
75        new.params = Option::Some(Option::Some(value.into()));
76        new
77    }
78    /// If provided - cut off results with worse scores
79    pub fn score_threshold(self, value: f32) -> Self {
80        let mut new = self;
81        new.score_threshold = Option::Some(Option::Some(value));
82        new
83    }
84    /// Offset of the result
85    pub fn offset(self, value: u64) -> Self {
86        let mut new = self;
87        new.offset = Option::Some(Option::Some(value));
88        new
89    }
90    /// Define which vector to use for recommendation, if not specified - default vector
91    pub fn using<VALUE: core::convert::Into<String>>(self, value: VALUE) -> Self {
92        let mut new = self;
93        new.using = Option::Some(Option::Some(value.into()));
94        new
95    }
96    /// Options for specifying which vectors to include into response
97    pub fn with_vectors<VALUE: core::convert::Into<with_vectors_selector::SelectorOptions>>(
98        self,
99        value: VALUE,
100    ) -> Self {
101        let mut new = self;
102        new.with_vectors = Option::Some(value.into());
103        new
104    }
105    /// Name of the collection to use for points lookup, if not specified - use current collection
106    pub fn lookup_from<VALUE: core::convert::Into<LookupLocation>>(self, value: VALUE) -> Self {
107        let mut new = self;
108        new.lookup_from = Option::Some(Option::Some(value.into()));
109        new
110    }
111    /// Options for specifying read consistency guarantees
112    pub fn read_consistency<VALUE: core::convert::Into<read_consistency::Value>>(
113        self,
114        value: VALUE,
115    ) -> Self {
116        let mut new = self;
117        new.read_consistency = Option::Some(value.into());
118        new
119    }
120    /// How to use the example vectors to find the results
121    pub fn strategy<VALUE: core::convert::Into<i32>>(self, value: VALUE) -> Self {
122        let mut new = self;
123        new.strategy = Option::Some(Option::Some(value.into()));
124        new
125    }
126    /// If set, overrides global timeout setting for this request. Unit is seconds.
127    pub fn timeout(self, value: u64) -> Self {
128        let mut new = self;
129        new.timeout = Option::Some(Option::Some(value));
130        new
131    }
132    /// Specify in which shards to look for the points, if not specified - look in all shards
133    pub fn shard_key_selector<VALUE: core::convert::Into<ShardKeySelector>>(
134        self,
135        value: VALUE,
136    ) -> Self {
137        let mut new = self;
138        new.shard_key_selector = Option::Some(Option::Some(value.into()));
139        new
140    }
141
142    fn build_inner(self) -> Result<RecommendPoints, RecommendPointsBuilderError> {
143        Ok(RecommendPoints {
144            collection_name: match self.collection_name {
145                Some(value) => value,
146                None => {
147                    return Result::Err(core::convert::Into::into(
148                        ::derive_builder::UninitializedFieldError::from("collection_name"),
149                    ));
150                }
151            },
152            positive: self.positive.unwrap_or_default(),
153            negative: self.negative.unwrap_or_default(),
154            filter: self.filter.unwrap_or_default(),
155            limit: match self.limit {
156                Some(value) => value,
157                None => {
158                    return Result::Err(core::convert::Into::into(
159                        ::derive_builder::UninitializedFieldError::from("limit"),
160                    ));
161                }
162            },
163            with_payload: { convert_option(&self.with_payload) },
164            params: self.params.unwrap_or_default(),
165            score_threshold: self.score_threshold.unwrap_or_default(),
166            offset: self.offset.unwrap_or_default(),
167            using: self.using.unwrap_or_default(),
168            with_vectors: { convert_option(&self.with_vectors) },
169            lookup_from: self.lookup_from.unwrap_or_default(),
170            read_consistency: { convert_option(&self.read_consistency) },
171            strategy: self.strategy.unwrap_or_default(),
172            positive_vectors: self.positive_vectors.unwrap_or_default(),
173            negative_vectors: self.negative_vectors.unwrap_or_default(),
174            timeout: self.timeout.unwrap_or_default(),
175            shard_key_selector: self.shard_key_selector.unwrap_or_default(),
176        })
177    }
178    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
179    fn create_empty() -> Self {
180        Self {
181            collection_name: core::default::Default::default(),
182            positive: core::default::Default::default(),
183            negative: core::default::Default::default(),
184            filter: core::default::Default::default(),
185            limit: core::default::Default::default(),
186            with_payload: core::default::Default::default(),
187            params: core::default::Default::default(),
188            score_threshold: core::default::Default::default(),
189            offset: core::default::Default::default(),
190            using: core::default::Default::default(),
191            with_vectors: core::default::Default::default(),
192            lookup_from: core::default::Default::default(),
193            read_consistency: core::default::Default::default(),
194            strategy: core::default::Default::default(),
195            positive_vectors: core::default::Default::default(),
196            negative_vectors: core::default::Default::default(),
197            timeout: core::default::Default::default(),
198            shard_key_selector: core::default::Default::default(),
199        }
200    }
201}
202
203impl From<RecommendPointsBuilder> for RecommendPoints {
204    fn from(value: RecommendPointsBuilder) -> Self {
205        value.build_inner().unwrap_or_else(|_| {
206            panic!(
207                "Failed to convert {0} to {1}",
208                "RecommendPointsBuilder", "RecommendPoints"
209            )
210        })
211    }
212}
213
214impl RecommendPointsBuilder {
215    /// Builds the desired type. Can often be omitted.
216    pub fn build(self) -> RecommendPoints {
217        self.build_inner().unwrap_or_else(|_| {
218            panic!(
219                "Failed to build {0} into {1}",
220                "RecommendPointsBuilder", "RecommendPoints"
221            )
222        })
223    }
224}
225
226impl RecommendPointsBuilder {
227    pub(crate) fn empty() -> Self {
228        Self::create_empty()
229    }
230}
231
232/// Error type for RecommendPointsBuilder
233#[non_exhaustive]
234#[derive(Debug)]
235pub enum RecommendPointsBuilderError {
236    /// Uninitialized field
237    UninitializedField(&'static str),
238    /// Custom validation error
239    ValidationError(String),
240}
241
242// Implementing the Display trait for better error messages
243impl std::fmt::Display for RecommendPointsBuilderError {
244    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
245        match self {
246            Self::UninitializedField(field) => {
247                write!(f, "`{field}` must be initialized")
248            }
249            Self::ValidationError(error) => write!(f, "{error}"),
250        }
251    }
252}
253
254// Implementing the Error trait
255impl std::error::Error for RecommendPointsBuilderError {}
256
257// Implementing From trait for conversion from UninitializedFieldError
258impl From<derive_builder::UninitializedFieldError> for RecommendPointsBuilderError {
259    fn from(error: derive_builder::UninitializedFieldError) -> Self {
260        Self::UninitializedField(error.field_name())
261    }
262}
263
264// Implementing From trait for conversion from String
265impl From<String> for RecommendPointsBuilderError {
266    fn from(error: String) -> Self {
267        Self::ValidationError(error)
268    }
269}