Skip to main content

qdrant_client/builders/
recommend_points_builder.rs

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