Help us shape the future of Go! Take the 2026 Go Developer Survey

vectordata

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package vectordata defines provider-independent vector store contracts, record schemas, retrieval options, and vector and keyword-hybrid searches.

Store wraps a database provider and creates typed or dynamic collections. Providers must support concurrent calls, honor context cancellation, and treat caller-supplied schemas, filters, and options as read-only. Record ownership is provider-specific: providers document whether records are copied or retained, whether returned records share storage, and whether generated keys are written into inputs.

Collections use typed keys and records. Records may be structs, pointers to structs, or map[string]any for dynamic mapping. Providers combine vectordata struct tags with an explicit schema when constructing collection handles. Search values may be vectors, embeddings, or inputs for a provider's configured embedding generator. Experimental model-building utilities for providers live in the providerservices subpackage.

Iterators perform work when consumed and must stop on the first error or when the consumer stops. A provider which owns resources may implement io.Closer; cleanup and service lookup are not required by these interfaces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteBatch

func DeleteBatch[TKey, TRecord any](ctx context.Context, collection Collection[TKey, TRecord], keys []TKey) error

DeleteBatch deletes keys, ignoring missing records. If collection implements DeleteBatch(context.Context, []TKey) error, it is used; otherwise keys are deleted sequentially, stopping at the first error. A failed batch may have deleted some keys and can be retried. Nil and empty slices do not call the provider.

func GetBatch

func GetBatch[TKey, TRecord any](ctx context.Context, collection Collection[TKey, TRecord], keys []TKey, options *RecordRetrievalOptions) iter.Seq2[TRecord, error]

GetBatch retrieves the found records for keys, omitting missing records. If collection implements a GetBatch method with this signature, it is used. Otherwise records are retrieved sequentially in key order. Native batch providers may return records in a different order. Work begins on iteration. A nil or empty keys slice is an empty batch and does not call the provider.

Types

type Collection

type Collection[TKey, TRecord any] interface {
	Searcher[TRecord]

	// Name returns the collection's name.
	Name() string

	// Exists reports whether the collection exists in the store.
	Exists(ctx context.Context) (bool, error)

	// EnsureExists creates the collection if it does not already exist.
	EnsureExists(ctx context.Context) error

	// EnsureDeleted deletes the collection if it exists.
	EnsureDeleted(ctx context.Context) error

	// Get returns the record identified by key, or found=false if the key is
	// missing. It does not create the collection.
	// Use [GetBatch] for native or sequential batch retrieval.
	Get(ctx context.Context, key TKey, options *RecordRetrievalOptions) (TRecord, bool, error)

	// GetFiltered returns up to top records matching filter.
	GetFiltered(ctx context.Context, filter Filter, top int, options *FilteredRecordRetrievalOptions) iter.Seq2[TRecord, error]

	// Delete deletes the record identified by key if it exists. It does not
	// create the collection. Use [DeleteBatch] for native or sequential batch
	// deletion.
	Delete(ctx context.Context, key TKey) error

	// Upsert inserts or updates records as one batch. It accepts individual
	// records or an expanded slice. No records, a nil slice, and an empty slice
	// represent an empty batch. Implementations should generate embeddings once
	// per batch, not once per record. Providers document whether batches are atomic.
	Upsert(ctx context.Context, records ...TRecord) error
}

Collection manages a named collection and its records. Providers document which key, data, vector, filter, and index types they support.

func GetCollection

func GetCollection[TKey, TRecord any](s *Store, name string, definition *Definition) (Collection[TKey, TRecord], error)

GetCollection returns a typed handle without creating the collection. A nil definition uses the record's vectordata tags. Providers validate the requested types and schema. The returned collection preserves record identity, provider errors, and optional provider capabilities. It reports an error if a provider returns a record of an incompatible type.

This function supports Go 1.26 callers. On Go 1.27, s.Collection[TKey, TRecord] provides the same operation as a generic method.

type DataProperty

type DataProperty struct {
	Name                string
	Type                reflect.Type
	StorageName         string
	ProviderAnnotations map[string]any
	IsIndexed           bool
	IsFullTextIndexed   bool
}

DataProperty describes a stored data field. Indexing and full-text indexing are disabled by default. Type can be inferred from a struct field.

type Definition

type Definition struct {
	Properties         []Property
	EmbeddingGenerator *embedding.BoundGenerator
}

