-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.rs
More file actions
49 lines (41 loc) · 1.28 KB
/
basic.rs
File metadata and controls
49 lines (41 loc) · 1.28 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
use serde_json::json;
use surgedb_core::{Config, DistanceMetric, VectorDb};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Initialize a new in-memory database
let config = Config {
dimensions: 3,
distance_metric: DistanceMetric::Cosine,
..Default::default()
};
let mut db = VectorDb::new(config)?;
println!("SurgeDB Rust Example");
println!("-------------------");
// 2. Insert some vectors with metadata
db.insert(
"apple",
&[1.0, 0.0, 0.0],
Some(json!({"type": "fruit", "color": "red"})),
)?;
db.insert(
"banana",
&[0.0, 1.0, 0.0],
Some(json!({"type": "fruit", "color": "yellow"})),
)?;
db.insert(
"truck",
&[0.0, 0.0, 1.0],
Some(json!({"type": "vehicle", "color": "blue"})),
)?;
println!("Inserted 3 vectors.");
// 3. Search for the most similar vector
let query = [0.9, 0.1, 0.0];
println!("\nSearching for: {:?}", query);
let results = db.search(&query, 1, None)?;
if let Some((id, distance, metadata)) = results.first() {
println!("Match Found!");
println!("ID: {}", id);
println!("Distance: {:.4}", distance);
println!("Metadata: {}", metadata.as_ref().unwrap());
}
Ok(())
}