-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathbasic_usage.rs
More file actions
73 lines (60 loc) · 2.04 KB
/
basic_usage.rs
File metadata and controls
73 lines (60 loc) · 2.04 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
//! Basic usage example for Ruvector
//!
//! Demonstrates:
//! - Creating a database
//! - Inserting vectors
//! - Searching for similar vectors
//! - Basic configuration
use ruvector_core::{VectorDB, VectorEntry, SearchQuery, DbOptions, Result};
fn main() -> Result<()> {
println!("🚀 Ruvector Basic Usage Example\n");
// 1. Create a database
println!("1. Creating database...");
let mut options = DbOptions::default();
options.dimensions = 128;
options.storage_path = "./examples_basic.db".to_string();
let db = VectorDB::new(options)?;
println!(" ✓ Database created with 128 dimensions\n");
// 2. Insert a single vector
println!("2. Inserting single vector...");
let entry = VectorEntry {
id: Some("doc_001".to_string()),
vector: vec![0.1; 128],
metadata: None,
};
let id = db.insert(entry)?;
println!(" ✓ Inserted vector: {}\n", id);
// 3. Insert multiple vectors
println!("3. Inserting multiple vectors...");
let entries: Vec<VectorEntry> = (0..100)
.map(|i| VectorEntry {
id: Some(format!("doc_{:03}", i + 2)),
vector: vec![0.1 + (i as f32) * 0.001; 128],
metadata: None,
})
.collect();
let ids = db.insert_batch(entries)?;
println!(" ✓ Inserted {} vectors\n", ids.len());
// 4. Search for similar vectors
println!("4. Searching for similar vectors...");
let query = SearchQuery {
vector: vec![0.15; 128],
k: 5,
filter: None,
include_vectors: false,
};
let results = db.search(&query)?;
println!(" ✓ Found {} results:", results.len());
for (i, result) in results.iter().enumerate() {
println!(" {}. ID: {}, Distance: {:.6}",
i + 1, result.id, result.distance
);
}
println!();
// 5. Get database stats
println!("5. Database statistics:");
let total = db.count();
println!(" ✓ Total vectors: {}\n", total);
println!("✅ Example completed successfully!");
Ok(())
}