Skip to main content

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//! # Query (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::QueryPointsBuilder;
97//!
98//!# async fn query(client: &Qdrant)
99//!# -> Result<(), QdrantError> {
100//! let query_request = QueryPointsBuilder::new("my_collection") // Collection name
101//!     .query(vec![0.0_f32; 512])                               // Query vector
102//!     .limit(4)                                                // Search limit, number of results to return
103//!     .with_payload(true);                                     // Include full payload in the result
104//!
105//! let response = client.query(query_request).await?;
106//!# Ok(())
107//!# }
108//! ```
109//!
110//! The parameter for [`QueryPointsBuilder::new()`](qdrant::QueryPointsBuilder::new) is pretty
111//! straightforward: the name of the collection to query in. It is combined with other
112//! [functions](qdrant::QueryPointsBuilder#implementations) to further specialize your query to
113//! cover all query flavors.
114//!
115//! In this example [`query(...)`](qdrant::QueryPointsBuilder::query) is used to enable vector
116//! similarity search on the given vector. [`limit(4)`](qdrant::QueryPointsBuilder::limit)
117//! specifies we only want up to 4 top-k results. And
118//! [`with_payload(true)`](qdrant::QueryPointsBuilder::with_payload) tells Qdrant to also return
119//! the (full) payload data for each point. [`filter()`](qdrant::QueryPointsBuilder::filter) is
120//! also commonly used to apply payload based filtering. See the [`Filter`](qdrant::Filter)
121//! documentation for details.
122//!
123//! Documentation: <https://qdrant.tech/documentation/concepts/search/#query-api>
124
125#![doc(html_logo_url = "https://qdrant.tech/favicon/android-chrome-192x192.png")]
126#![doc(issue_tracker_base_url = "https://github.com/qdrant/rust-client/issues/")]
127
128// Generated Qdrant API types
129/// API types
130#[allow(deprecated, clippy::all)]
131#[rustfmt::skip]
132pub mod qdrant;
133
134// Internal modules
135mod auth;
136mod builder_ext;
137mod builder_types;
138mod builders;
139mod channel_pool;
140mod expressions;
141mod filters;
142mod grpc_conversions;
143mod grpc_macros;
144mod manual_builder;
145mod payload;
146mod qdrant_client;
147#[cfg(feature = "serde")]
148mod serde_impl;
149
150#[cfg(feature = "serde")]
151pub mod serde_deser;
152
153// Re-exports
154pub use crate::payload::Payload;
155pub use crate::qdrant_client::error::QdrantError;
156pub use crate::qdrant_client::{Qdrant, QdrantBuilder};
157
158/// Client configuration
159pub mod config {
160    pub use crate::qdrant_client::config::{
161        AsOptionApiKey, AsTimeout, CompressionEncoding, QdrantConfig,
162    };
163}
164
165#[cfg(test)]
166mod tests {
167    use std::collections::HashMap;
168
169    use crate::builders::CreateCollectionBuilder;
170    use crate::payload::Payload;
171    use crate::qdrant::value::Kind::*;
172    use crate::qdrant::{
173        Condition, CreateFieldIndexCollection, DeletePayloadPointsBuilder, DeletePointsBuilder,
174        Distance, FieldType, Filter, GetPointsBuilder, ListValue, PointStruct, SearchPointsBuilder,
175        SetPayloadPointsBuilder, SnapshotDownloadBuilder, Struct, UpsertPointsBuilder, Value,
176        VectorParamsBuilder,
177    };
178    use crate::Qdrant;
179
180    #[test]
181    fn display() {
182        let value = Value {
183            kind: Some(StructValue(Struct {
184                fields: [
185                    ("text", StringValue("Hi Qdrant!".into())),
186                    ("int", IntegerValue(42)),
187                    ("float", DoubleValue(1.23)),
188                    (
189                        "list",
190                        ListValue(ListValue {
191                            values: vec![Value {
192                                kind: Some(NullValue(0)),
193                            }],
194                        }),
195                    ),
196                    (
197                        "struct",
198                        StructValue(Struct {
199                            fields: [(
200                                "bool".into(),
201                                Value {
202                                    kind: Some(BoolValue(true)),
203                                },
204                            )]
205                            .into(),
206                        }),
207                    ),
208                ]
209                .into_iter()
210                .map(|(k, v)| (k.into(), Value { kind: Some(v) }))
211                .collect(),
212            })),
213        };
214        let text = format!("{value}");
215        assert!([
216            "\"float\":1.23",
217            "\"list\":[null]",
218            "\"struct\":{\"bool\":true}",
219            "\"int\":42",
220            "\"text\":\"Hi Qdrant!\""
221        ]
222        .into_iter()
223        .all(|item| text.contains(item)));
224    }
225
226    #[tokio::test]
227    async fn test_qdrant_queries() -> anyhow::Result<()> {
228        let client = Qdrant::from_url("http://localhost:6334")
229            .timeout(10u64) // larger timeout to account for the slow snapshot creation
230            .build()?;
231
232        let health = client.health_check().await?;
233        println!("{health:?}");
234
235        let collections_list = client.list_collections().await?;
236        println!("{collections_list:?}");
237
238        let collection_name = "test_qdrant_queries";
239        client.delete_collection(collection_name).await?;
240
241        client
242            .create_collection(
243                CreateCollectionBuilder::new(collection_name)
244                    .vectors_config(VectorParamsBuilder::new(10, Distance::Cosine)),
245            )
246            .await?;
247
248        let exists = client.collection_exists(collection_name).await?;
249        assert!(exists);
250
251        let collection_info = client.collection_info(collection_name).await?;
252        println!("{collection_info:#?}");
253
254        let mut sub_payload = Payload::new();
255        sub_payload.insert("foo", "Not bar");
256
257        let payload: Payload = vec![
258            ("foo", "Bar".into()),
259            ("bar", 12.into()),
260            ("sub_payload", sub_payload.into()),
261        ]
262        .into_iter()
263        .collect::<HashMap<_, Value>>()
264        .into();
265
266        let points = vec![PointStruct::new(0, vec![12.; 10], payload)];
267        client
268            .upsert_points(UpsertPointsBuilder::new(collection_name, points).wait(true))
269            .await?;
270
271        let mut search_points =
272            SearchPointsBuilder::new(collection_name, vec![11.; 10], 10).build();
273
274        // Keyword filter result
275        search_points.filter = Some(Filter::all([Condition::matches("foo", "Bar".to_string())]));
276        let search_result = client.search_points(search_points.clone()).await?;
277        eprintln!("search_result = {search_result:#?}");
278        assert!(!search_result.result.is_empty());
279
280        // Existing implementations full text search filter result (`Condition::matches`)
281        search_points.filter = Some(Filter::all([Condition::matches(
282            "sub_payload.foo",
283            "Not ".to_string(),
284        )]));
285        let search_result = client.search_points(search_points.clone()).await?;
286        eprintln!("search_result = {search_result:#?}");
287        assert!(!search_result.result.is_empty());
288
289        // Full text search filter result (`Condition::matches_text`)
290        search_points.filter = Some(Filter::all([Condition::matches_text(
291            "sub_payload.foo",
292            "Not",
293        )]));
294        let search_result = client.search_points(search_points).await?;
295        eprintln!("search_result = {search_result:#?}");
296        assert!(!search_result.result.is_empty());
297
298        // Override payload of the existing point
299        let new_payload: Payload = vec![("foo", "BAZ".into())]
300            .into_iter()
301            .collect::<HashMap<_, Value>>()
302            .into();
303
304        let payload_result = client
305            .set_payload(
306                SetPayloadPointsBuilder::new(collection_name, new_payload).points_selector([0]),
307            )
308            .await?;
309        eprintln!("payload_result = {payload_result:#?}");
310
311        // Delete some payload fields
312        client
313            .delete_payload(
314                DeletePayloadPointsBuilder::new(collection_name, ["sub_payload".into()])
315                    .points_selector([0]),
316            )
317            .await?;
318
319        let get_points_result = client
320            .get_points(
321                GetPointsBuilder::new(collection_name, [0.into()])
322                    .with_vectors(true)
323                    .with_payload(true),
324            )
325            .await?;
326        eprintln!("get_points_result = {get_points_result:#?}");
327        assert_eq!(get_points_result.result.len(), 1);
328        let point = get_points_result.result[0].clone();
329        assert!(point.payload.contains_key("foo"));
330        assert!(!point.payload.contains_key("sub_payload"));
331
332        let delete_points_result = client
333            .delete_points(
334                DeletePointsBuilder::new(collection_name)
335                    .points([0])
336                    .wait(true),
337            )
338            .await?;
339        eprintln!("delete_points_result = {delete_points_result:#?}");
340
341        // Access raw point api with client
342        client
343            .with_points_client(|mut client| async move {
344                client
345                    .create_field_index(CreateFieldIndexCollection {
346                        collection_name: collection_name.to_string(),
347                        wait: None,
348                        field_name: "foo".to_string(),
349                        field_type: Some(FieldType::Keyword as i32),
350                        field_index_params: None,
351                        ordering: None,
352                        timeout: None,
353                    })
354                    .await
355            })
356            .await?;
357
358        // slow operation
359        let snapshot_result = client.create_snapshot(collection_name).await?;
360        eprintln!("snapshot_result = {snapshot_result:#?}");
361
362        #[cfg(feature = "download_snapshots")]
363        client
364            .download_snapshot(SnapshotDownloadBuilder::new("test.tar", collection_name))
365            .await?;
366
367        Ok(())
368    }
369}