vector

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: MIT Imports: 7 Imported by: 0 modules, 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 vector stores embeddings by ID and answers nearest-neighbour queries. Small indexes are searched exactly; past Params.Threshold vectors an HNSW graph answers approximately (see hnsw.go and index.go).

Index

Constants

This section is empty.

Variables

View Source
var DefaultParams = Params{M: 16, EfConstruction: 200, EfSearch: 64, Threshold: 1000}
View Source
var ErrEmpty = errors.New("ERR zero-length vector")

Functions

This section is empty.

Types

type Index

type Index struct {
	Params Params
	// contains filtered or unexported fields
}

func New

func New() *Index

func (*Index) Add

func (x *Index) Add(id string, v []float32, attr string) (bool, error)

Add inserts or replaces a vector. The first vector fixes the dimension. Returns true if id is new.

func (*Index) Dim

func (x *Index) Dim() int

func (*Index) Get

func (x *Index) Get(id string) ([]float32, string, bool)

Get returns the stored vector and attributes.

func (*Index) IDs

func (x *Index) IDs() []string

IDs returns all ids (order unspecified).

func (*Index) Len

func (x *Index) Len() int

func (*Index) MemSize

func (x *Index) MemSize() int64

func (*Index) Remove

func (x *Index) Remove(id string) bool

func (*Index) Search

func (x *Index) Search(q []float32, k int, filter func(id, attr string) bool) ([]Result, error)

Search returns the k most similar vectors to q, best first. filter, if non-nil, skips ids for which it returns false; it is called at most once per id.

Below Params.Threshold vectors (or when k covers the whole index) the scan is exact. Otherwise the HNSW graph, which Add keeps up to date, answers approximately; if it finds fewer than k matches passing the filter, Search falls back to the exact scan so a selective filter never loses results.

type Params

type Params struct {
	M              int // links per node above layer 0; layer 0 allows 2M
	EfConstruction int // candidate list size while inserting
	EfSearch       int // minimum candidate list size while querying (x2 with a filter)
	Threshold      int // below this many vectors Search is exact
}

Params tunes the HNSW graph. Changes apply the next time the graph is started.

type Result

type Result struct {
	ID    string
	Score float32 // cosine similarity, 1 = identical
	Attr  string
}

Jump to

Keyboard shortcuts

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