pub struct HnswIndex {
pub m: usize,
pub m_max0: usize,
pub ef_construction: usize,
pub entry_point: u32,
pub max_level: u32,
pub ef_search: usize,
/* private fields */
}Expand description
A built HNSW index. Reads borrow from the on-disk payload at open time; the graph is owned (small relative to embeddings).
Fields§
§m: usize§m_max0: usize§ef_construction: usize§entry_point: u32§max_level: u32§ef_search: usizethe search beam floor, ef_construction at decode time: a caller’s
ef widens the beam above it (max(ef, ef_search)) and never
narrows it, so the recall measured at build stays the floor too.
Implementations§
Source§impl HnswIndex
impl HnswIndex
Sourcepub fn build(
vectors: Vec<f32>,
n: usize,
dim: usize,
m: usize,
ef_construction: usize,
seed: u64,
) -> Self
pub fn build( vectors: Vec<f32>, n: usize, dim: usize, m: usize, ef_construction: usize, seed: u64, ) -> Self
Build an HNSW index from f32 vectors. Deterministic given the
same seed. vectors is row-major n*dim.
Sourcepub fn build_from_int8(
view: &Int8EmbeddingsView<'_>,
m: usize,
ef: usize,
seed: u64,
) -> Self
pub fn build_from_int8( view: &Int8EmbeddingsView<'_>, m: usize, ef: usize, seed: u64, ) -> Self
Build from int8 quantized embeddings. Dequantizes once (lossy) and
hands off to build. Recall ends up bounded by quantization
noise; in practice still well above 0.95 @ k=10 for real corpora.
Source§impl HnswIndex
impl HnswIndex
Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
pub fn to_bytes(&self) -> Vec<u8> ⓘ
Encode the index to bytes for embedding in section 0x07 (v2).
Sourcepub fn from_bytes(
bytes: &[u8],
n: usize,
dim: usize,
) -> Result<Self, RuntimeError>
pub fn from_bytes( bytes: &[u8], n: usize, dim: usize, ) -> Result<Self, RuntimeError>
Parse an HNSW payload (v1 or v2). The vectors are reconstructed
from the embeddings section by the caller; this constructor is for
the on-disk graph only. Call attach_vectors before search.
Source§impl HnswIndex
impl HnswIndex
Sourcepub fn attach_vectors(&mut self, vectors: Vec<f32>)
pub fn attach_vectors(&mut self, vectors: Vec<f32>)
Attach f32 vectors as the search store. Kept for callers that
already hold an f32 buffer (build, tests); the runtime open path
uses attach_store to avoid an f32 expansion.
Sourcepub fn level0_neighbors(&self, i: usize) -> &[u32]
pub fn level0_neighbors(&self, i: usize) -> &[u32]
Level-0 (densest layer) out-neighbors of node i, or an empty slice
if i is out of range. Read-only accessor the graph build path uses to
derive top-m semantic edges from the already-built hnsw graph without
re-running an O(n^2) exact knn. The neighbor SET is the build-order
hnsw adjacency; the graph builder sorts canonically before writing.
Sourcepub fn search(&self, q: &[f32], ef: usize) -> Vec<usize>
pub fn search(&self, q: &[f32], ef: usize) -> Vec<usize>
Search for the ef closest candidates to q. Returns ids only -
the runtime reranks with the exact dot product to produce the
final cosine score. the beam is max(ef, self.ef_search): ef
widens the search above the file’s floor and never narrows it.