mod bm25;
mod flat;
mod hnsw;
mod ivf;
pub use bm25::{BM25Index, BM25SearchResult, BM25Stats};
pub use flat::FlatIndex;
pub use hnsw::HNSWIndex;
pub use ivf::IVFIndex;
use serde::{Deserialize, Serialize};
use crate::distance::Distance;
use crate::error::Result;
use crate::storage::Storage;
use crate::types::SearchResult;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub enum IndexType {
#[default]
Flat,
HNSW {
m: usize,
ef_construction: usize,
},
IVF {
num_clusters: usize,
num_probes: usize,
},
}
impl IndexType {
pub fn hnsw() -> Self {
IndexType::HNSW {
m: 16,
ef_construction: 200,
}
}
pub fn hnsw_with_params(m: usize, ef_construction: usize) -> Self {
IndexType::HNSW { m, ef_construction }
}
pub fn ivf() -> Self {
IndexType::IVF {
num_clusters: 100,
num_probes: 10,
}
}
pub fn ivf_with_params(num_clusters: usize, num_probes: usize) -> Self {
IndexType::IVF {
num_clusters,
num_probes,
}
}
}
pub trait Index: Send + Sync {
fn add(
&self,
id: &str,
vector: &[f32],
storage: &dyn Storage,
distance: Distance,
) -> Result<()>;
fn remove(&self, id: &str) -> Result<bool>;
fn search(
&self,
query: &[f32],
k: usize,
storage: &dyn Storage,
distance: Distance,
) -> Result<Vec<SearchResult>>;
fn rebuild(&self, storage: &dyn Storage) -> Result<()>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn clear(&self);
fn serialize_index(&self) -> Result<Option<Vec<u8>>> {
Ok(None)
}
fn load_index(&self, _data: &[u8]) -> Result<()> {
Ok(())
}
}