redis

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: 11 Imported by: 0

Documentation

Overview

Package redis 提供 Redis Stack (RediSearch) 向量存储集成

Redis Stack 通过 RediSearch 模块提供向量搜索能力, 适合需要低延迟、高吞吐量的实时向量检索场景。

特性:

  • 亚毫秒级查询延迟
  • 支持 FLAT 和 HNSW 索引
  • 混合查询(向量 + 属性过滤)
  • 实时索引更新
  • 内存优先,可持久化

前置条件:

  • Redis Stack 7.0+ (redis/redis-stack Docker 镜像)
  • 或 Redis 7.0+ 带 RediSearch 模块

使用示例:

store, err := redis.NewStore(ctx,
    redis.WithAddr("localhost:6379"),
    redis.WithIndex("doc_index"),
    redis.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"
	// DistanceIP 内积距离
	DistanceIP DistanceMetric = "IP"
)

type IndexAlgorithm

type IndexAlgorithm string

IndexAlgorithm 索引算法

const (
	// AlgorithmFlat 暴力搜索(精确)
	AlgorithmFlat IndexAlgorithm = "FLAT"
	// AlgorithmHNSW HNSW 索引(近似,推荐)
	AlgorithmHNSW IndexAlgorithm = "HNSW"
)

type Option

type Option func(*Store)

Option 配置选项

func WithAddr

func WithAddr(addr string) Option

WithAddr 设置 Redis 地址

func WithAlgorithm

func WithAlgorithm(algo IndexAlgorithm) Option

WithAlgorithm 设置索引算法

func WithClient

func WithClient(client *goredis.Client) Option

WithClient 设置已有的 Redis 客户端

func WithDimension

func WithDimension(dim int) Option

WithDimension 设置向量维度

func WithDistance

func WithDistance(d DistanceMetric) Option

WithDistance 设置距离度量

func WithIndex

func WithIndex(index string) Option

WithIndex 设置索引名称

func WithPassword

func WithPassword(password string) Option

WithPassword 设置密码

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix 设置 key 前缀

type Store

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

Store Redis Stack 向量存储

func NewStore

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

NewStore 创建 Redis Stack 向量存储

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