qdrant_client/builders/
search_points_builder.rs

1use crate::grpc_macros::convert_option;
2use crate::qdrant::*;
3
4#[derive(Clone)]
5pub struct SearchPointsBuilder {
6    /// name of the collection
7    pub(crate) collection_name: Option<String>,
8    /// vector
9    pub(crate) vector: Option<Vec<f32>>,
10    /// Filter conditions - return only those points that satisfy the specified conditions
11    pub(crate) filter: Option<Option<Filter>>,
12    /// Max number of result
13    pub(crate) limit: Option<u64>,
14    /// Options for specifying which payload to include or not
15    with_payload: Option<with_payload_selector::SelectorOptions>,
16    /// Search config
17    pub(crate) params: Option<Option<SearchParams>>,
18    /// If provided - cut off results with worse scores
19    pub(crate) score_threshold: Option<Option<f32>>,
20    /// Offset of the result
21    pub(crate) offset: Option<Option<u64>>,
22    /// Which vector to use for search, if not specified - use default vector
23    pub(crate) vector_name: Option<Option<String>>,
24    /// Options for specifying which vectors to include into response
25    with_vectors: Option<with_vectors_selector::SelectorOptions>,
26    /// Options for specifying read consistency guarantees
27    read_consistency: Option<read_consistency::Value>,
28    /// If set, overrides global timeout setting for this request. Unit is seconds.
29    pub(crate) timeout: Option<Option<u64>>,
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    pub(crate) sparse_indices: Option<Option<SparseIndices>>,
33}
34
35impl SearchPointsBuilder {
36    /// name of the collection
37    #[allow(unused_mut)]
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    /// vector
44    #[allow(unused_mut)]
45    pub fn vector(self, value: Vec<f32>) -> Self {
46        let mut new = self;
47        new.vector = Option::Some(value);
48        new
49    }
50    /// Filter conditions - return only those points that satisfy the specified conditions
51    #[allow(unused_mut)]
52    pub fn filter<VALUE: core::convert::Into<Filter>>(self, value: VALUE) -> Self {
53        let mut new = self;
54        new.filter = Option::Some(Option::Some(value.into()));
55        new
56    }
57    /// Max number of result
58    #[allow(unused_mut)]
59    pub fn limit(self, value: u64) -> Self {
60        let mut new = self;
61        new.limit = Option::Some(value);
62        new
63    }
64    /// Options for specifying which payload to include or not
65    #[allow(unused_mut)]
66    pub fn with_payload<VALUE: core::convert::Into<with_payload_selector::SelectorOptions>>(
67        self,
68        value: VALUE,
69    ) -> Self {
70        let mut new = self;
71        new.with_payload = Option::Some(value.into());
72        new
73    }
74    /// Search config
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    /// If provided - cut off results with worse scores
82    #[allow(unused_mut)]
83    pub fn score_threshold(self, value: f32) -> Self {
84        let mut new = self;
85        new.score_threshold = Option::Some(Option::Some(value));
86        new
87    }
88    /// Offset of the result
89    #[allow(unused_mut)]
90    pub fn offset(self, value: u64) -> Self {
91        let mut new = self;
92        new.offset = Option::Some(Option::Some(value));
93        new
94    }
95    /// Which vector to use for search, if not specified - use default vector
96    #[allow(unused_mut)]
97    pub fn vector_name<VALUE: core::convert::Into<String>>(self, value: VALUE) -> Self {
98        let mut new = self;
99        new.vector_name = Option::Some(Option::Some(value.into()));
100        new
101    }
102    /// Options for specifying which vectors to include into 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 read consistency guarantees
113    #[allow(unused_mut)]
114    pub fn read_consistency<VALUE: core::convert::Into<read_consistency::Value>>(
115        self,
116        value: VALUE,
117    ) -> Self {
118        let mut new = self;
119        new.read_consistency = Option::Some(value.into());
120        new
121    }
122    /// If set, overrides global timeout setting for this request. Unit is seconds.
123    #[allow(unused_mut)]
124    pub fn timeout(self, value: u64) -> Self {
125        let mut new = self;
126        new.timeout = Option::Some(Option::Some(value));
127        new
128    }
129    /// Specify in which shards to look for the points, if not specified - look in all shards
130    #[allow(unused_mut)]
131    pub fn shard_key_selector<VALUE: core::convert::Into<ShardKeySelector>>(
132        self,
133        value: VALUE,
134    ) -> Self {
135        let mut new = self;
136        new.shard_key_selector = Option::Some(Option::Some(value.into()));
137        new
138    }
139    #[allow(unused_mut)]
140    pub fn sparse_indices<VALUE: core::convert::Into<SparseIndices>>(self, value: VALUE) -> Self {
141        let mut new = self;
142        new.sparse_indices = Option::Some(Option::Some(value.into()));
143        new
144    }
145
146    fn build_inner(self) -> Result<SearchPoints, SearchPointsBuilderError> {
147        Ok(SearchPoints {
148            collection_name: match self.collection_name {
149                Some(value) => value,
150                None => {
151                    return Result::Err(core::convert::Into::into(
152                        ::derive_builder::UninitializedFieldError::from("collection_name"),
153                    ));
154                }
155            },
156            vector: match self.vector {
157                Some(value) => value,
158                None => {
159                    return Result::Err(core::convert::Into::into(
160                        ::derive_builder::UninitializedFieldError::from("vector"),
161                    ));
162                }
163            },
164            filter: self.filter.unwrap_or_default(),
165            limit: match self.limit {
166                Some(value) => value,
167                None => {
168                    return Result::Err(core::convert::Into::into(
169                        ::derive_builder::UninitializedFieldError::from("limit"),
170                    ));
171                }
172            },
173            with_payload: { convert_option(&self.with_payload) },
174            params: self.params.unwrap_or_default(),
175            score_threshold: self.score_threshold.unwrap_or_default(),
176            offset: self.offset.unwrap_or_default(),
177            vector_name: self.vector_name.unwrap_or_default(),
178            with_vectors: { convert_option(&self.with_vectors) },
179            read_consistency: { convert_option(&self.read_consistency) },
180            timeout: self.timeout.unwrap_or_default(),
181            shard_key_selector: self.shard_key_selector.unwrap_or_default(),
182            sparse_indices: self.sparse_indices.unwrap_or_default(),
183        })
184    }
185    /// Create an empty builder, with all fields set to `None` or `PhantomData`.
186    fn create_empty() -> Self {
187        Self {
188            collection_name: core::default::Default::default(),
189            vector: core::default::Default::default(),
190            filter: core::default::Default::default(),
191            limit: core::default::Default::default(),
192            with_payload: core::default::Default::default(),
193            params: core::default::Default::default(),
194            score_threshold: core::default::Default::default(),
195            offset: core::default::Default::default(),
196            vector_name: core::default::Default::default(),
197            with_vectors: core::default::Default::default(),
198            read_consistency: core::default::Default::default(),
199            timeout: core::default::Default::default(),
200            shard_key_selector: core::default::Default::default(),
201            sparse_indices: core::default::Default::default(),
202        }
203    }
204}
205
206impl From<SearchPointsBuilder> for SearchPoints {
207    fn from(value: SearchPointsBuilder) -> Self {
208        value.build_inner().unwrap_or_else(|_| {
209            panic!(
210                "Failed to convert {0} to {1}",
211                "SearchPointsBuilder", "SearchPoints"
212            )
213        })
214    }
215}
216
217impl SearchPointsBuilder {
218    /// Builds the desired type. Can often be omitted.
219    pub fn build(self) -> SearchPoints {
220        self.build_inner().unwrap_or_else(|_| {
221            panic!(
222                "Failed to build {0} into {1}",
223                "SearchPointsBuilder", "SearchPoints"
224            )
225        })
226    }
227}
228
229impl SearchPointsBuilder {
230    pub(crate) fn empty() -> Self {
231        Self::create_empty()
232    }
233}
234
235#[non_exhaustive]
236#[derive(Debug)]
237pub enum SearchPointsBuilderError {
238    /// Uninitialized field
239    UninitializedField(&'static str),
240    /// Custom validation error
241    ValidationError(String),
242}
243
244// Implementing the Display trait for better error messages
245impl std::fmt::Display for SearchPointsBuilderError {
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 SearchPointsBuilderError {}
258
259// Implementing From trait for conversion from UninitializedFieldError
260impl From<derive_builder::UninitializedFieldError> for SearchPointsBuilderError {
261    fn from(error: derive_builder::UninitializedFieldError) -> Self {
262        Self::UninitializedField(error.field_name())
263    }
264}
265
266// Implementing From trait for conversion from String
267impl From<String> for SearchPointsBuilderError {
268    fn from(error: String) -> Self {
269        Self::ValidationError(error)
270    }
271}