pub struct HnswConfig {
pub m: usize,
pub m_max0: usize,
pub ef_construction: usize,
pub ef_search: usize,
pub ml: f64,
pub pq_segments: usize,
pub pq_centroids: usize,
pub pq_training_samples: usize,
}Expand description
Configuration parameters for an HNSW index.
§Parameters
-
m- Maximum number of connections per node in each layer. Typical values: 16-64. Higher values give better recall but use more memory. -
m_max0- Maximum number of connections in layer 0 (the densest layer). Typically set to2 * m. -
ef_construction- Beam width during index construction. Higher values give better index quality but slower construction. Typical values: 100-500. -
ef_search- Default beam width during search. Higher values give better recall but slower search. Can be overridden per-query. Typical values: 10-500. -
ml- Level multiplier for determining max level. Typically1 / ln(m). Affects the distribution of nodes across layers.
§Product Quantization (PQ) for Compression
When pq_segments is set (non-zero), vectors are compressed using Product Quantization:
-
pq_segments- Number of subspaces to divide vectors into. Must divide the vector dimension evenly. Typical values: 8, 16, 32. Higher values = better accuracy but larger codes. -
pq_centroids- Number of centroids per subspace. Default: 256 (8-bit codes).
PQ reduces memory usage by 4-8x with minimal recall loss. During search:
- Full precision vectors are used for the first candidate selection
- Compressed vectors are stored in memory for efficient distance computation
- Original vectors can be retrieved from storage for final reranking
Fields§
§m: usizeMaximum number of connections per node (M parameter).
m_max0: usizeMaximum connections in layer 0 (typically 2 * M).
ef_construction: usizeBeam width for construction.
ef_search: usizeDefault beam width for search.
ml: f64Level multiplier (1 / ln(M)).
pq_segments: usizeNumber of PQ segments (0 = disabled). Must divide vector dimension evenly.
pq_centroids: usizeNumber of centroids per PQ segment. Default: 256.
pq_training_samples: usizeMinimum vectors required before PQ training. Default: 1000.
Implementations§
Source§impl HnswConfig
impl HnswConfig
Sourcepub fn new(m: usize) -> Self
pub fn new(m: usize) -> Self
Create a new HNSW configuration with the specified M parameter.
Other parameters are set to sensible defaults:
m_max0= 2 * mef_construction= 200ef_search= 50ml= 1 / ln(m)pq_segments= 0 (disabled)
Sourcepub const fn with_ef_construction(self, ef: usize) -> Self
pub const fn with_ef_construction(self, ef: usize) -> Self
Set the beam width for construction.
Sourcepub const fn with_ef_search(self, ef: usize) -> Self
pub const fn with_ef_search(self, ef: usize) -> Self
Set the default beam width for search.
Sourcepub const fn with_m_max0(self, m_max0: usize) -> Self
pub const fn with_m_max0(self, m_max0: usize) -> Self
Set the maximum connections in layer 0.
Sourcepub const fn with_pq(self, segments: usize) -> Self
pub const fn with_pq(self, segments: usize) -> Self
Enable Product Quantization with the specified number of segments.
The vector dimension must be divisible by segments.
This reduces memory usage by approximately dimension * 4 / segments bytes per vector.
§Arguments
segments: Number of subspaces. Common values: 8, 16, 32.
§Example
// For 128-dim vectors with 8 segments = 16x compression
let config = HnswConfig::new(16).with_pq(8);Sourcepub const fn with_pq_centroids(self, centroids: usize) -> Self
pub const fn with_pq_centroids(self, centroids: usize) -> Self
Set the number of centroids per PQ segment.
Default is 256 (8-bit codes). Higher values give better accuracy but require more memory for codebooks.
Sourcepub const fn with_pq_training_samples(self, samples: usize) -> Self
pub const fn with_pq_training_samples(self, samples: usize) -> Self
Set the minimum number of vectors required before training PQ.
PQ training requires enough samples for k-means clustering. Default is 1000.
Sourcepub const fn pq_enabled(&self) -> bool
pub const fn pq_enabled(&self) -> bool
Check if Product Quantization is enabled.
Trait Implementations§
Source§impl Clone for HnswConfig
impl Clone for HnswConfig
Source§fn clone(&self) -> HnswConfig
fn clone(&self) -> HnswConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more