Skip to main content

VectorSearch

Struct VectorSearch 

Source
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

Source

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 store
  • table - Table name containing the vector column
  • column - Vector column name (must be initialized with vector_init)
Source

pub fn distance_metric(self, metric: DistanceMetric) -> Self

Set the distance metric

Default is Cosine similarity.

Source

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);
}
Source

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?;
Source

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?;
Source

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);
}
Source

pub fn table(&self) -> &str

Get the table name

Source

pub fn column(&self) -> &str

Get the column name

Source

pub fn distance_metric_value(&self) -> DistanceMetric

Get the distance metric

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more