HNSW (Go)
A Go implementation of the HNSW approximate nearest neighbor search algorithm, enabling developers to embed efficient vector similarity search directly into Go services and custom vector database solutions.
About this tool
HNSW (Go)
Category: SDKs & Libraries
Technology: Go, Approximate Nearest Neighbor (ANN), Vector Search
Source: https://github.com/coder/hnsw
Overview
HNSW (Go) is a Go implementation of Hierarchical Navigable Small World (HNSW) graphs for fast approximate nearest neighbor (ANN) search over high-dimensional vectors. It can serve as an in-memory alternative to a vector database, suitable for embedding vector similarity search directly into Go applications and custom vector databases.
Features
Core Functionality
- In-memory HNSW graph implementation in pure Go.
- Approximate nearest neighbor search for high-dimensional vector data.
- Designed as an in-memory alternative to vector databases (e.g., Pinecone, Weaviate) with essential operations.
Data Model & Operations
- Insert
- Operation: Insert a vector into the graph.
- Complexity: ~O(log n).
- Delete
- Operation: Delete a vector from the graph.
- Complexity: ~O(M² · log n), where M is the maximum number of neighbors per node.
- Search
- Operation: Search for nearest neighbors of a query vector.
- Complexity: ~O(log n).
- Lookup
- Operation: Retrieve a vector by ID.
- Complexity: O(1).
- Configurable construction parameters (e.g., M, mL/
Graph.Ml) to trade off speed, recall, and memory usage, following the original HNSW paper.
Go API & Usage
- Simple Go module import:
go get github.com/coder/hnsw@main - Graph creation via
hnsw.NewGraph[IDType](), where the ID is generic (e.g.,int). - Node creation with
hnsw.MakeNode(id, []float32{...}). - Vector addition using
Graph.Add(...). - Nearest neighbor search using
Graph.Search(queryVector, k).
Persistence & Serialization
- In-memory graph operations with optional persistent storage support.
- Import/Export via streams:
Graph.Export(io.Writer)— write graph to anio.Writer.Graph.Import(io.Reader)— load graph from anio.Reader.
- File-backed SavedGraph abstraction:
LoadSavedGraph[T](path)to load or initialize a graph backed by a single file.SavedGraph.Save()to persist the current graph state to disk.
- Uses a fast binary encoding for the graph to achieve near disk-speed save/load performance (benchmarks provided for import/export on 100 vectors × 256 dimensions).
Performance & Tuning
- Designed for fast querying with approximate nearest neighbor results.
- Performance characteristics:
- At high dimensionality (e.g., 1536 dimensions), a large portion of query time is spent in the distance function.
- Suggested tuning guidelines:
- To reduce latency:
- Reduce vector dimensionality.
- Increase
M(maximum neighbors per node) for better search behavior at the cost of memory.
- To reduce memory usage:
- Decrease
Graph.M(M — maximum neighbors per node). - Decrease
Graph.Ml(mL — level generation parameter).
- Decrease
- To reduce latency:
Pricing
Not applicable. HNSW (Go) is an open-source Go library; no pricing information is provided in the source content.
Loading more......
Information
Categories
Tags
Similar Products
6 result(s)A Rust implementation of the HNSW (Hierarchical Navigable Small World) approximate nearest neighbor search algorithm, useful for building high-performance, memory-safe vector search components in Rust-based AI and retrieval systems.
jvector is a high-performance Java-based library and engine for vector search and approximate nearest neighbor indexing.
NearestNeighbors.jl is a Julia package implementing various nearest neighbor search algorithms and index structures for high-dimensional vector data.
Voyager is a Spotify open-source vector search library and service for efficient nearest neighbor search on large-scale vector datasets.
vsag is an Alibaba open-source library implementing efficient vector search algorithms, including approximate nearest neighbor search for high-dimensional vectors.
NVIDIA CAGRA is a GPU-accelerated graph-based library for approximate nearest neighbor searches, optimized for high-performance vector search leveraging modern GPU parallelism. It is suitable for scenarios requiring rapid, large-scale vector retrieval.