use tcvectordb::{
enums::{FieldType, IndexType, MetricType, ReadConsistency},
index::HNSWParams,
Embedding, FilterIndex, Index, Result, VectorDBClient, VectorIndex,
};
use uuid;
#[tokio::main]
async fn main() -> Result<()> {
println!("📚 Python Equivalent Demo - Creating Collection with Embedding Config");
let url = std::env::var("VECTORDB_URL").unwrap_or_else(|_| "http://localhost:8100".to_string());
let username = std::env::var("VECTORDB_USERNAME").unwrap_or_else(|_| "root".to_string());
let api_key = std::env::var("VECTORDB_API_KEY")
.expect("VECTORDB_API_KEY environment variable is required");
let client = VectorDBClient::new(
&url,
&username,
&api_key,
ReadConsistency::EventualConsistency,
30,
)?;
println!("✅ VectorDB client created successfully");
let db_name = "db-test";
let db = client.create_database_if_not_exists(db_name).await?;
println!("✅ Database '{}' ready", db_name);
let mut index = Index::new();
index.add_filter_index(FilterIndex::new(
"id",
FieldType::String,
IndexType::PRIMARY_KEY,
))?;
index.add_vector_index(VectorIndex::new(
"vector",
768, IndexType::HNSW,
MetricType::COSINE,
Some(tcvectordb::index::IndexParams::HNSW(HNSWParams::new(
16, 200,
))),
))?;
index.add_filter_index(FilterIndex::new(
"author",
FieldType::String,
IndexType::FILTER,
))?;
index.add_filter_index(FilterIndex::new(
"tags",
FieldType::Array,
IndexType::FILTER,
))?;
index.add_filter_index(FilterIndex::new(
"bookName",
FieldType::String,
IndexType::FILTER,
))?;
let embedding = Embedding::new("vector", "text").with_model_name("bge-base-zh");
println!("🔧 Creating collection with embedding configuration...");
let collection_name = format!(
"book-emb-{}",
uuid::Uuid::new_v4().to_string().replace("-", "")[..8].to_string()
);
let collection = db
.create_collection(
&collection_name, 1, 0, Some("this is a collection of test embedding".to_string()), Some(index), Some(embedding), None, )
.await?;
println!(
"✅ Collection '{}' created successfully!",
collection.name()
);
println!("📋 Collection Information:");
println!(" - Name: {}", collection.name());
println!(" - Database: {}", collection.database().name());
println!(
" - Embedding configured: ✅ (vector_field='vector', field='text', model='bge-base-zh')"
);
println!(" - Index configured: ✅ (768-dim HNSW vector + filter indexes)");
println!("\n💡 This collection is now configured with:");
println!(" - Automatic text-to-vector embedding using bge-base-zh model");
println!(" - 768-dimensional HNSW vector index");
println!(" - Filter indexes for author, tags, and bookName fields");
println!(" - Ready for text search and hybrid search operations");
println!("\n🧹 Cleaning up...");
match db.drop_collection(&collection_name).await {
Ok(_) => println!("✅ Collection '{}' dropped", collection_name),
Err(e) => println!("⚠️ Failed to drop collection: {}", e),
}
match client.drop_database(db_name).await {
Ok(_) => println!("✅ Database '{}' dropped", db_name),
Err(e) => println!("⚠️ Failed to drop database: {}", e),
}
println!("\n🎉 Python equivalent demo completed!");
println!("📝 This Rust code is equivalent to the Python example you provided.");
Ok(())
}