Definition configures the properties stored in a collection. Providers combine it with the record's vectordata tags when constructing a collection handle; they do not modify the caller's definition.

Tags use a role followed by comma-separated settings:

ID     string    `vectordata:"key,autogenerated=false,storage=id"`
Text   string    `vectordata:"data,indexed,fulltext"`
Vector []float32 `vectordata:"vector,dimensions=1536,index=hnsw,distance=cosine_similarity"`

Built-in index and distance names use lowercase snake case. Provider-specific values and storage names retain their casing. Explicit properties override tags, while empty storage, index, and distance names retain tag settings. Dynamic map[string]any records require explicit property types.

EmbeddingGenerator supplies the default generator for vector properties, overriding any store default. A property's generator takes precedence. Use embedding.Bind to configure typed generators; input and output types may differ between vector properties.

type DistanceFunction

type DistanceFunction string

DistanceFunction identifies a vector comparison function. Built-in values use lowercase snake case. Providers may accept additional, case-sensitive values. An empty value uses the provider's default.

const (
	// CosineSimilarity compares vector angles, from -1 (opposite) to 1 (identical).
	CosineSimilarity DistanceFunction = "cosine_similarity"
	// CosineDistance is 1 minus cosine similarity; smaller values are nearer.
	CosineDistance DistanceFunction = "cosine_distance"
	// DotProductSimilarity compares vector lengths and angles; larger is nearer.
	DotProductSimilarity DistanceFunction = "dot_product_similarity"
	// NegativeDotProductSimilarity is negative dot product; smaller is nearer.
	NegativeDotProductSimilarity DistanceFunction = "negative_dot_product_similarity"
	// EuclideanDistance is the L2 distance.
	EuclideanDistance DistanceFunction = "euclidean_distance"
	// EuclideanSquaredDistance is the squared L2 distance.
	EuclideanSquaredDistance DistanceFunction = "euclidean_squared_distance"
	// HammingDistance counts differing coordinates.
	HammingDistance DistanceFunction = "hamming_distance"
	// ManhattanDistance is the sum of absolute coordinate differences.
	ManhattanDistance DistanceFunction = "manhattan_distance"
)

type Error

type Error struct {
	SystemName     string
	StoreName      string
	CollectionName string
	OperationName  string
	Message        string
	Err            error
}

Error describes a vector store failure while preserving the original cause. Provider errors may use it to attach database, collection, and operation names.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying provider error.

type Filter

type Filter struct {
	Operator FilterOperator
	Property string
	Value    any
	Operands []Filter
}

Filter is a provider-translatable predicate over record property names. Comparisons use Property and Value. Equal and NotEqual support nil values. Contains tests whether a collection-valued property contains Value. In tests whether the property's value occurs in a slice or array supplied as Value.

And and Or combine two or more Operands; Not negates a single operand. Logical nodes do not use Property or Value. Providers must return errors for unsupported operations instead of dropping predicates or filtering only after retrieving a limited result set. Filters and values are read-only.

func (*Filter) Validate

func (f *Filter) Validate() error

Validate checks the structure of a filter. A nil receiver means no filter. Property existence, data-type compatibility, and operator support are validated by the provider.

type FilterOperator

type FilterOperator string

FilterOperator identifies a portable filter operation. Provider support for individual operations and property types varies.

const (
	Equal              FilterOperator = "eq"
	NotEqual           FilterOperator = "ne"
	GreaterThan        FilterOperator = "gt"
	GreaterThanOrEqual FilterOperator = "ge"
	LessThan           FilterOperator = "lt"
	LessThanOrEqual    FilterOperator = "le"
	Contains           FilterOperator = "contains"
	In                 FilterOperator = "in"
	And                FilterOperator = "and"
	Or                 FilterOperator = "or"
	Not                FilterOperator = "not"
)

type FilteredRecordRetrievalOptions

type FilteredRecordRetrievalOptions struct {
	Skip           int
	OrderBy        []Sort
	IncludeVectors bool
}

FilteredRecordRetrievalOptions controls retrieval by a filter without vector ranking. With no OrderBy, result order is provider-dependent. IncludeVectors defaults to false; provider-specific exceptions may apply.

func (*FilteredRecordRetrievalOptions) Validate

func (o *FilteredRecordRetrievalOptions) Validate() error

