Documentation
¶
Overview ¶
Package index provides vector indexing implementations
Package index provides advanced indexing structures for vector search ¶
Package index provides various indexing implementations for vector search ¶
Package index provides advanced indexing structures
Index ¶
- func CosineDistance(a, b []float32) float32
- func DotProductDistance(a, b []float32) float32
- func EuclideanDistance(a, b []float32) float32
- type CombineStrategy
- type FlatIndex
- func (f *FlatIndex) BatchInsert(ids []string, vectors [][]float32) error
- func (f *FlatIndex) Clear()
- func (f *FlatIndex) Delete(id string) bool
- func (f *FlatIndex) GetVector(id string) ([]float32, bool)
- func (f *FlatIndex) Insert(id string, vector []float32) error
- func (f *FlatIndex) RangeSearch(query []float32, radius float32) ([]string, []float32)
- func (f *FlatIndex) Search(query []float32, k int) ([]string, []float32)
- func (f *FlatIndex) Size() int
- func (f *FlatIndex) Stats() map[string]interface{}
- type HNSW
- func (h *HNSW) Delete(id string) error
- func (h *HNSW) Insert(id string, vector []float32) error
- func (h *HNSW) InsertBatch(vectors []struct{ ... }) error
- func (h *HNSW) InsertBatchParallel(vectors []struct{ ... }, numWorkers int) error
- func (h *HNSW) Load(r io.Reader) error
- func (h *HNSW) Save(w io.Writer) error
- func (h *HNSW) Search(query []float32, k int, ef int) ([]string, []float32)
- func (h *HNSW) SearchVectorIndex(query []float32, k int) ([]string, []float32)
- func (h *HNSW) SetQuantizer(q Quantizer)
- func (h *HNSW) Size() int
- func (h *HNSW) Stats() map[string]interface{}
- type HNSWAdapter
- type HNSWNode
- type HybridIndex
- func (h *HybridIndex) Delete(id string) error
- func (h *HybridIndex) Insert(id string, vector []float32) error
- func (h *HybridIndex) Search(query []float32, k int) ([]string, []float32)
- func (h *HybridIndex) SetAlpha(alpha float32)
- func (h *HybridIndex) Size() int
- func (h *HybridIndex) Train(vectors [][]float32) error
- type IVFAdapter
- type IVFIndex
- func (ivf *IVFIndex) Add(id string, vector []float32) error
- func (ivf *IVFIndex) Clear()
- func (ivf *IVFIndex) Delete(id string) error
- func (ivf *IVFIndex) Load(r io.Reader) error
- func (ivf *IVFIndex) Save(w io.Writer) error
- func (ivf *IVFIndex) Search(query []float32, k int) ([]string, []float32, error)
- func (ivf *IVFIndex) SetNProbe(nprobe int)
- func (ivf *IVFIndex) Size() int
- func (ivf *IVFIndex) Stats() map[string]interface{}
- func (ivf *IVFIndex) Train(vectors [][]float32) error
- type IndexType
- type LSHConfig
- type LSHIndex
- func (lsh *LSHIndex) Clear()
- func (lsh *LSHIndex) Delete(id string) bool
- func (lsh *LSHIndex) Insert(id string, vector []float32) error
- func (lsh *LSHIndex) Search(query []float32, k int) ([]LSHSearchResult, error)
- func (lsh *LSHIndex) SearchWithMultiProbe(query []float32, k int, numProbes int) ([]LSHSearchResult, error)
- func (lsh *LSHIndex) SetDistanceFunc(distFunc func([]float32, []float32) float32)
- func (lsh *LSHIndex) Size() int
- func (lsh *LSHIndex) Stats() map[string]interface{}
- type LSHSearchResult
- type MultiIndex
- func (m *MultiIndex) AddIndex(indexType IndexType, index VectorIndex)
- func (m *MultiIndex) Delete(id string) error
- func (m *MultiIndex) Insert(id string, vector []float32) error
- func (m *MultiIndex) Search(query []float32, k int) ([]string, []float32)
- func (m *MultiIndex) Size() int
- func (m *MultiIndex) Stats() map[string]interface{}
- type MultiIndexConfig
- type Quantizer
- type VectorIndex
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CosineDistance ¶
CosineDistance computes cosine distance (1 - cosine similarity)
func DotProductDistance ¶
DotProductDistance computes negative dot product (for similarity)
func EuclideanDistance ¶
EuclideanDistance computes Euclidean distance
Types ¶
type CombineStrategy ¶
type CombineStrategy string
CombineStrategy defines how to combine results from multiple indices
const ( // Take best results from primary index only StrategyPrimaryOnly CombineStrategy = "primary_only" // Merge results from all indices StrategyMergeAll CombineStrategy = "merge_all" // Use primary for candidates, secondary for reranking StrategyRerank CombineStrategy = "rerank" // Voting-based combination StrategyVoting CombineStrategy = "voting" )
type FlatIndex ¶
type FlatIndex struct {
// contains filtered or unexported fields
}
FlatIndex implements a brute-force exact search index This guarantees finding the exact nearest neighbors but with O(n) complexity
func NewFlatIndex ¶
NewFlatIndex creates a new brute-force index
func NewFlatIndexCosine ¶
NewFlatIndexCosine creates a flat index optimized for cosine similarity
func (*FlatIndex) BatchInsert ¶
BatchInsert adds multiple vectors efficiently
func (*FlatIndex) RangeSearch ¶
RangeSearch finds all vectors within a specified distance from the query
type HNSW ¶
type HNSW struct {
// Parameters
M int // Max number of bi-directional links per node
MaxM int // Max number of links for layer 0
EfConstruction int // Size of dynamic candidate list
ML float64 // Level assignment probability
Seed int64 // Random seed
// Index data
Nodes map[string]*HNSWNode
EntryPoint string
// Distance function
DistFunc func(a, b []float32) float32
// Quantization
Quantizer Quantizer
// contains filtered or unexported fields
}
HNSW implements Hierarchical Navigable Small World index
func (*HNSW) InsertBatch ¶ added in v2.8.0
InsertBatch inserts multiple vectors into the index efficiently This is faster than calling Insert multiple times as it avoids repeated lock/unlock overhead
func (*HNSW) InsertBatchParallel ¶ added in v2.8.0
func (h *HNSW) InsertBatchParallel(vectors []struct { ID string Vector []float32 }, numWorkers int) error
InsertBatchParallel inserts vectors in parallel using multiple goroutines
func (*HNSW) SearchVectorIndex ¶
SearchVectorIndex provides VectorIndex-compatible search with default ef
func (*HNSW) SetQuantizer ¶
SetQuantizer sets the quantizer for the index
type HNSWAdapter ¶
type HNSWAdapter struct {
*HNSW
// contains filtered or unexported fields
}
HNSWAdapter wraps HNSW to implement VectorIndex interface
func NewHNSWAdapter ¶
func NewHNSWAdapter(dimension, M int, distFunc func([]float32, []float32) float32) *HNSWAdapter
NewHNSWAdapter creates a new HNSW adapter
type HNSWNode ¶
type HNSWNode struct {
ID string
Vector []float32
Quantized []byte // Quantized vector data (optional)
Level int
Neighbors [][]string // Neighbors at each level
Deleted bool
}
HNSWNode represents a node in the HNSW graph
type HybridIndex ¶
type HybridIndex struct {
// contains filtered or unexported fields
}
HybridIndex combines HNSW for speed with IVF for accuracy
func NewHybridIndex ¶
func NewHybridIndex(dimension int, hnswM int, ivfCentroids int) *HybridIndex
NewHybridIndex creates a new hybrid HNSW+IVF index
func (*HybridIndex) Delete ¶
func (h *HybridIndex) Delete(id string) error
Delete removes from both indices
func (*HybridIndex) Insert ¶
func (h *HybridIndex) Insert(id string, vector []float32) error
Insert adds a vector to both indices
func (*HybridIndex) Search ¶
func (h *HybridIndex) Search(query []float32, k int) ([]string, []float32)
Search performs hybrid search
func (*HybridIndex) SetAlpha ¶
func (h *HybridIndex) SetAlpha(alpha float32)
SetAlpha sets the weight between HNSW (0) and IVF (1)
func (*HybridIndex) Train ¶
func (h *HybridIndex) Train(vectors [][]float32) error
Train trains the IVF component
type IVFAdapter ¶
type IVFAdapter struct {
*IVFIndex
}
IVFAdapter wraps IVFIndex to implement VectorIndex interface
func NewIVFAdapter ¶
func NewIVFAdapter(dimension, nCentroids int) *IVFAdapter
NewIVFAdapter creates a new IVF adapter
type IVFIndex ¶
type IVFIndex struct {
NCentroids int // Number of cluster centroids
Dimension int // Vector dimension
Centroids [][]float32 // Cluster centroids
Invlists [][]int // Inverted lists (vector IDs per cluster)
Vectors [][]float32 // Original vectors (optional, for reranking)
IDs []string // Vector IDs
Trained bool
NProbe int // Number of clusters to search
// contains filtered or unexported fields
}
IVFIndex implements Inverted File Index for partitioned vector search
func NewIVFIndex ¶
NewIVFIndex creates a new IVF index
type LSHConfig ¶
type LSHConfig struct {
NumTables int // Number of hash tables (more = better recall, more memory)
NumHashFuncs int // Number of hash functions per table (more = more selective)
Dimension int // Vector dimension
Seed int64 // Random seed for reproducibility
}
LSHConfig contains configuration for LSH index
type LSHIndex ¶
type LSHIndex struct {
// contains filtered or unexported fields
}
LSHIndex implements Locality Sensitive Hashing for fast approximate nearest neighbor search
func (*LSHIndex) Search ¶
func (lsh *LSHIndex) Search(query []float32, k int) ([]LSHSearchResult, error)
Search finds approximate nearest neighbors
func (*LSHIndex) SearchWithMultiProbe ¶
func (lsh *LSHIndex) SearchWithMultiProbe(query []float32, k int, numProbes int) ([]LSHSearchResult, error)
SearchWithMultiProbe uses multi-probe LSH for better recall
func (*LSHIndex) SetDistanceFunc ¶
SetDistanceFunc sets the distance function
type LSHSearchResult ¶
LSHSearchResult represents a search result from LSH index
type MultiIndex ¶
type MultiIndex struct {
// contains filtered or unexported fields
}
MultiIndex combines multiple index types for optimal performance
func NewMultiIndex ¶
func NewMultiIndex(config MultiIndexConfig) *MultiIndex
NewMultiIndex creates a new multi-index
func (*MultiIndex) AddIndex ¶
func (m *MultiIndex) AddIndex(indexType IndexType, index VectorIndex)
AddIndex adds an index to the multi-index
func (*MultiIndex) Delete ¶
func (m *MultiIndex) Delete(id string) error
Delete removes a vector from all indices
func (*MultiIndex) Insert ¶
func (m *MultiIndex) Insert(id string, vector []float32) error
Insert inserts a vector into all indices
func (*MultiIndex) Search ¶
func (m *MultiIndex) Search(query []float32, k int) ([]string, []float32)
Search performs multi-index search based on strategy
func (*MultiIndex) Size ¶
func (m *MultiIndex) Size() int
Size returns the size of the primary index
func (*MultiIndex) Stats ¶
func (m *MultiIndex) Stats() map[string]interface{}
Stats returns statistics for all indices
type MultiIndexConfig ¶
type MultiIndexConfig struct {
// Primary index for fast approximate search
PrimaryIndex IndexType
// Secondary indices for refinement
SecondaryIndices []IndexType
// Strategy for combining results
CombineStrategy CombineStrategy
// Reranking configuration
RerankTopK int
// Parallel search
Parallel bool
}
MultiIndexConfig configures the multi-index strategy