faiss

package
v0.5.11 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

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

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 IndexType

type IndexType string

IndexType 索引类型

const (
	// IndexFlat 暴力搜索(精确,适合小规模数据)
	IndexFlat IndexType = "flat"

	// IndexIVFFlat IVF + Flat(适合中等规模数据)
	IndexIVFFlat IndexType = "ivf_flat"

	// IndexHNSW HNSW 索引(适合大规模数据,推荐)
	IndexHNSW IndexType = "hnsw"

	// IndexIVFPQ IVF + PQ(高压缩比,适合超大规模数据)
	IndexIVFPQ IndexType = "ivf_pq"
)

type MetricType

type MetricType string

MetricType 距离度量方式

const (
	// MetricL2 L2 距离(欧几里得距离)
	MetricL2 MetricType = "l2"

	// MetricCosine 余弦距离
	MetricCosine MetricType = "cosine"

	// MetricInnerProduct 内积
	MetricInnerProduct MetricType = "inner_product"
)

type Option

type Option func(*Store)

Option 配置选项

func WithDimension

func WithDimension(dim int) Option

WithDimension 设置向量维度

func WithEngine

func WithEngine(engine FaissEngine) Option

WithEngine 设置自定义 Faiss 引擎(如 CGo 绑定)

func WithIndexPath

func WithIndexPath(path string) Option

WithIndexPath 设置索引文件路径(用于持久化)

func WithIndexType

func WithIndexType(t IndexType) Option

WithIndexType 设置索引类型

func WithMetric

func WithMetric(m MetricType) Option

WithMetric 设置距离度量方式

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 向量存储

func NewStore

func NewStore(opts ...Option) (*Store, error)

NewStore 创建 Faiss 向量存储

如果未指定自定义引擎,使用内置的纯 Go 内存实现。

func (*Store) Add

func (s *Store) Add(ctx context.Context, docs []vector.Document) error

Add 添加文档

func (*Store) Clear

func (s *Store) Clear(ctx context.Context) error

Clear 清空存储

func (*Store) Close

func (s *Store) Close() error

Close 关闭存储

func (*Store) Count

func (s *Store) Count(ctx context.Context) (int, error)

Count 返回文档数量

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, ids []string) error

Delete 删除文档

func (*Store) Load

func (s *Store) Load(path string) error

Load 从文件加载索引

func (*Store) Save

func (s *Store) Save(path string) error

Save 保存索引到文件

func (*Store) Search

func (s *Store) Search(ctx context.Context, embedding []float32, topK int, filter map[string]any) ([]vector.Document, error)

Search 搜索相似文档

func (*Store) Stats

func (s *Store) Stats(ctx context.Context) map[string]any

Stats 返回存储统计信息

func (*Store) Train

func (s *Store) Train(ctx context.Context, vectors [][]float32) error

Train 训练索引(IVF 类索引在大量数据插入前需要先训练)

Jump to

Keyboard shortcuts

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