![]() |
VOOZH | about |
dotnet add package HNSW --version 26.6.1848
NuGet\Install-Package HNSW -Version 26.6.1848
<PackageReference Include="HNSW" Version="26.6.1848" />
<PackageVersion Include="HNSW" Version="26.6.1848" />Directory.Packages.props
<PackageReference Include="HNSW" />Project file
paket add HNSW --version 26.6.1848
#r "nuget: HNSW, 26.6.1848"
#:package HNSW@26.6.1848
#addin nuget:?package=HNSW&version=26.6.1848Install as a Cake Addin
#tool nuget:?package=HNSW&version=26.6.1848Install as a Cake Tool
<a href="https://curiosity.ai"><img src="https://curiosity.ai/media/cat.color.square.svg" width="100" height="100" align="right" /></a>
.Net library for fast approximate nearest neighbours search.
Exact k nearest neighbours search algorithms tend to perform poorly in high-dimensional spaces. To overcome curse of dimensionality the ANN algorithms come in place. This library implements one of such algorithms described in the "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs" article. It provides simple API for building nearest neighbours graphs, (de)serializing them and running k-NN search queries.
Check out the following code snippets once you've added the library reference to your project.
var parameters = new SmallWorld<float[], float>.Parameters()
{
M = 15,
LevelLambda = 1 / Math.Log(15),
EfSearch = 50,
};
float[] vectors = GetFloatVectors();
var graph = new SmallWorld<float[], float>(CosineDistance.NonOptimized);
graph.BuildGraph(vectors, new Random(42), parameters);
SmallWorld<float[], float> graph = GetGraph();
float[] query = Enumerable.Repeat(1f, 100).ToArray();
var best20 = graph.KNNSearch(query, 20);
var best1 = best20.OrderBy(r => r.Distance).First();
KNN search can stop traversing the graph once the result set has stopped improving, trading a little recall for fewer distance computations (see the Manticore write-up and "Patience in Proximity", Teofili & Lin, ECIR 2025). The savings grow with k and EfSearch. It is disabled by default.
var parameters = new SmallWorld<float[], float>.Parameters()
{
EfSearch = 100,
EnableEarlyTermination = true, // turn the optimization on
EarlyTerminationSaturationThreshold = 0.95, // optional: fraction of top-k left unchanged to count a hop as "non-improving"
EarlyTerminationPatience = 0, // optional: consecutive non-improving hops before stopping (0 = adaptive, scales with EfSearch)
};
SmallWorld<float[], float> graph = GetGraph();
byte[] buffer = graph.SerializeGraph(); // buffer stores information about parameters and graph edges
// distance function must be the same as the one which was used for building the original graph
var copy = new SmallWorld<float[], float>(CosineDistance.NonOptimized);
copy.DeserializeGraph(vectors, buffer); // the original vectors to attach to the "copy" vertices
The only one distance function supplied by the library is the cosine distance. But there are 4 versions to address universality/performance tradeoff.
CosineDistance.NonOptimized // most generic version works for all cases
CosineDistance.ForUnits // gives correct result only when arguments are "unit" vectors
CosineDistance.SIMD // uses SIMD instructions to optimize calculations
CosineDistance.SIMDForUnits // uses SIMD and requires arguments to be "units"
But the API allows to inject any custom distance function tailored specifically for your needs.
Your contributions and suggestions are very welcome! Please note that this project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact with any additional questions or comments.
The contributions to this project are released to the public under the . Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.
If you've found a bug or have a feature request then please open an issue with detailed description. We will be glad to see your pull requests as well.
git clone https://github.com/Microsoft/HNSW.Net.git
cd HNSW.Net
git checkout -b [username]/[feature]
cd Src
dotnet build
dotnet test
[username]/[feature] to master branch.When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA.
The library is distributed as a bundle of sources. We are working on enabling CI and creating Nuget package for the project.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 4 NuGet packages that depend on HNSW:
| Package | Downloads |
|---|---|
|
mostlylucid.botdetection
DEPRECATION NOTICE: This package will be renamed to 'stylobot' on June 1 2025. Version 7.0 will publish under the new name with a Stylobot.* namespace. See https://github.com/scottgal/stylobot/blob/main/docs/migration-v7.md Bot detection middleware for ASP.NET Core applications with behavioral analysis, header inspection, IP-based detection, and optional LLM-based classification. |
|
|
LLM.Guard
Easily implement guardrails around LLM prompts. |
|
|
IndxSearchLib
A new type of search system with pattern recognition. Enterprise-grade search in a class library. |
|
|
Iciclecreek.Lucene.Net.Vector
Adds support for vector embeddings to Lucene.Net 4.8, supporting similarity search and RAG patterns. |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 26.6.1848 | 260 | 6/13/2026 |
| 26.6.1827 | 154 | 6/12/2026 |
| 26.6.1714 | 198 | 6/9/2026 |
| 26.6.1655 | 210 | 6/8/2026 |
| 26.4.177 | 11,802 | 4/1/2026 |
| 26.3.129 | 301 | 3/30/2026 |
| 26.3.126 | 156 | 3/30/2026 |
| 26.1.63669 | 5,815 | 1/22/2026 |
| 26.1.63655 | 162 | 1/22/2026 |
| 25.12.63133 | 1,897 | 12/9/2025 |
| 25.12.63132 | 535 | 12/9/2025 |
| 25.7.60036 | 5,342 | 7/30/2025 |
| 25.7.59928 | 657 | 7/24/2025 |
| 25.3.56901 | 8,922 | 3/20/2025 |
| 24.9.52330 | 29,853 | 9/24/2024 |
| 1.0.52302 | 978 | 9/23/2024 |
| 1.0.52255 | 273 | 9/23/2024 |
| 1.0.52254 | 278 | 9/23/2024 |
| 1.0.51856 | 478 | 9/12/2024 |
| 1.0.51569 | 528 | 8/30/2024 |