-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathsearch.rs
85 lines (75 loc) · 2.42 KB
/
search.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use qdrant_client::qdrant::{
Condition, CreateCollectionBuilder, Distance, Filter, PointStruct, ScalarQuantizationBuilder,
SearchParamsBuilder, SearchPointsBuilder, UpsertPointsBuilder, VectorParamsBuilder,
};
use qdrant_client::{Payload, Qdrant, QdrantError};
#[tokio::main]
async fn main() -> Result<(), QdrantError> {
// Example of top level client
// You may also use tonic-generated client from `src/qdrant.rs`
let client = Qdrant::from_url("http://localhost:6334").build()?;
let collections_list = client.list_collections().await?;
dbg!(collections_list);
// collections_list = {
// "collections": [
// {
// "name": "test"
// }
// ]
// }
let collection_name = "test";
client.delete_collection(collection_name).await?;
client
.create_collection(
CreateCollectionBuilder::new(collection_name)
.vectors_config(VectorParamsBuilder::new(10, Distance::Cosine))
.quantization_config(ScalarQuantizationBuilder::default()),
)
.await?;
let collection_info = client.collection_info(collection_name).await?;
dbg!(collection_info);
let payload: Payload = serde_json::json!(
{
"foo": "Bar",
"bar": 12,
"baz": {
"qux": "quux"
}
}
)
.try_into()
.unwrap();
let points = vec![PointStruct::new(0, vec![12.; 10], payload)];
client
.upsert_points(UpsertPointsBuilder::new(collection_name, points))
.await?;
let search_result = client
.search_points(
SearchPointsBuilder::new(collection_name, [11.; 10], 10)
.filter(Filter::all([Condition::matches("bar", 12)]))
.with_payload(true)
.params(SearchParamsBuilder::default().exact(true)),
)
.await?;
dbg!(&search_result);
// search_result = [
// {
// "id": 0,
// "version": 0,
// "score": 1.0000001,
// "payload": {
// "bar": 12,
// "baz": {
// "qux": "quux"
// },
// "foo": "Bar"
// }
// }
// ]
let found_point = search_result.result.into_iter().next().unwrap();
let mut payload = found_point.payload;
let baz_payload = payload.remove("baz").unwrap().into_json();
println!("baz: {}", baz_payload);
// baz: {"qux":"quux"}
Ok(())
}