Documentation
¶
Overview ¶
Package faiss 提供 Facebook AI Similarity Search (Faiss) 向量存储集成
Faiss 是 Facebook Research 开发的高效向量相似性搜索库, 适合大规模向量数据集的离线和在线检索场景。
特性:
- 支持多种索引类型: Flat, IVF, HNSW, PQ, IVF-PQ 等
- GPU 加速支持
- 亚毫秒级查询延迟(内存模式)
- 支持十亿级向量规模
- 索引持久化(序列化到文件)
实现说明:
- 本包提供 Faiss 的 Go 接口封装
- 内置纯 Go 的内存后端作为 fallback(无需 CGo)
- 支持可插拔的 Faiss 引擎接口,可接入 CGo 绑定
使用示例:
store, err := faiss.NewStore(
faiss.WithDimension(1536),
faiss.WithIndexType(faiss.IndexHNSW),
faiss.WithMetric(faiss.MetricCosine),
)
defer store.Close()
store.Add(ctx, docs)
results, err := store.Search(ctx, queryEmbedding, 5)
Index ¶
- type FaissEngine
- type IndexType
- type MetricType
- type Option
- type SearchResult
- type Store
- func (s *Store) Add(ctx context.Context, docs []vector.Document) error
- func (s *Store) Clear(ctx context.Context) error
- func (s *Store) Close() error
- func (s *Store) Count(ctx context.Context) (int, error)
- func (s *Store) Delete(ctx context.Context, ids []string) error
- func (s *Store) Load(path string) error
- func (s *Store) Save(path string) error
- func (s *Store) Search(ctx context.Context, embedding []float32, topK int, filter map[string]any) ([]vector.Document, error)
- func (s *Store) Stats(ctx context.Context) map[string]any
- func (s *Store) Train(ctx context.Context, vectors [][]float32) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FaissEngine ¶
type FaissEngine interface {
// Train 训练索引(IVF 类索引需要)
Train(vectors [][]float32) error
// Add 添加向量
Add(ids []string, vectors [][]float32) error
// Search 搜索最近邻
Search(query []float32, topK int) ([]SearchResult, error)
// Remove 删除向量
Remove(ids []string) error
// Save 保存索引到文件
Save(path string) error
// Load 从文件加载索引
Load(path string) error
// Size 返回索引中的向量数量
Size() int
// Close 关闭引擎释放资源
Close() error
}
FaissEngine Faiss 引擎接口
可以由 CGo 绑定或纯 Go 实现来实现此接口。 默认使用内置的纯 Go 内存实现。
type MetricType ¶
type MetricType string
MetricType 距离度量方式
const ( // MetricL2 L2 距离(欧几里得距离) MetricL2 MetricType = "l2" // MetricCosine 余弦距离 MetricCosine MetricType = "cosine" // MetricInnerProduct 内积 MetricInnerProduct MetricType = "inner_product" )
type SearchResult ¶
type SearchResult struct {
// ID 文档 ID
ID string
// Distance 距离值
Distance float32
// Score 相似度分数 (0-1)
Score float32
}
SearchResult 搜索结果
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store Faiss 向量存储
Click to show internal directories.
Click to hide internal directories.