pub struct VectorSearch { /* private fields */ }Expand description
Vector search builder
Provides k-NN search with optional filtering and custom distance metrics.
§Example
use azoth::prelude::*;
use azoth_vector::{VectorSearch, Vector, DistanceMetric};
let db = AzothDb::open("./data")?;
let query = Vector::new(vec![0.1, 0.2, 0.3]);
let search = VectorSearch::new(db.projection().clone(), "embeddings", "vector")
.distance_metric(DistanceMetric::Cosine);
let results = search.knn(&query, 10).await?;Implementations§
Source§impl VectorSearch
impl VectorSearch
Sourcepub fn new(
projection: Arc<SqliteProjectionStore>,
table: impl Into<String>,
column: impl Into<String>,
) -> Self
pub fn new( projection: Arc<SqliteProjectionStore>, table: impl Into<String>, column: impl Into<String>, ) -> Self
Create a new vector search builder
§Arguments
projection- The SQLite projection storetable- Table name containing the vector columncolumn- Vector column name (must be initialized with vector_init)
Sourcepub fn distance_metric(self, metric: DistanceMetric) -> Self
pub fn distance_metric(self, metric: DistanceMetric) -> Self
Set the distance metric
Default is Cosine similarity.
Sourcepub async fn knn(&self, query: &Vector, k: usize) -> Result<Vec<SearchResult>>
pub async fn knn(&self, query: &Vector, k: usize) -> Result<Vec<SearchResult>>
Perform k-nearest neighbors search
Returns up to k results ordered by similarity (closest first).
§Example
let query_vector = Vector::new(vec![0.1, 0.2, 0.3]);
let results = search.knn(&query_vector, 10).await?;
for result in results {
println!("Row {}: distance = {}", result.rowid, result.distance);
}Sourcepub async fn threshold(
&self,
query: &Vector,
max_distance: f32,
k: usize,
) -> Result<Vec<SearchResult>>
pub async fn threshold( &self, query: &Vector, max_distance: f32, k: usize, ) -> Result<Vec<SearchResult>>
Search with distance threshold
Returns all results within the given distance threshold, up to k results.
§Example
let query = Vector::new(vec![0.1, 0.2, 0.3]);
// Only return results with cosine distance < 0.3 (similarity > 0.7)
let results = search.threshold(&query, 0.3, 100).await?;Sourcepub async fn knn_filtered(
&self,
query: &Vector,
k: usize,
filter: &str,
filter_params: Vec<String>,
) -> Result<Vec<SearchResult>>
pub async fn knn_filtered( &self, query: &Vector, k: usize, filter: &str, filter_params: Vec<String>, ) -> Result<Vec<SearchResult>>
Search with custom SQL filter
Allows filtering results by additional columns in the table.
§Example
let query = Vector::new(vec![0.1, 0.2, 0.3]);
// Only search within a specific category
let results = search
.knn_filtered(&query, 10, "category = ?", vec!["tech".to_string()])
.await?;Sourcepub fn projection(&self) -> &Arc<SqliteProjectionStore>
pub fn projection(&self) -> &Arc<SqliteProjectionStore>
Get multiple results by rowids and include their distances from query
Useful for retrieving full records after search.
§Example
let query = Vector::new(vec![0.1, 0.2, 0.3]);
let results = search.knn(&query, 10).await?;
// Get full records
for result in results {
let record: String = search.projection()
.query(|conn: &rusqlite::Connection| {
conn.query_row(
"SELECT content FROM embeddings WHERE rowid = ?",
[result.rowid],
|row: &rusqlite::Row| row.get(0),
).map_err(|e| azoth_core::error::AzothError::Projection(e.to_string()))
})?;
println!("Distance: {}, Content: {}", result.distance, record);
}Sourcepub fn distance_metric_value(&self) -> DistanceMetric
pub fn distance_metric_value(&self) -> DistanceMetric
Get the distance metric
Auto Trait Implementations§
impl Freeze for VectorSearch
impl RefUnwindSafe for VectorSearch
impl Send for VectorSearch
impl Sync for VectorSearch
impl Unpin for VectorSearch
impl UnwindSafe for VectorSearch
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more