Skip to main content

HNSWIndex.Net python module

Project description

HNSWIndex

Perform 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.

Key Features

  • High Performance: Implements the HNSW algorithm for fast approximate k-NN search.
  • Flexible Distance Metric: Pass any Func<TVector, TVector, TDistance> for custom distance calculation.
  • Flexible Heuristic: Pass heuristic function for nodes linking.
  • Concurrency Support: Thread safe graph building API
  • Configurable Parameters: Fine-tune the indexing performance and memory trade-offs with parameters
  • Save and Load: Save resulting structure on file system and restore later

Installation

Install via NuGet:

dotnet add package HNSWIndex

Or inside your .csproj:

<PackageReference Include="HNSWIndex" Version="x.x.x" />

Getting Started

1. Optionally configure parameters

var parameters = new HNSWParameters
{ 
    RandomSeed = 123,
    DistributionRate = 1.0,
    MaxEdges = 16,
    CollectionSize = 1024,
    // ... other parameters
};

2. Create empty graph structure ()

var index = new HNSWIndex<float[], float>(Metrics.SquaredEuclideanMetric.Compute, parameters);

3. Build the graph

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]);
});

4. Query the structure

var k = 5;
var results = index.KnnQuery(queryPoint, k);

5. Save and Load graph from file system

index.Serialize(pathToFile);
var index = HNSWIndex<float[], float>.Deserialize(Metrics.SquaredEuclideanMetric.Compute, pathToFile);

Concurrency notes

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.

Parameters

  • MaxEdges - Maximum number of outgoing edges per node. Sometimes labeld as M
  • MaxCandidates - Number of nodes resolved during insert operation. Sometimes labeled as efConstruction
  • CollectionSize - Expected number of elements that will be stored. Index is fully dynamic, however, often resizes might impact performance.
  • DistributionRate - Distribution rate used to promote nodes to higher levels of the graph.
  • MinNN - The minimal number of nodes obtained by knn search. If provided k exceeds this value, the search result will be trimmed to k. Sometimes labeled as efSearch.
  • RandomSeed - Seed for internal RNG.
  • AllowRemovals - Indicates if removals are allowed in the index.

Python bindings

Installation

pip install hnswindex

Example usage

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)

License

This software is licensed under the MIT license

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

hnswindex-1.6.0-cp313-cp313-win_amd64.whl (695.5 kB view details)

Uploaded CPython 3.13Windows x86-64

hnswindex-1.6.0-cp313-cp313-win32.whl (695.5 kB view details)

Uploaded CPython 3.13Windows x86

hnswindex-1.6.0-cp313-cp313-macosx_12_0_arm64.whl (719.2 kB view details)

Uploaded CPython 3.13macOS 12.0+ ARM64

hnswindex-1.6.0-cp312-cp312-win_amd64.whl (695.5 kB view details)

Uploaded CPython 3.12Windows x86-64

hnswindex-1.6.0-cp312-cp312-win32.whl (695.5 kB view details)

Uploaded CPython 3.12Windows x86

hnswindex-1.6.0-cp312-cp312-macosx_12_0_arm64.whl (719.2 kB view details)

Uploaded CPython 3.12macOS 12.0+ ARM64

hnswindex-1.6.0-cp311-cp311-win_amd64.whl (695.5 kB view details)

Uploaded CPython 3.11Windows x86-64

hnswindex-1.6.0-cp311-cp311-win32.whl (695.5 kB view details)

Uploaded CPython 3.11Windows x86

hnswindex-1.6.0-cp311-cp311-macosx_12_0_arm64.whl (719.2 kB view details)

Uploaded CPython 3.11macOS 12.0+ ARM64

hnswindex-1.6.0-cp310-cp310-win_amd64.whl (695.5 kB view details)

Uploaded CPython 3.10Windows x86-64

hnswindex-1.6.0-cp310-cp310-win32.whl (695.5 kB view details)

Uploaded CPython 3.10Windows x86

hnswindex-1.6.0-cp310-cp310-macosx_12_0_arm64.whl (719.2 kB view details)

Uploaded CPython 3.10macOS 12.0+ ARM64

hnswindex-1.6.0-cp39-cp39-win_amd64.whl (695.5 kB view details)

Uploaded CPython 3.9Windows x86-64

hnswindex-1.6.0-cp39-cp39-win32.whl (695.5 kB view details)

Uploaded CPython 3.9Windows x86

hnswindex-1.6.0-cp39-cp39-macosx_12_0_arm64.whl (719.2 kB view details)

Uploaded CPython 3.9macOS 12.0+ ARM64

File details

