hnsw

package
v1.40.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hnsw implements an in-memory Hierarchical Navigable Small World graph (Malkov & Yashunin) for approximate nearest neighbor search over float32 vectors. Scores use the same arithmetic as vec.Score, so a document found approximately carries exactly the score exact search would give it.

Index

Constants

View Source
const (
	DefaultM              = 16
	DefaultEfConstruction = 200
	DefaultEfSearch       = 100
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph holds vectors and their navigable layers. Add is not safe for concurrent use; once building is done, Search is safe for concurrent use.

func New

func New(metric vec.Metric, params Params) (*Graph, error)

New creates an empty graph for metric.

func Read added in v1.33.0

func Read(r io.Reader) (*Graph, error)

Read restores a graph written by WriteTo, consuming exactly its bytes. Input is checksummed and structurally validated, so a corrupt or foreign stream returns an error rather than a graph that could panic in Search.

func ReadLinks(r io.Reader, ids []uint64, data []float32) (*Graph, error)

ReadLinks restores a graph written by WriteLinksTo, consuming exactly its bytes. ids and data, node-major with the graph's dimension, must be those of the written graph; a mismatch is an error. The graph keeps data.

func (*Graph) Add

func (g *Graph) Add(id uint64, vector []float32) error

Add inserts vector for document id. Every vector must share one dimension; the same id may be added more than once.

func (*Graph) Len

func (g *Graph) Len() int

Len returns the number of vectors.

func (*Graph) Metric added in v1.33.0

func (g *Graph) Metric() vec.Metric

Metric returns the graph's similarity metric.

func (*Graph) Params added in v1.33.0

func (g *Graph) Params() Params

Params returns the graph's parameters with defaults applied.

func (*Graph) Search

func (g *Graph) Search(query []float32, k, ef int, accept func(uint64) bool) ([]vec.Match, error)

Search returns up to k documents, best first with ties by ascending id; repeated ids keep their best score. ef bounds the candidate list (0 uses the graph's EfSearch; k raises it). accept optionally filters results with the traversal semantics documented by SearchContext.

func (*Graph) SearchContext

func (g *Graph) SearchContext(ctx context.Context, query []float32, k, ef int,
	accept func(uint64) bool) ([]vec.Match, error)

SearchContext is Search with cancellation during descent and layer expansion. Discovered rejected nodes are always expanded as bridges, even with a full accepted beam. Accepted nodes remain score-pruned, so search is approximate; selective filters may traverse an entire connected rejected region.

func (*Graph) WriteLinksTo added in v1.40.0

func (g *Graph) WriteLinksTo(w io.Writer) (int64, error)

WriteLinksTo serializes the graph without its ids and vectors; ReadLinks restores it given the same ids and vectors. It may run concurrently with Search but not with Add.

func (*Graph) WriteTo added in v1.33.0

func (g *Graph) WriteTo(w io.Writer) (int64, error)

WriteTo serializes the graph to w; Read restores it. Graphs built by the same Adds before and after a WriteTo/Read round trip are identical. WriteTo may run concurrently with Search but not with Add.

type Params

type Params struct {
	// M is the maximum number of neighbors per node above level 0; level 0
	// keeps 2*M. Larger values raise recall and memory. Minimum 2.
	M int
	// EfConstruction is the candidate list size while inserting.
	EfConstruction int
	// EfSearch is the candidate list size while searching; k raises it when larger.
	EfSearch int
}

Params tunes graph construction and search. Zero fields take the defaults.

func (Params) Validate

func (p Params) Validate() error

Validate rejects negative values, M below 2, and overflow of the 2*M level-0 limit.

func (Params) WithDefaults

func (p Params) WithDefaults() Params

WithDefaults fills zero fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL