Skip to main content

qdrant_client/builders/
query_points_builder.rs

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