HNSWIndex<T>

Models & Types · AiDotNet.RetrievalAugmentedGeneration.VectorSearch.Indexes

Hierarchical Navigable Small World (HNSW) graph-based index for approximate nearest neighbor search.

Implements: IVectorIndex<T>

How It Works

HNSW builds a multi-layer graph structure where each layer is a proximity graph. Search starts at the top layer and progressively refines results by moving down layers. This provides excellent recall with logarithmic search complexity.

Search complexity: O(log n) on average where n is the number of vectors. Best for large datasets (100K+ vectors) requiring high recall and fast search.

Based on the paper: “Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs” by Malkov and Yashunin (2018).

Constructors

Constructor Summary
HNSWIndex(ISimilarityMetric<T> metric, int maxConnections, int efConstruction, int efSearch, int seed) Initializes a new instance of the HNSWIndex class.

Properties

Property Summary
int Count { get; }

Methods

Method Summary
List<ValueTuple<string, T>> ScoreCandidatesGpu(Vector<T> query, IReadOnlyList<string> candidateIds, int gpuThreshold) Scores an explicit set of candidate node ids against the query, using a single batched GPU matrix multiply when a device is available and the candidate set is large enough, and falling back to the exact CPU metric otherwise.
List<ValueTuple<string, T>> Search(Vector<T> query, int k)
bool Remove(string id)
void Add(string id, Vector<T> vector)
void AddBatch(Dictionary<string, Vector<T>> vectors)
void Clear()