Skip to main content

qdrant_client/builders/
discover_points_builder.rs

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