hnswlib implements fast hierarchical navigable small world search in header-only C++ with Python bindings, supporting incremental inserts, deletions, filters, and optional no-exceptions APIs.
Overview
hnswlib provides approximate nearest neighbor search suitable for embedding indexes and similarity retrieval. The C++ core is header-only with C++11 dependencies, and Python bindings expose index creation, batch insertion, queries, and persistence.
Distances include squared L2, inner product, and cosine variants, with documentation on metric behavior. Indexes support incremental construction, resizing, marking deletions, replacing deleted slots, streaming save/load, and multi-threaded adds or queries with documented thread-safety limits.
Recent releases add filtering during search, StatusOr-style APIs for builds without exceptions, CMake fixes, and CI across Clang, GCC, and MSVC with sanitizers. Parameters such as M, ef_construction, and ef search are documented in ALGOPARAMS.md.
Python indexes are picklable, and custom distance functions are supported on the C++ side for specialized spaces.
Key features
- HNSW graph approximate search
- Header-only C++11 implementation
- Python bindings and pickling
- L2, inner product, and cosine metrics
- Incremental insert and delete support
- Filtered k-NN queries
- Multithreaded add and query paths
- Stream save and load of indexes
Use cases
- Vector similarity search in Python apps
- Embedding indexes with incremental updates
- Custom metric nearest neighbor retrieval
- High-performance ANN in C++ services