forked from milvus-io/milvus-sdk-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_example.rs
More file actions
135 lines (110 loc) · 4.38 KB
/
index_example.rs
File metadata and controls
135 lines (110 loc) · 4.38 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use milvus::{
client::Client,
data::FieldColumn,
index::{IndexParams, IndexType, MetricType},
options::LoadOptions,
query::QueryOptions,
schema::{CollectionSchemaBuilder, FieldSchema},
};
use rand::Rng;
use std::collections::HashMap;
const DIM: i64 = 8;
const COLLECTION_NAME: &str = "hello_milvus";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new("http://localhost:19530").await?;
if client.has_collection(COLLECTION_NAME).await? {
println!("Dropping existing collection: {}", COLLECTION_NAME);
client.drop_collection(COLLECTION_NAME).await?;
}
let mut schema = CollectionSchemaBuilder::new(COLLECTION_NAME, "Hello Milvus collection");
schema
.add_field(FieldSchema::new_primary_int64("id", "", true)) // is_primary = true
.add_field(FieldSchema::new_float_vector("embeddings", "", DIM))
.add_field(FieldSchema::new_varchar("title", "", 64))
.enable_dynamic_field();
let schema = schema.build()?;
println!("Creating collection");
client.create_collection(schema.clone(), None).await?;
println!("Start inserting entities");
let mut rng = rand::thread_rng();
let mut embeddings_data = Vec::new();
let mut title_data = Vec::new();
for i in 1..=6 {
embeddings_data.extend((0..DIM).map(|_| rng.gen::<f32>()));
title_data.push(format!("t{}", i));
}
let embeddings_col = FieldColumn::new(schema.get_field("embeddings").unwrap(), embeddings_data);
let title_col = FieldColumn::new(schema.get_field("title").unwrap(), title_data);
let insert_result = client
.insert(COLLECTION_NAME, vec![embeddings_col, title_col], None)
.await?;
println!("Inserting entities done");
println!("Insert result: {:?}", insert_result);
client.flush(COLLECTION_NAME).await?;
println!("Start create index for embeddings");
let index_params = IndexParams::new(
"embeddings_index".to_string(),
IndexType::IvfFlat,
MetricType::L2,
HashMap::from([("nlist".to_string(), "32".to_string())]),
);
client
.create_index(COLLECTION_NAME, "embeddings", index_params)
.await?;
println!("Start create index for title");
let title_index_params = IndexParams::new(
"my_trie".to_string(),
IndexType::Trie,
MetricType::L2,
HashMap::new(),
);
client
.create_index(COLLECTION_NAME, "title", title_index_params)
.await?;
let index_names = client.list_indexes(COLLECTION_NAME, None).await?;
println!("Index names for {}: {:?}", COLLECTION_NAME, index_names);
for index_name in &index_names {
let index_info = client.describe_index(COLLECTION_NAME, index_name).await?;
println!("Index info for index {}: {:?}", index_name, index_info);
}
println!("Start load collection");
client
.load_collection(COLLECTION_NAME, Some(LoadOptions::default()))
.await?;
println!("Start query by specifying primary keys");
let query_options = QueryOptions::default();
let query_results = client
.query(COLLECTION_NAME, "id == 2", &query_options)
.await?;
if let Some(result) = query_results.first() {
println!("Query result: {:?}", result);
}
println!("Start query by specifying filtering expression");
let query_results = client
.query(COLLECTION_NAME, "title == 't2'", &query_options)
.await?;
for ret in query_results {
println!("Query result: {:?}", ret);
}
let field_index_names = client
.list_indexes(COLLECTION_NAME, Some("embeddings"))
.await?;
println!(
"Index names for {}'s field embeddings: {:?}",
COLLECTION_NAME, field_index_names
);
println!("Try to drop index");
client.release_collection(COLLECTION_NAME).await?;
match client.drop_index(COLLECTION_NAME, "my_trie").await {
Ok(_) => println!("Successfully dropped index for title field"),
Err(e) => println!("Caught error when dropping index: {}", e),
}
match client.drop_index(COLLECTION_NAME, "my_trie").await {
Ok(_) => println!("Successfully dropped index for title field"),
Err(e) => println!("Caught error when dropping index: {}", e),
}
client.drop_collection(COLLECTION_NAME).await?;
println!("Example completed successfully!");
Ok(())
}