qdrant_client/lib.rs
1//! The [Qdrant](https://qdrant.tech/) - High-Performance Vector Search at Scale - client for Rust.
2//!
3//! This crate connects to your Qdrant server over gRPC and provides an easy to use API interface
4//! for it.
5//!
6//! # Connect
7//!
8//! First you'll need to [set up](Qdrant#set-up) a [`Qdrant`] client, used to connect to a Qdrant
9//! instance:
10//!
11//! ```no_run
12//! use qdrant_client::Qdrant;
13//!# use qdrant_client::QdrantError;
14//!
15//!# fn establish_connection(url: &str) -> Result<Qdrant, QdrantError> {
16//! let client = Qdrant::from_url("http://localhost:6334")
17//! .api_key(std::env::var("QDRANT_API_KEY"))
18//! .build()?;
19//!# Ok(client)
20//!# }
21//! ```
22//!
23//! # Create collection
24//!
25//! Qdrant works with [Collections ⧉ ](https://qdrant.tech/documentation/concepts/collections/) of
26//! [Points ⧉ ](https://qdrant.tech/documentation/concepts/points/). To add vector data, you first
27//! [create a collection](Qdrant::create_collection):
28//!
29//! ```no_run
30//!# use qdrant_client::{Qdrant, QdrantError};
31//! use qdrant_client::qdrant::{CreateCollectionBuilder, Distance, VectorParamsBuilder};
32//!
33//!# async fn create_collection(client: &Qdrant)
34//!# -> Result<(), QdrantError> {
35//! let response = client
36//! .create_collection(
37//! CreateCollectionBuilder::new("my_collection")
38//! .vectors_config(VectorParamsBuilder::new(512, Distance::Cosine)),
39//! )
40//! .await?;
41//!# Ok(())
42//!# }
43//! ```
44//!
45//! The most interesting parts are the two arguments of
46//! [`VectorParamsBuilder::new`](qdrant::VectorParamsBuilder::new). The first one (`512`) is the
47//! length of vectors to store and the second one ([`Distance::Cosine`](qdrant::Distance::Cosine))
48//! is the Distance, which is the [`Distance`](qdrant::Distance) measure to gauge similarity for
49//! the nearest neighbors search.
50//!
51//! Documentation: <https://qdrant.tech/documentation/concepts/collections/#create-a-collection>
52//!
53//! # Upsert points
54//!
55//! Now we have a collection, we can insert (or rather upsert) points.
56//! Points have an id, one or more vectors and a payload.
57//! We can usually do that in bulk, but for this example, we'll add a
58//! single point:
59//!
60//! ```no_run
61//!# use qdrant_client::{Qdrant, QdrantError};
62//! use qdrant_client::qdrant::{PointStruct, UpsertPointsBuilder};
63//!
64//!# async fn do_upsert(client: &Qdrant)
65//!# -> Result<(), QdrantError> {
66//! let points = vec![
67//! PointStruct::new(
68//! 42, // Unique point ID
69//! vec![0.0_f32; 512], // Vector to upsert
70//! // Attached payload
71//! [
72//! ("great", true.into()),
73//! ("level", 9000.into()),
74//! ("text", "Hi Qdrant!".into()),
75//! ("list", vec![1.234f32, 0.815].into()),
76//! ],
77//! ),
78//! ];
79//!
80//! let response = client
81//! .upsert_points(UpsertPointsBuilder::new("my_collection", points))
82//! .await?;
83//!# Ok(())
84//!# }
85//! ```
86//!
87//! Documentation: <https://qdrant.tech/documentation/concepts/points/#upload-points>
88//!
89//! # Search
90//!
91//! Finally, we can retrieve points in various ways, the common one being a plain similarity
92//! search:
93//!
94//! ```no_run
95//!# use qdrant_client::{Qdrant, QdrantError};
96//! use qdrant_client::qdrant::SearchPointsBuilder;
97//!
98//!# async fn search(client: &Qdrant)
99//!# -> Result<(), QdrantError> {
100//! let search_request = SearchPointsBuilder::new(
101//! "my_collection", // Collection name
102//! vec![0.0_f32; 512], // Search vector
103//! 4, // Search limit, number of results to return
104//! ).with_payload(true);
105//!
106//! let response = client.search_points(search_request).await?;
107//!# Ok(())
108//!# }
109//! ```
110//!
111//! The parameter for [`SearchPointsBuilder::new()`](qdrant::SearchPointsBuilder::new) constructor
112//! are pretty straightforward: name of the collection, the vector and how many top-k results to
113//! return. The [`with_payload(true)`](qdrant::SearchPointsBuilder::with_payload) call tells qdrant
114//! to also return the (full) payload data for each point. You can also add a
115//! [`filter()`](qdrant::SearchPointsBuilder::filter) call to the
116//! [`SearchPointsBuilder`](qdrant::SearchPointsBuilder) to filter the result. See the
117//! [`Filter`](qdrant::Filter) documentation for details.
118//!
119//! Documentation: <https://qdrant.tech/documentation/concepts/search/>
120
121#![doc(html_logo_url = "https://qdrant.tech/favicon/android-chrome-192x192.png")]
122#![doc(issue_tracker_base_url = "https://github.com/qdrant/rust-client/issues/")]
123
124// Generated Qdrant API types
125/// API types
126#[allow(deprecated, clippy::all)]
127#[rustfmt::skip]
128pub mod qdrant;
129
130// Internal modules
131mod auth;
132mod builder_ext;
133mod builder_types;
134mod builders;
135mod channel_pool;
136mod expressions;
137mod filters;
138mod grpc_conversions;
139mod grpc_macros;
140mod manual_builder;
141mod payload;
142mod qdrant_client;
143// Deprecated modules
144/// Deprecated Qdrant client
145#[deprecated(
146 since = "1.10.0",
147 note = "use new client at `qdrant_client::Qdrant` instead"
148)]
149#[doc(hidden)]
150pub mod client;
151/// Deprecated error type
152#[deprecated(
153 since = "1.10.0",
154 note = "use new error type at `qdrant_client::Error` instead"
155)]
156#[doc(hidden)]
157pub mod error;
158/// Deprecated prelude
159#[deprecated(since = "1.10.0", note = "use types directly")]
160#[doc(hidden)]
161pub mod prelude;
162/// Deprecated serde helper
163#[cfg(feature = "serde")]
164#[deprecated(since = "1.10.0", note = "use `Payload::try_from` instead")]
165#[doc(hidden)]
166pub mod serde;
167
168// Re-exports
169pub use crate::payload::Payload;
170pub use crate::qdrant_client::error::QdrantError;
171pub use crate::qdrant_client::{Qdrant, QdrantBuilder};
172
173/// Client configuration
174pub mod config {
175 pub use crate::qdrant_client::config::{
176 AsOptionApiKey, AsTimeout, CompressionEncoding, QdrantConfig,
177 };
178}
179
180#[cfg(test)]
181mod tests {
182 use std::collections::HashMap;
183
184 use crate::builders::CreateCollectionBuilder;
185 use crate::payload::Payload;
186 use crate::qdrant::value::Kind::*;
187 use crate::qdrant::{
188 Condition, CreateFieldIndexCollection, DeletePayloadPointsBuilder, DeletePointsBuilder,
189 Distance, FieldType, Filter, GetPointsBuilder, ListValue, PointStruct, SearchPointsBuilder,
190 SetPayloadPointsBuilder, SnapshotDownloadBuilder, Struct, UpsertPointsBuilder, Value,
191 VectorParamsBuilder,
192 };
193 use crate::Qdrant;
194
195 #[test]
196 fn display() {
197 let value = Value {
198 kind: Some(StructValue(Struct {
199 fields: [
200 ("text", StringValue("Hi Qdrant!".into())),
201 ("int", IntegerValue(42)),
202 ("float", DoubleValue(1.23)),
203 (
204 "list",
205 ListValue(ListValue {
206 values: vec![Value {
207 kind: Some(NullValue(0)),
208 }],
209 }),
210 ),
211 (
212 "struct",
213 StructValue(Struct {
214 fields: [(
215 "bool".into(),
216 Value {
217 kind: Some(BoolValue(true)),
218 },
219 )]
220 .into(),
221 }),
222 ),
223 ]
224 .into_iter()
225 .map(|(k, v)| (k.into(), Value { kind: Some(v) }))
226 .collect(),
227 })),
228 };
229 let text = format!("{}", value);
230 assert!([
231 "\"float\":1.23",
232 "\"list\":[null]",
233 "\"struct\":{\"bool\":true}",
234 "\"int\":42",
235 "\"text\":\"Hi Qdrant!\""
236 ]
237 .into_iter()
238 .all(|item| text.contains(item)));
239 }
240
241 #[tokio::test]
242 async fn test_qdrant_queries() -> anyhow::Result<()> {
243 let client = Qdrant::from_url("http://localhost:6334")
244 .timeout(10u64) // larger timeout to account for the slow snapshot creation
245 .build()?;
246
247 let health = client.health_check().await?;
248 println!("{:?}", health);
249
250 let collections_list = client.list_collections().await?;
251 println!("{:?}", collections_list);
252
253 let collection_name = "test_qdrant_queries";
254 client.delete_collection(collection_name).await?;
255
256 client
257 .create_collection(
258 CreateCollectionBuilder::new(collection_name)
259 .vectors_config(VectorParamsBuilder::new(10, Distance::Cosine)),
260 )
261 .await?;
262
263 let exists = client.collection_exists(collection_name).await?;
264 assert!(exists);
265
266 let collection_info = client.collection_info(collection_name).await?;
267 println!("{:#?}", collection_info);
268
269 let mut sub_payload = Payload::new();
270 sub_payload.insert("foo", "Not bar");
271
272 let payload: Payload = vec![
273 ("foo", "Bar".into()),
274 ("bar", 12.into()),
275 ("sub_payload", sub_payload.into()),
276 ]
277 .into_iter()
278 .collect::<HashMap<_, Value>>()
279 .into();
280
281 let points = vec![PointStruct::new(0, vec![12.; 10], payload)];
282 client
283 .upsert_points(UpsertPointsBuilder::new(collection_name, points).wait(true))
284 .await?;
285
286 let mut search_points =
287 SearchPointsBuilder::new(collection_name, vec![11.; 10], 10).build();
288
289 // Keyword filter result
290 search_points.filter = Some(Filter::all([Condition::matches("foo", "Bar".to_string())]));
291 let search_result = client.search_points(search_points.clone()).await?;
292 eprintln!("search_result = {:#?}", search_result);
293 assert!(!search_result.result.is_empty());
294
295 // Existing implementations full text search filter result (`Condition::matches`)
296 search_points.filter = Some(Filter::all([Condition::matches(
297 "sub_payload.foo",
298 "Not ".to_string(),
299 )]));
300 let search_result = client.search_points(search_points.clone()).await?;
301 eprintln!("search_result = {:#?}", search_result);
302 assert!(!search_result.result.is_empty());
303
304 // Full text search filter result (`Condition::matches_text`)
305 search_points.filter = Some(Filter::all([Condition::matches_text(
306 "sub_payload.foo",
307 "Not",
308 )]));
309 let search_result = client.search_points(search_points).await?;
310 eprintln!("search_result = {:#?}", search_result);
311 assert!(!search_result.result.is_empty());
312
313 // Override payload of the existing point
314 let new_payload: Payload = vec![("foo", "BAZ".into())]
315 .into_iter()
316 .collect::<HashMap<_, Value>>()
317 .into();
318
319 let payload_result = client
320 .set_payload(
321 SetPayloadPointsBuilder::new(collection_name, new_payload).points_selector([0]),
322 )
323 .await?;
324 eprintln!("payload_result = {:#?}", payload_result);
325
326 // Delete some payload fields
327 client
328 .delete_payload(
329 DeletePayloadPointsBuilder::new(collection_name, ["sub_payload".into()])
330 .points_selector([0]),
331 )
332 .await?;
333
334 let get_points_result = client
335 .get_points(
336 GetPointsBuilder::new(collection_name, [0.into()])
337 .with_vectors(true)
338 .with_payload(true),
339 )
340 .await?;
341 eprintln!("get_points_result = {:#?}", get_points_result);
342 assert_eq!(get_points_result.result.len(), 1);
343 let point = get_points_result.result[0].clone();
344 assert!(point.payload.contains_key("foo"));
345 assert!(!point.payload.contains_key("sub_payload"));
346
347 let delete_points_result = client
348 .delete_points(
349 DeletePointsBuilder::new(collection_name)
350 .points([0])
351 .wait(true),
352 )
353 .await?;
354 eprintln!("delete_points_result = {:#?}", delete_points_result);
355
356 // Access raw point api with client
357 client
358 .with_points_client(|mut client| async move {
359 client
360 .create_field_index(CreateFieldIndexCollection {
361 collection_name: collection_name.to_string(),
362 wait: None,
363 field_name: "foo".to_string(),
364 field_type: Some(FieldType::Keyword as i32),
365 field_index_params: None,
366 ordering: None,
367 })
368 .await
369 })
370 .await?;
371
372 // slow operation
373 let snapshot_result = client.create_snapshot(collection_name).await?;
374 eprintln!("snapshot_result = {:#?}", snapshot_result);
375
376 #[cfg(feature = "download_snapshots")]
377 client
378 .download_snapshot(SnapshotDownloadBuilder::new("test.tar", collection_name))
379 .await?;
380
381 Ok(())
382 }
383}