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    pub fn collection_name(self, value: String) -> Self {
41        let mut new = self;
42        new.collection_name = Option::Some(value);
43        new
44    }
45    /// Sub-requests to perform first. If present, the query will be performed on the results of the prefetches.
46    pub fn prefetch<VALUE: core::convert::Into<Vec<PrefetchQuery>>>(self, value: VALUE) -> Self {
47        let mut new = self;
48        new.prefetch = Option::Some(value.into());
49        new
50    }
51    /// Query to perform. If missing, returns points ordered by their IDs.
52    pub fn query<VALUE: core::convert::Into<Query>>(self, value: VALUE) -> Self {
53        let mut new = self;
54        new.query = Option::Some(Option::Some(value.into()));
55        new
56    }
57    /// Define which vector to use for querying. If missing, the default vector is used.
58    pub fn using<VALUE: core::convert::Into<String>>(self, value: VALUE) -> Self {
59        let mut new = self;
60        new.using = Option::Some(Option::Some(value.into()));
61        new
62    }
63    /// Filter conditions - return only those points that satisfy the specified conditions.
64    pub fn filter<VALUE: core::convert::Into<Filter>>(self, value: VALUE) -> Self {
65        let mut new = self;
66        new.filter = Option::Some(Option::Some(value.into()));
67        new
68    }
69    /// Search params for when there is no prefetch.
70    pub fn params<VALUE: core::convert::Into<SearchParams>>(self, value: VALUE) -> Self {
71        let mut new = self;
72        new.params = Option::Some(Option::Some(value.into()));
73        new
74    }
75    /// Return points with scores better than this threshold.
76    pub fn score_threshold<VALUE: core::convert::Into<f32>>(self, value: VALUE) -> Self {
77        let mut new = self;
78        new.score_threshold = Option::Some(Option::Some(value.into()));
79        new
80    }
81    /// Max number of points. Default is 10.
82    pub fn limit(self, value: u64) -> Self {
83        let mut new = self;
84        new.limit = Option::Some(Option::Some(value));
85        new
86    }
87    /// Offset of the result. Skip this many points. Default is 0.
88    pub fn offset(self, value: u64) -> Self {
89        let mut new = self;
90        new.offset = Option::Some(Option::Some(value));
91        new
92    }
93    /// Options for specifying which vectors to include into the response.
94    pub fn with_vectors<VALUE: core::convert::Into<with_vectors_selector::SelectorOptions>>(
95        self,
96        value: VALUE,
97    ) -> Self {
98        let mut new = self;
99        new.with_vectors = Option::Some(value.into());
100        new
101    }
102    /// Options for specifying which payload to include or not.
103    pub fn with_payload<VALUE: core::convert::Into<with_payload_selector::SelectorOptions>>(
104        self,
105        value: VALUE,
106    ) -> Self {
107        let mut new = self;
108        new.with_payload = 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    /// Specify in which shards to look for the points, if not specified - look in all shards.
121    pub fn shard_key_selector<VALUE: core::convert::Into<ShardKeySelector>>(
122        self,
123        value: VALUE,
124    ) -> Self {
125        let mut new = self;
126        new.shard_key_selector = Option::Some(Option::Some(value.into()));
127        new
128    }
129    /// The location to use for IDs lookup, if not specified - use the current collection and the 'using' vector
130    pub fn lookup_from<VALUE: core::convert::Into<LookupLocation>>(self, value: VALUE) -> Self {
131        let mut new = self;
132        new.lookup_from = Option::Some(Option::Some(value.into()));
133        new
134    }
135    /// If set, overrides global timeout setting for this request. Unit is seconds.
136    pub fn timeout(self, value: u64) -> Self {
137        let mut new = self;
138        new.timeout = Option::Some(Option::Some(value));
139        new
140    }
141
142    fn build_inner(self) -> Result<QueryPoints, QueryPointsBuilderError> {
143        Ok(QueryPoints {
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            prefetch: self.prefetch.unwrap_or_default(),
153            query: self.query.unwrap_or_default(),
154            using: self.using.unwrap_or_default(),
155            filter: self.filter.unwrap_or_default(),
156            params: self.params.unwrap_or_default(),
157            score_threshold: self.score_threshold.unwrap_or_default(),
158            limit: self.limit.unwrap_or_default(),
159            offset: self.offset.unwrap_or_default(),
160            with_vectors: { convert_option(&self.with_vectors) },
161            with_payload: { convert_option(&self.with_payload) },
162            read_consistency: { convert_option(&self.read_consistency) },
163            shard_key_selector: self.shard_key_selector.unwrap_or_default(),
164            lookup_from: self.lookup_from.unwrap_or_default(),
165            timeout: self.timeout.unwrap_or_default(),
166        })
167    }
168    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
169    fn create_empty() -> Self {
170        Self {
171            collection_name: core::default::Default::default(),
172            prefetch: core::default::Default::default(),
173            query: core::default::Default::default(),
174            using: core::default::Default::default(),
175            filter: core::default::Default::default(),
176            params: core::default::Default::default(),
177            score_threshold: core::default::Default::default(),
178            limit: core::default::Default::default(),
179            offset: core::default::Default::default(),
180            with_vectors: core::default::Default::default(),
181            with_payload: core::default::Default::default(),
182            read_consistency: core::default::Default::default(),
183            shard_key_selector: core::default::Default::default(),
184            lookup_from: core::default::Default::default(),
185            timeout: core::default::Default::default(),
186        }
187    }
188}
189
190impl From<QueryPointsBuilder> for QueryPoints {
191    fn from(value: QueryPointsBuilder) -> Self {
192        value.build_inner().unwrap_or_else(|_| {
193            panic!(
194                "Failed to convert {0} to {1}",
195                "QueryPointsBuilder", "QueryPoints"
196            )
197        })
198    }
199}
200
201impl QueryPointsBuilder {
202    /// Builds the desired type. Can often be omitted.
203    pub fn build(self) -> QueryPoints {
204        self.build_inner().unwrap_or_else(|_| {
205            panic!(
206                "Failed to build {0} into {1}",
207                "QueryPointsBuilder", "QueryPoints"
208            )
209        })
210    }
211}
212
213impl QueryPointsBuilder {
214    pub(crate) fn empty() -> Self {
215        Self::create_empty()
216    }
217}
218
219/// Error type for QueryPointsBuilder
220#[non_exhaustive]
221#[derive(Debug)]
222pub enum QueryPointsBuilderError {
223    /// Uninitialized field
224    UninitializedField(&'static str),
225    /// Custom validation error
226    ValidationError(String),
227}
228
229// Implementing the From trait for conversion from UninitializedFieldError
230impl From<derive_builder::UninitializedFieldError> for QueryPointsBuilderError {
231    fn from(error: derive_builder::UninitializedFieldError) -> Self {
232        Self::UninitializedField(error.field_name())
233    }
234}
235
236// Implementing the From trait for conversion from String
237impl From<String> for QueryPointsBuilderError {
238    fn from(error: String) -> Self {
239        Self::ValidationError(error)
240    }
241}
242
243// Implementing the Display trait for better error messages
244impl std::fmt::Display for QueryPointsBuilderError {
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 QueryPointsBuilderError {}