Validate checks pagination and ordering. A nil receiver uses defaults.

type HybridSearchOptions

type HybridSearchOptions struct {
	Filter             *Filter
	VectorProperty     string
	AdditionalProperty string
	Skip               int
	IncludeVectors     bool
	ScoreThreshold     *float64
}

HybridSearchOptions controls combined vector and keyword search. AdditionalProperty selects a full-text-indexed data property by record name. If empty, the collection must have exactly one such property. ScoreThreshold applies to the provider's combined relevance score.

func (*HybridSearchOptions) Validate

func (o *HybridSearchOptions) Validate() error

Validate checks provider-independent option constraints. A nil receiver uses defaults. Providers validate property and hybrid-search support.

type IndexKind

type IndexKind string

IndexKind identifies a vector index. Built-in values use lowercase snake case. Providers may accept additional, case-sensitive values. An empty value uses the provider's default.

const (
	HNSW          IndexKind = "hnsw"
	Flat          IndexKind = "flat"
	IVFFlat       IndexKind = "ivf_flat"
	DiskANN       IndexKind = "disk_ann"
	QuantizedFlat IndexKind = "quantized_flat"
	Dynamic       IndexKind = "dynamic"
)

type KeyProperty

type KeyProperty struct {
	Name                string
	Type                reflect.Type
	StorageName         string
	ProviderAnnotations map[string]any
	IsAutoGenerated     *bool
}

KeyProperty describes the unique record key. Type can be inferred from a struct field; dynamic records require it explicitly. A nil IsAutoGenerated leaves key generation to the provider's default, distinct from explicit false. StorageName defaults to Name unless the provider reserves a key storage name. ProviderAnnotations are provider-specific.

type KeywordHybridSearcher

type KeywordHybridSearcher[TRecord any] interface {
	// HybridSearch combines vector similarity for searchValue with full-text
	// search for keywords using a full-text-indexed data property. Keywords,
	// search values, and options are read-only. Providers must reject an
	// ambiguous vector or full-text property when no property is selected explicitly.
	HybridSearch(ctx context.Context, searchValue any, keywords []string, top int, options *HybridSearchOptions) iter.Seq2[SearchResult[TRecord], error]
}

KeywordHybridSearcher is an optional capability combining vector similarity with full-text keyword search.

type Property

type Property interface {
	// contains filtered or unexported methods
}

Property describes a key, data, or vector field. Its implementations are *KeyProperty, *DataProperty, and *VectorProperty.

type RecordRetrievalOptions

type RecordRetrievalOptions struct {
	IncludeVectors bool
}

RecordRetrievalOptions controls retrieval of one or more records. IncludeVectors defaults to false, requesting vector exclusion. Providers may document exceptions, such as in-memory storage returning native vectors directly.

type SearchOptions

type SearchOptions struct {
	Filter         *Filter
	VectorProperty string
	Skip           int
	IncludeVectors bool
	ScoreThreshold *float64
}

SearchOptions controls vector search. Property names refer to record fields, not storage names. An empty VectorProperty selects the sole vector property; no vectors or multiple vectors require an explicit selection or an error. A nil Filter does not filter records. Skip defaults to zero.

ScoreThreshold includes scores equal to the threshold. Similarity functions discard lower scores; distance functions discard higher scores. The score range and supported threshold behavior depend on the provider. IncludeVectors defaults to false; provider-specific exceptions may apply.

func (*SearchOptions) Validate

func (o *SearchOptions) Validate() error

Validate checks provider-independent option constraints. A nil receiver uses defaults. Providers are responsible for validating property selections and support for filters and score thresholds.

type SearchResult

type SearchResult[TRecord any] struct {
	Record TRecord
	Score  *float64
}

SearchResult contains a matching record and its provider-specific score. A nil Score means the provider did not report a score; zero is a valid score. Similarity scores rank higher values first; distance scores rank lower values first. Scores are not normalized across providers.

type Searcher

type Searcher[TRecord any] interface {
	// Search returns up to top records closest to searchValue, which may be a
	// vector, an embedding, or an input supported by a configured embedding
	// generator. Providers must reject nil search values, nonpositive top,
	// invalid options, and ambiguous vector-property selection rather than
	// selecting a property silently. Filtering happens before ranking, and
	// Skip is applied before taking top.
	Search(ctx context.Context, searchValue any, top int, options *SearchOptions) iter.Seq2[SearchResult[TRecord], error]
}