Details for the file hnswindex-1.6.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 09178c547e3a419f5a6bac10489c8f36e24815ca502bfd3f5e41d1380dc5949e
MD5 49f670792c60119c4f9b7025809cb8d3
BLAKE2b-256 650c424c6cfef24d6e1782f724da95cc525494f091460c52d37f6290804679d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp313-cp313-win_amd64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp313-cp313-win32.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp313-cp313-win32.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.13, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp313-cp313-win32.whl
Algorithm Hash digest
SHA256 a083702e1669f5580cf9a87959c2b3903f1dd8e1ea778021169b9bda0e022f4f
MD5 bc99e592bb1541448aa9e8e35a428b95
BLAKE2b-256 3a368e55eeb81f9e7375fe3dbf168d8036a1baf6e7d040288dcdef8f6c733f85

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp313-cp313-win32.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp313-cp313-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for hnswindex-1.6.0-cp313-cp313-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 e48d3d1b770b5c451ccafa329ad1ab09fd5a724181e3c3b09986d3ac01ab4215
MD5 4f52e5548c827549973437d03aadd732
BLAKE2b-256 32d959b09462956d4a55c8f11428eac1d10d6ff4cd44a90ed538b6c3b2d97e4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp313-cp313-macosx_12_0_arm64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 c95db23a0044e2fa1d603100cc38dd305e0aa1d2ded13ca6f2a970a9990504ed
MD5 fb4f293927656106abb739ea2ee8c3a2
BLAKE2b-256 d5e7f358d6d9a72e688c43f0daf20e161abd1f396a753f55e52c51b0d432fc12

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp312-cp312-win_amd64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp312-cp312-win32.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp312-cp312-win32.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.12, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp312-cp312-win32.whl
Algorithm Hash digest
SHA256 7c4c0f218e795178f7501d004bf85be219c4e37434b9b2faec61cc8fa0418ec5
MD5 0cc2affd919e87faa62c371efd42f6ab
BLAKE2b-256 5d0091c4121dc5f8487d658b5867b051ceb1cb6e6ba4650a48fcef3e2c56a689

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp312-cp312-win32.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp312-cp312-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for hnswindex-1.6.0-cp312-cp312-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 e65fe1a118e5b3cb545d393375db82980e5549260970513a70feacea61907f8c
MD5 735235073cb933efbaf4b9dab438d8f9
BLAKE2b-256 ca2adda12af5982c952eb87ada4be603d1cf5bc0a334bf498737a729bbed470f

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp312-cp312-macosx_12_0_arm64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 36990f63b590865c2f067643005fa37b5ca5c725a3cf3ce7b97f7cf37b4cec28
MD5 096fdf8bd0a7cc331e4f883c28ae5a00
BLAKE2b-256 f5835a83fe061d01c005eda5932b7b9e31a8fbb5b995fd76a157646d6fb37026

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp311-cp311-win_amd64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp311-cp311-win32.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp311-cp311-win32.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.11, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp311-cp311-win32.whl
Algorithm Hash digest
SHA256 5ff8408dbd4b47ed3047ae93f199d8cec4185de20652179df83ffb94ae8fbcbc
MD5 1ef4a899586eb57547e437566462d911
BLAKE2b-256 960a03b926eac48b51b92442fc7c3daf584ae25abf92f31d62a0614678e61fef

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp311-cp311-win32.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp311-cp311-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for hnswindex-1.6.0-cp311-cp311-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 5357656709e43b03118abf3a4435f1991dd7fbb19934dccd1ca65dd839636f80
MD5 5fd81334c64728bff8117288d7677d50
BLAKE2b-256 8aa9feab3254ae5e82f100afbd8e63871c65542ce35e84e9deec99335e0659b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp311-cp311-macosx_12_0_arm64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 b8df6d5c800174d3440d9f650c087b9f4e9db2e1b02e620d350214bf448fcfba
MD5 b484544927dacc84733afe882370569b
BLAKE2b-256 28cc3754a4586ff6dd593a9fb1103f30e2823129ef7629c71173296ff601b94a

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp310-cp310-win_amd64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp310-cp310-win32.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp310-cp310-win32.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.10, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp310-cp310-win32.whl
Algorithm Hash digest
SHA256 63d046df7af4ffbf275b20ed5ad3335d7e59e4289d02117459e414ff8d40a00b
MD5 a072d04eb3e20574525c373a046df6b9
BLAKE2b-256 4dcf43eb77c119d3d4249c8f255a3f2c09e1c8a3064b70e2930b9b17dee442e5

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp310-cp310-win32.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp310-cp310-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for hnswindex-1.6.0-cp310-cp310-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 b87f2f7d34369a282165398ee2fa6d321dfbcc53200fb2314c909502fb190b49
MD5 ee1eaf62fec2c5c6a9c98f1129f7f0b3
BLAKE2b-256 b94df05686bf2d0f8b40003b407fba4ee337a647b7ad9c52624dcfef0c25a631

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp310-cp310-macosx_12_0_arm64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ef2a9d4f617683ff01e65501222302b40729374fb2bc3001f0e1c47016876d45
MD5 d860ce2e44900425d42fa80ccbd370aa
BLAKE2b-256 22b0de997c2f960e1dafa8fdb4bb7f18d491e6a48104bdca021eb4928ea468b2

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp39-cp39-win_amd64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp39-cp39-win32.whl.

File metadata

  • Download URL: hnswindex-1.6.0-cp39-cp39-win32.whl
  • Upload date:
  • Size: 695.5 kB
  • Tags: CPython 3.9, Windows x86
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hnswindex-1.6.0-cp39-cp39-win32.whl
Algorithm Hash digest
SHA256 115b03aa6ef68337302bc5854edadded02a7ca31443b36174d460c31a89cd488
MD5 82a53ccd6b983d0d888267318f930cab
BLAKE2b-256 a25942d2560b6208610214cf8ec27ac4acb7ca297f4147c4dfd46314f5b53cd1

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp39-cp39-win32.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hnswindex-1.6.0-cp39-cp39-macosx_12_0_arm64.whl.

File metadata

File hashes

Hashes for hnswindex-1.6.0-cp39-cp39-macosx_12_0_arm64.whl
Algorithm Hash digest
SHA256 9c2d1c20a77318bb0ca89eeedbb3166e17796eba78010d2dc14aef5a1436528e
MD5 485fea76300e52ac68498e7d961a35e9
BLAKE2b-256 6b73aa471216f253f38866f376c4c8b7d58c02873a2ea8ef3173ff877f231a9f

See more details on using hashes here.

Provenance

The following attestation bundles were made for hnswindex-1.6.0-cp39-cp39-macosx_12_0_arm64.whl:

Publisher: publish_pypi.yml on Skaipi/HNSWIndex.Net

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page