tcvectordb 0.1.9

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

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create client
    let client = VectorDBClient::new(
        "http://127.0.0.1:8100",
        "root",
        "your-api-key",
        ReadConsistency::EventualConsistency,
        30,
    )?;

    // Create database
    let database_name = "test_db";
    let db = client.create_database_if_not_exists(database_name).await?;
    println!("Database '{}' created or already exists", database_name);

    // Create index
    let mut index = Index::new();
    
    let vector_index = VectorIndex::new(
        "vector",
        3,
        IndexType::HNSW,
        MetricType::COSINE,
        Some(tcvectordb::index::IndexParams::HNSW(HNSWParams::new(16, 200))),
    );
    index.add_vector_index(vector_index)?;

    let id_index = FilterIndex::new("id", FieldType::String, IndexType::PRIMARY_KEY);
    index.add_filter_index(id_index)?;

    let category_index = FilterIndex::new("category", FieldType::String, IndexType::FILTER);
    index.add_filter_index(category_index)?;

    // Create collection
    let collection_name = "test_collection";
    let collection = db.create_collection_if_not_exists(
        collection_name,
        3,
        2,
        Some("Test collection".to_string()),
        Some(index),
        None,
        None,
    ).await?;
    println!("Collection '{}' created or already exists", collection_name);

    // Prepare documents
    let documents = vec![
        Document::new()
            .with_id("doc1")
            .with_vector(vec![0.1, 0.2, 0.3])
            .with_field("title", "Document 1")
            .with_field("category", "tech"),
        
        Document::new()
            .with_id("doc2")
            .with_vector(vec![0.4, 0.5, 0.6])
            .with_field("title", "Document 2")
            .with_field("category", "science"),
        
        Document::new()
            .with_id("doc3")
            .with_vector(vec![0.7, 0.8, 0.9])
            .with_field("title", "Document 3")
            .with_field("category", "tech"),
    ];

    // Upsert documents
    let upsert_result = collection.upsert(documents, None, true).await?;
    println!("Documents upserted: {:?}", upsert_result);

    // Wait for indexing
    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;

    // Query documents
    let query_results = collection.query(
        Some(vec!["doc1".to_string(), "doc2".to_string()]),
        false,
        Some(10),
        None,
        None,
        Some(vec!["id".to_string(), "title".to_string()]),
        None,
    ).await?;
    
    println!("Query results:");
    for doc in &query_results {
        println!("  ID: {:?}, Title: {:?}", doc.get_id(), doc.get("title"));
    }

    // Vector search
    let search_results = collection.search(
        vec![vec![0.1, 0.2, 0.3]],
        Some(Filter::new("category=\"tech\"")),
        None,
        false,
        10,
        None,
        None,
        None,
    ).await?;
    
    println!("Search results:");
    for batch in &search_results {
        for doc in batch {
            println!("  ID: {:?}, Title: {:?}, Score: {:?}", 
                    doc.get_id(), doc.get("title"), doc.get_score());
        }
    }

    // Count documents
    let count = collection.count(None).await?;
    println!("Total documents: {}", count);

    // Clean up
    let _drop_collection_result = db.drop_collection(collection_name).await?;
    let _drop_database_result = client.drop_database(database_name).await?;
    println!("Cleanup completed");

    Ok(())
}