qdrant_client/builders/
search_points_builder.rs

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