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>> {
let client = VectorDBClient::new(
"http://127.0.0.1:8100", "root", "your-api-key", ReadConsistency::EventualConsistency,
30, )?;
let database_name = "book";
let db = client.create_database_if_not_exists(database_name).await?;
println!("Database '{}' created or already exists", database_name);
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 book_name_index = FilterIndex::new("bookName", FieldType::String, IndexType::FILTER);
index.add_filter_index(book_name_index)?;
let page_index = FilterIndex::new("page", FieldType::Uint64, IndexType::FILTER);
index.add_filter_index(page_index)?;
let collection_name = "book_segments";
let collection = db.create_collection_if_not_exists(
collection_name,
3, 2, Some("测试集合".to_string()), Some(index), None, None, ).await?;
println!("Collection '{}' created or already exists", collection_name);
let documents = vec![
Document::new()
.with_id("0001")
.with_vector(vec![0.2123, 0.21, 0.213])
.with_field("bookName", "西游记")
.with_field("author", "吴承恩")
.with_field("page", 21)
.with_field("segment", "富贵功名,前缘分定,为人切莫欺心。"),
Document::new()
.with_id("0002")
.with_vector(vec![0.2123, 0.22, 0.213])
.with_field("bookName", "西游记")
.with_field("author", "吴承恩")
.with_field("page", 22)
.with_field("segment", "正大光明,忠良善果弥深。些些狂妄天加谴,眼前不遇待时临。"),
Document::new()
.with_id("0003")
.with_vector(vec![0.2123, 0.23, 0.213])
.with_field("bookName", "三国演义")
.with_field("author", "罗贯中")
.with_field("page", 23)
.with_field("segment", "细作探知这个消息,飞报吕布。"),
Document::new()
.with_id("0004")
.with_vector(vec![0.2123, 0.24, 0.213])
.with_field("bookName", "三国演义")
.with_field("author", "罗贯中")
.with_field("page", 24)
.with_field("segment", "布大惊,与陈宫商议。"),
Document::new()
.with_id("0005")
.with_vector(vec![0.2123, 0.25, 0.213])
.with_field("bookName", "三国演义")
.with_field("author", "罗贯中")
.with_field("page", 25)
.with_field("segment", "玄德曰:布乃当今英勇之士,可出迎之。"),
];
let upsert_result = collection.upsert(documents, None, true).await?;
println!("Documents upserted: {:?}", upsert_result);
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
let document_ids = vec!["0001".to_string(), "0002".to_string(), "0003".to_string()];
let filter = Filter::new("bookName=\"三国演义\"");
let output_fields = vec!["id".to_string(), "bookName".to_string()];
let query_results = collection.query(
Some(document_ids),
true, Some(2), Some(1), Some(filter), Some(output_fields), None, ).await?;
println!("Query results:");
for doc in &query_results {
println!(" ID: {:?}, BookName: {:?}", doc.get_id(), doc.get("bookName"));
}
let search_vectors = vec![vec![0.3123, 0.43, 0.213], vec![0.233, 0.12, 0.97]];
let search_params = tcvectordb::document::SearchParams::new().with_ef(200);
let search_results = collection.search(
search_vectors,
None, Some(search_params), false, 10, None, None, None, ).await?;
println!("Search results:");
for (i, batch) in search_results.iter().enumerate() {
println!(" Vector {} results:", i + 1);
for doc in batch {
println!(" ID: {:?}, BookName: {:?}, Score: {:?}",
doc.get_id(), doc.get("bookName"), doc.get_score());
}
}
let search_by_id_results = collection.search_by_id(
vec!["0003".to_string()],
Some(Filter::new("bookName=\"三国演义\"")),
Some(tcvectordb::document::SearchParams::new().with_ef(100)),
false,
2,
None,
None,
None,
).await?;
println!("Search by ID results:");
for batch in &search_by_id_results {
for doc in batch {
println!(" ID: {:?}, BookName: {:?}", doc.get_id(), doc.get("bookName"));
}
}
let update_doc = Document::new().with_field("page", 24);
let update_result = collection.update(
update_doc,
Some(vec!["0001".to_string(), "0003".to_string()]),
Some(Filter::new("bookName=\"三国演义\"")),
).await?;
println!("Update result: {:?}", update_result);
let delete_result = collection.delete(
Some(vec!["0001".to_string()]),
Some(Filter::new("bookName=\"西游记\"")),
None,
).await?;
println!("Delete result: {:?}", delete_result);
let count = collection.count(None).await?;
println!("Total documents: {}", count);
let rebuild_result = collection.rebuild_index().await?;
println!("Rebuild index result: {:?}", rebuild_result);
let drop_collection_result = db.drop_collection(collection_name).await?;
println!("Collection dropped: {:?}", drop_collection_result);
let drop_database_result = client.drop_database(database_name).await?;
println!("Database dropped: {:?}", drop_database_result);
Ok(())
}