Skip to main content

qdrant_client/builders/
search_points_builder.rs

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