API Reference¶
This page provides a comprehensive reference for the HNSW API.
Core Types¶
Vector¶
A Vector
is a slice of float32 values representing a point in a high-dimensional space.
Node¶
A Node
represents a point in the vector space with a unique key.
Methods¶
Creates a new node with the specified key and vector.
Graph¶
type Graph[K cmp.Ordered] struct {
// Distance is the distance function used to compare embeddings.
Distance DistanceFunc
// M is the maximum number of neighbors to keep for each node.
// A good default for OpenAI embeddings is 16.
M int
// Ml is the level generation factor.
// E.g., for Ml = 0.25, each layer is 1/4 the size of the previous layer.
Ml float64
// EfSearch is the number of nodes to consider in the search phase.
// 20 is a reasonable default. Higher values improve search accuracy at
// the expense of memory.
EfSearch int
}
A Graph
is a Hierarchical Navigable Small World graph that stores vectors and allows for efficient similarity search.
Constructor Functions¶
NewGraph¶
Creates a new graph with default parameters:
- M: 16
- Ml: 0.25
- Distance: CosineDistance
- EfSearch: 20
NewGraphWithConfig¶
func NewGraphWithConfig[K cmp.Ordered](m int, ml float64, efSearch int, distance DistanceFunc) (*Graph[K], error)
Creates a new graph with the specified parameters and validates the configuration.
Graph Methods¶
Add¶
Adds one or more nodes to the graph. If a node with the same key already exists, it is replaced.
BatchAdd¶
Adds multiple nodes to the graph in a single operation. This is more efficient than calling Add
multiple times when adding many nodes.
Search¶
Searches for the k nearest neighbors to the query vector.
ParallelSearch¶
Performs a parallel search for the k nearest neighbors to the query vector using the specified number of worker goroutines.
SearchWithNegative¶
func (g *Graph[K]) SearchWithNegative(near Vector, negative Vector, k int, negWeight float32) ([]Node[K], error)
Searches for vectors that are similar to the positive example (near
) but dissimilar to the negative example (negative
).
SearchWithNegatives¶
func (g *Graph[K]) SearchWithNegatives(near Vector, negatives []Vector, k int, negWeight float32) ([]Node[K], error)
Searches for vectors that are similar to the positive example (near
) but dissimilar to multiple negative examples (negatives
).
Delete¶
Deletes a node from the graph. Returns true if the node was found and deleted.
BatchDelete¶
Deletes multiple nodes from the graph in a single operation. Returns a slice of booleans indicating which nodes were found and deleted.
Lookup¶
Looks up a node by key and returns its vector. Returns false if the node is not found.
Len¶
Returns the number of nodes in the graph.
Export¶
Exports the graph to the specified writer.
Import¶
Imports a graph from the specified reader.
Distance Functions¶
A DistanceFunc
calculates the distance between two vectors.
Built-in Distance Functions¶
CosineDistance¶
Calculates the cosine distance between two vectors: 1 - cosine similarity.
EuclideanDistance¶
Calculates the Euclidean (L2) distance between two vectors.
DotProductDistance¶
Calculates the negative dot product between two vectors.
Extensions¶
HNSW provides several extensions that enhance its functionality:
Metadata Extension¶
The Metadata Extension allows you to store and retrieve JSON metadata alongside vectors in the graph.
Key Types¶
type MetadataNode[K cmp.Ordered] struct {
Node hnsw.Node[K]
Metadata json.RawMessage
}
type MetadataGraph[K cmp.Ordered] struct {
Graph *hnsw.Graph[K]
Store MetadataStore[K]
}
Constructor¶
func NewMetadataGraph[K cmp.Ordered](graph *hnsw.Graph[K], store MetadataStore[K]) *MetadataGraph[K]
Creates a new metadata graph that wraps an HNSW graph and a metadata store.
Faceted Search Extension¶
The Faceted Search extension allows you to filter search results based on facets or attributes.
Key Types¶
type FacetedNode[K cmp.Ordered] struct {
Node hnsw.Node[K]
Facets map[string]interface{}
}
type FacetedGraph[K cmp.Ordered] struct {
Graph *hnsw.Graph[K]
Store FacetStore[K]
}
Constructor¶
Creates a new faceted graph that wraps an HNSW graph and a facet store.
Error Handling¶
Most methods in the HNSW API return errors that should be checked. Common errors include:
- Dimension mismatch errors when adding vectors with different dimensions
- Configuration validation errors when creating a graph with invalid parameters
- I/O errors when importing or exporting graphs
Example error handling: