HNSWIndex provides a high-performance, approximate nearest-neighbor search solution using the HNSW data structure. It supports custom distance metrics, efficient searching with concurrency, and configurable parameters to balance speed, recall, and memory usage. Perfect for .NET applications that require fast similarity lookups (e.g., image retrieval, recommendation systems, or vector-based search).
$ dotnet add package HNSWIndexPerform KNN Query for millions of data points fast and with great accuracy.
HNSWIndex is a .NET library for constructing approximate nearest-neighbor (ANN) indices based on the Hierarchical Navigable Small World (HNSW) graph. This data structure provides efficient similarity searches for large, high-dimensional datasets.
Func<TVector, TVector, TDistance> for custom distance calculation.Install via NuGet:
dotnet add package HNSWIndex
Or inside your .csproj:
<PackageReference Include="HNSWIndex" Version="x.x.x" />
var parameters = new HNSWParameters
{
RandomSeed = 123,
DistributionRate = 1.0,
MaxEdges = 16,
CollectionSize = 1024,
// ... other parameters
};
var index = new HNSWIndex<float[], float>(Metrics.SquaredEuclideanMetric.Compute, parameters);
var vectors = RandomVectors();
foreach (var vector in vectors)
{
index.Add(vector);
}
Or multi-threaded
var vectors = RandomVectors();
Parallel.For(0, vectors.Count, i => {
index.Add(vectors[i]);
});
var k = 5;
var results = index.KnnQuery(queryPoint, k);
index.Serialize(pathToFile);
var index = HNSWIndex<float[], float>.Deserialize(Metrics.SquaredEuclideanMetric.Compute, pathToFile);
Operations are thread-safe per type. You may run multiple operations of the same type in parallel on a single index instance. Mixing different operation types concurrently on the same index instance is not supported.
pip install hnswindex
import numpy as np
from hnswindex import Index
vectors = np.random.rand(2_000, 128)
# Create index (metric options: "sq_euclid", "cosine", "ucosine")
index = Index(dim=128, metric="sq_euclid")
index.set_collection_size(2_000)
# Batch add data
ids = index.add(vectors)
# Batch query data
# Note: distances are squared Euclidean when metric="sq_euclid".
ids, distances = index.knn_query(vectors, k=1)
This software is licensed under the MIT license