HnswConfig

Struct HnswConfig 

Source
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 to 2 * 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. Typically 1 / 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: usize

Maximum number of connections per node (M parameter).

§m_max0: usize

Maximum connections in layer 0 (typically 2 * M).

§ef_construction: usize

Beam width for construction.

§ef_search: usize

Default beam width for search.

§ml: f64

Level multiplier (1 / ln(M)).

§pq_segments: usize

Number of PQ segments (0 = disabled). Must divide vector dimension evenly.

§pq_centroids: usize

Number of centroids per PQ segment. Default: 256.

§pq_training_samples: usize

Minimum vectors required before PQ training. Default: 1000.

Implementations§

Source§

impl HnswConfig

Source

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 * m
  • ef_construction = 200
  • ef_search = 50
  • ml = 1 / ln(m)
  • pq_segments = 0 (disabled)
Source

pub const fn with_ef_construction(self, ef: usize) -> Self

Set the beam width for construction.

Set the default beam width for search.

Source

pub const fn with_m_max0(self, m_max0: usize) -> Self

Set the maximum connections in layer 0.

Source

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

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.

Source

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.

Source

pub const fn pq_enabled(&self) -> bool

Check if Product Quantization is enabled.

Trait Implementations§

Source§

impl Clone for HnswConfig

Source§

fn clone(&self) -> HnswConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HnswConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HnswConfig

Source§

fn default() -> Self

Create a default HNSW configuration.

Uses M=16, which is a good balance between recall and speed.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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