qdrant_client/builders/
query_points_builder.rs

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