Skip to main content

qdrant_client/builders/
discover_points_builder.rs

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