pgvector

package
v0.5.10 Latest Latest
Warning

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

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

Documentation

Overview

Package pgvector 提供 PostgreSQL pgvector 扩展的向量存储集成

pgvector 是 PostgreSQL 的开源向量搜索扩展,适合已有 PostgreSQL 基础设施的团队。

特性:

  • 基于成熟的 PostgreSQL 生态
  • 支持 IVFFlat 和 HNSW 索引
  • 精确和近似最近邻搜索
  • 事务支持,ACID 保证
  • 与现有 SQL 数据无缝结合

前置条件:

  • PostgreSQL 15+
  • pgvector 扩展 (CREATE EXTENSION vector)

使用示例:

store, err := pgvector.NewStore(ctx,
    pgvector.WithDSN("postgres://user:pass@localhost:5432/mydb"),
    pgvector.WithTable("embeddings"),
    pgvector.WithDimension(1536),
)
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 DistanceMetric

type DistanceMetric string

DistanceMetric 距离度量方式

const (
	// DistanceCosine 余弦距离(默认)
	DistanceCosine DistanceMetric = "cosine"
	// DistanceL2 欧几里得距离(L2)
	DistanceL2 DistanceMetric = "l2"
	// DistanceInnerProduct 内积距离
	DistanceInnerProduct DistanceMetric = "inner_product"
)

type IndexType

type IndexType string

IndexType 索引类型

const (
	// IndexIVFFlat IVFFlat 索引(适合中小规模数据)
	IndexIVFFlat IndexType = "ivfflat"
	// IndexHNSW HNSW 索引(适合大规模数据,推荐)
	IndexHNSW IndexType = "hnsw"
	// IndexNone 不创建索引(精确搜索)
	IndexNone IndexType = "none"
)

type Option

type Option func(*Store)

Option 配置选项

func WithDB

func WithDB(db *sql.DB) Option

WithDB 设置已有的数据库连接

func WithDSN

func WithDSN(dsn string) Option

WithDSN 设置 PostgreSQL 连接字符串

func WithDimension

func WithDimension(dim int) Option

WithDimension 设置向量维度

func WithDistance

func WithDistance(d DistanceMetric) Option

WithDistance 设置距离度量方式

func WithIndex

func WithIndex(idx IndexType) Option

WithIndex 设置索引类型

func WithTable

func WithTable(table string) Option

WithTable 设置表名

type Store

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

Store pgvector 向量存储

func NewStore

func NewStore(ctx context.Context, opts ...Option) (*Store, error)

NewStore 创建 pgvector 向量存储

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) Search

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

Search 搜索相似文档

Jump to

Keyboard shortcuts

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