tcvectordb 0.1.9

Rust SDK for Tencent Cloud VectorDB
Documentation
use tcvectordb::{VectorDBClient, Result, enums::{ReadConsistency, IndexType, MetricType, FieldType}, Index, VectorIndex, FilterIndex, index::HNSWParams};

#[tokio::main]
async fn main() -> Result<()> {
    let url = std::env::var("VECTORDB_URL").expect("VECTORDB_URL not set");
    let username = std::env::var("VECTORDB_USERNAME").expect("VECTORDB_USERNAME not set");
    let api_key = std::env::var("VECTORDB_API_KEY").expect("VECTORDB_API_KEY not set");

    let client = VectorDBClient::new(&url, &username, &api_key, ReadConsistency::EventualConsistency, 30)?;
    println!("✅ Client created successfully");

    // 使用已存在的数据库
    let db_name = "advanced_search_test";
    println!("\n🔍 Getting database: {}", db_name);
    
    let db = match client.database(db_name).await {
        Ok(db) => {
            println!("✅ Database '{}' found", db_name);
            db
        }
        Err(e) => {
            println!("❌ Database not found: {}", e);
            println!("🔧 Creating database: {}", db_name);
            client.create_database(db_name).await?
        }
    };

    // 创建简单的索引
    let mut index = Index::new();
    index.add_vector_index(VectorIndex::new(
        "vector",
        3,
        IndexType::HNSW,
        MetricType::COSINE,
        Some(tcvectordb::index::IndexParams::HNSW(HNSWParams::new(16, 200))),
    ))?;
    index.add_filter_index(FilterIndex::new("id", FieldType::String, IndexType::PRIMARY_KEY))?;

    // 测试集合创建
    let collection_name = "debug_collection";
    println!("\n🔧 Creating collection: {}", collection_name);
    
    match db.create_collection(
        collection_name,
        3,
        0,  // 副本数设为0
        Some("Debug test collection".to_string()),
        Some(index),
        None,
        None,
    ).await {
        Ok(collection) => {
            println!("✅ Collection '{}' created successfully", collection.name());
        }
        Err(e) => {
            println!("❌ Collection creation failed: {}", e);
            return Err(e);
        }
    }

    // 清理
    println!("\n🧹 Cleaning up...");
    match db.drop_collection(collection_name).await {
        Ok(_) => {
            println!("✅ Collection '{}' dropped successfully", collection_name);
        }
        Err(e) => {
            println!("❌ Failed to drop collection: {}", e);
        }
    }

    Ok(())
}