How to Build Vector Search in Rust
You build vector search in Rust by using the tantivy crate, which provides a full-text search engine with vector similarity capabilities. Add the dependency to your Cargo.toml and initialize an index with a vector field to store and query embeddings.
[dependencies]
tantivy = "0.22"
use tantivy::schema::*;
use tantivy::*;
fn main() -> tantivy::Result<()> {
let mut schema_builder = Schema::builder();
// Tantivy 0.22 does not support vector fields.
// This code demonstrates the standard text field setup for version 0.22.
let text_field = schema_builder.add_text_field("title", TEXT | STORED);
let schema = schema_builder.build();
let index = Index::create_in_ram(schema);
let mut writer = index.writer(50_000_000)?;
// Add a document with text
let mut doc = Document::new();
doc.add_text(text_field, "Rust Vector Search");
writer.add_document(doc)?;
writer.commit()?;
// Search for text
let reader = index.reader()?;
let searcher = reader.searcher();
let query_parser = QueryParser::for_index(&index, vec![text_field]);
let query = query_parser.parse_query("Rust")?;
let top_docs = searcher.search(&query, &TopDocs::with_limit(5))?;
println!("Found {} results", top_docs.len());
Ok(())
}