Searcher performs vector similarity searches. Supported representations and distance functions depend on the provider.

type Sort

type Sort struct {
	Property   string
	Descending bool
}

Sort orders filtered records by a record property. The zero value of Descending requests ascending order. Multiple sorts are applied in slice order.

type Store

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

Store manages collections that may have different key and record types. Create one with NewStore or a database provider's constructor. Use Store.DynamicCollection for map records or GetCollection for typed records. Go 1.27 also supports the generic Collection method.

func NewStore

func NewStore(provider StoreProvider) *Store

NewStore wraps a database provider. It panics if provider is nil.

func (*Store) Collection

func (s *Store) Collection[TKey, TRecord any](name string, definition *Definition) (Collection[TKey, TRecord], error)

Collection returns a typed handle without creating the collection. It is equivalent to GetCollection and requires Go 1.27 or later.

func (*Store) CollectionExists

func (s *Store) CollectionExists(ctx context.Context, name string) (bool, error)

CollectionExists reports whether the named collection exists.

func (*Store) DynamicCollection

func (s *Store) DynamicCollection(name string, definition *Definition) (Collection[any, map[string]any], error)

DynamicCollection returns a handle for map records without creating the collection. The definition must be non-nil and specify explicit property types. Keys use the schema's key type. This method is available on all supported Go versions. Call Collection.EnsureExists to create the collection.

func (*Store) EnsureCollectionDeleted

func (s *Store) EnsureCollectionDeleted(ctx context.Context, name string) error

EnsureCollectionDeleted deletes the named collection if it exists.

func (*Store) ListCollectionNames

func (s *Store) ListCollectionNames(ctx context.Context) iter.Seq2[string, error]

ListCollectionNames returns the names of the collections in the store.

type StoreProvider

type StoreProvider interface {
	// OpenCollection returns a handle without creating the collection.
	// keyType and recordType describe the caller's Go types. The provider must
	// validate their compatibility with the schema and retain the actual record
	// types across operations; the any parameters are only a dispatch boundary.
	// Optional batch, hybrid-search, and io.Closer capabilities are preserved
	// by the store's typed collection adapters.
	OpenCollection(name string, keyType, recordType reflect.Type, definition *Definition) (Collection[any, any], error)

	ListCollectionNames(ctx context.Context) iter.Seq2[string, error]
	CollectionExists(ctx context.Context, name string) (bool, error)
	EnsureCollectionDeleted(ctx context.Context, name string) error
}

StoreProvider implements the database operations behind a Store. Implementations must support concurrent calls and treat inputs as read-only.

type VectorProperty

type VectorProperty struct {
	Name                string
	Type                reflect.Type
	StorageName         string
	ProviderAnnotations map[string]any
	Dimensions          int
	IndexKind           IndexKind
	DistanceFunction    DistanceFunction
	EmbeddingGenerator  *embedding.BoundGenerator
	EmbeddingType       reflect.Type
}

VectorProperty describes a vector field or input for embedding generation. Dimensions must be positive. IndexKind and DistanceFunction default to the provider's choices. Type can be inferred from a struct field.

EmbeddingGenerator overrides collection defaults; create it with embedding.Bind. For a field supported natively by the provider, an explicit EmbeddingType must match the field type. Its generator may still embed search queries. For other fields, EmbeddingType requests a supported generator output type; an explicit value must match the selected generator's declared output. The field must exactly match the generator's concrete input type or implement its input interface.

Without a generator, an explicit EmbeddingType can describe the output of provider-managed embedding generation. Providers validate supported types and their representation-specific storage requirements.

Directories

Path Synopsis
Package inmemory implements an in-process vector store for testing and prototyping.
Package inmemory implements an in-process vector store for testing and prototyping.
internal
recordvalue
Package recordvalue provides shared local filtering and ordering semantics.
Package recordvalue provides shared local filtering and ordering semantics.
Package providerservices provides experimental model-building, record-access, vector-conversion, embedding-batching, and local filtering and sorting support for vector data providers, not applications.
Package providerservices provides experimental model-building, record-access, vector-conversion, embedding-batching, and local filtering and sorting support for vector data providers, not applications.

Jump to

Keyboard shortcuts

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