pub struct Config {
pub dim: u16,
pub m: u8,
pub ef_construction: u16,
pub ef_search: u16,
pub max_level: u8,
pub level_mult: f64,
pub rng_seed: Option<u64>,
pub check_vectors: bool,
}Expand description
Construction and search parameters for an HNSW index.
Fields§
§dim: u16Vector dimensionality.
m: u8Maximum neighbors selected for a newly inserted node in layer zero.
Must be greater than zero. Upper layers select at most
Config::new_node_neighbors neighbors (max(m / 2, 1)). New-node
links and reverse-link pruning both use the paper / hnswlib diversity
heuristic, then cap outgoing degree at Config::max_degree: 2 * m
at layer 0 (Mmax0) and m at upper layers (Mmax). Pruning is
one-sided: a dropped outgoing edge is not removed from the peer, so
adjacency may become directed.
ef_construction: u16Candidate search width during insertion.
Must be greater than zero. A stored value of 0 is rejected rather
than silently clamped during construction search.
ef_search: u16Candidate search width during queries.
Must be greater than zero. search uses max(ef_search, k) so a
request larger than this value still returns up to k hits.
max_level: u8Highest level that may be assigned to a node.
level_mult: f64Probability of stopping at the current randomly selected level.
Insertion stops at the current level with this probability, so
P(level >= L) = (1 - level_mult)^L. Config::default uses
Config::level_mult_for_m (1.0 - 1.0 / m) so
P(level >= L) = m^{-L}, matching the HNSW paper and hnswlib.
1.0 always assigns level zero; 0.0 always assigns
Config::max_level. Stored snapshots may keep any finite value in
[0, 1], including the former 0.5 default.
rng_seed: Option<u64>Optional seed for repeatable construction with the pinned rand version.
Operating-system entropy is used when absent.
check_vectors: boolWhen true, insert and search return crate::Error::InvalidVector
for non-finite coordinates or an L2 norm more than
crate::vector::UNIT_NORM_TOLERANCE from 1.
Default is false: release builds keep the previous unchecked
contract. Debug builds always debug_assert the same conditions so
NaN/Inf and clearly unnormalized inputs fail in development. The flag
is not stored in .hnsw snapshots; call
crate::LoadedHnsw::set_check_vectors after mapping.
Implementations§
Source§impl Config
impl Config
Sourcepub fn level_mult_for_m(m: u8) -> f64
pub fn level_mult_for_m(m: u8) -> f64
Stop probability that yields P(level >= L) = m^{-L}.
This is the HNSW paper / hnswlib formula: 1.0 - 1.0 / m. m must
be greater than zero. level_mult is stored independently of m, so
a struct update that changes only m should call this if the paper
distribution is wanted.
Sourcepub fn new_node_neighbors(&self, level: u8) -> u8
pub fn new_node_neighbors(&self, level: u8) -> u8
Neighbors selected for a newly inserted node at level.
Layer 0 uses Config::m; upper layers use max(m / 2, 1).
Sourcepub fn max_degree(&self, level: u8) -> usize
pub fn max_degree(&self, level: u8) -> usize
Maximum outgoing degree after reverse-link pruning (HNSW Mmax / Mmax0).
Layer 0 allows 2 * m neighbors; upper layers allow m.