Skip to main content

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