milvus

package
v0.5.12 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 milvus 提供 Milvus 向量数据库集成

Package milvus 提供 Milvus 向量数据库集成(HTTP REST API v2)

本包通过 Milvus 的 RESTful API v2 实现向量存储,无需额外的 gRPC 依赖。 所有操作通过 HTTP POST 请求完成,支持标准的 Milvus 2.4+ 服务器。

Milvus 是一个开源的云原生向量数据库,专为海量向量数据的存储、索引和管理而设计。

特性:

  • 支持十亿级向量检索
  • 多种索引类型 (FLAT, IVF_FLAT, HNSW)
  • 分布式架构
  • 无 gRPC 依赖,仅使用 HTTP REST API

使用示例:

store, err := milvus.NewStore(ctx,
    milvus.WithAddress("http://localhost:19530"),
    milvus.WithCollection("documents"),
    milvus.WithDimension(1536),
)
defer store.Close()

docs := []vector.Document{
    {ID: "1", Content: "Hello", Embedding: embedding},
}
store.Add(ctx, docs)

Index

Constants

View Source
const DefaultAddress = "http://localhost:19530"

DefaultAddress 默认 Milvus HTTP 地址

View Source
const DefaultCollection = "hexagon_documents"

DefaultCollection 默认集合名称

View Source
const DefaultDimension = 1536

DefaultDimension 默认向量维度

View Source
const DefaultIndexType = "HNSW"

DefaultIndexType 默认索引类型

View Source
const DefaultMetricType = "COSINE"

DefaultMetricType 默认距离度量类型

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Store)

Option Store 配置选项

func WithAddress

func WithAddress(address string) Option

WithAddress 设置 Milvus HTTP 地址

地址格式应为 "http://host:port" 或 "https://host:port"。 如果省略 scheme,自动添加 "http://"。

func WithAuth

func WithAuth(username, password string) Option

WithAuth 设置认证信息

Milvus REST API 使用 Bearer Token 认证, Token 格式为 "username:password"。

func WithCollection

func WithCollection(collection string) Option

WithCollection 设置集合名称

func WithDimension

func WithDimension(dim int) Option

WithDimension 设置向量维度

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient 设置自定义 HTTP 客户端

主要用于测试场景注入 mock 客户端。

func WithIndexType

func WithIndexType(indexType string) Option

WithIndexType 设置索引类型

支持: FLAT, IVF_FLAT, IVF_SQ8, HNSW

func WithMetricType

func WithMetricType(metricType string) Option

WithMetricType 设置距离度量类型

支持: L2, IP (内积), COSINE (余弦)

type Store

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

Store Milvus 向量存储(HTTP REST API 实现)

通过 Milvus RESTful API v2 (POST /v2/vectordb/...) 实现完整的向量存储功能。 线程安全,所有方法均可并发调用。

func NewStore

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

NewStore 创建 Milvus 向量存储

连接到 Milvus HTTP REST API,自动创建集合(如果不存在)并加载到内存。

参数:

  • ctx: 上下文
  • opts: 配置选项

返回:

  • *Store: Milvus 存储实例
  • error: 连接或初始化失败时返回错误

func (*Store) Add

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

Add 添加文档

将文档批量插入 Milvus。每个文档必须包含与配置维度匹配的嵌入向量。 如果文档未提供 ID,将自动生成唯一 ID。

func (*Store) Clear

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

Clear 清空所有文档

通过删除并重建集合实现清空操作。

func (*Store) Close

func (s *Store) Close() error

Close 关闭存储连接

HTTP 连接是无状态的,此方法仅标记连接状态为已关闭。 关闭后的所有操作将返回错误。

func (*Store) Count

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

Count 统计文档数量

通过查询所有 ID 来统计文档总数。

func (*Store) CreateIndex

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

CreateIndex 创建向量索引

func (*Store) Delete

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

Delete 删除文档

func (*Store) DropIndex

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

DropIndex 删除向量索引

func (*Store) Flush

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

Flush 刷新数据到磁盘

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (*vector.Document, error)

Get 根据 ID 获取文档

func (*Store) LoadCollection

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

LoadCollection 加载集合到内存

func (*Store) ReleaseCollection

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

ReleaseCollection 释放集合

func (*Store) Search

func (s *Store) Search(ctx context.Context, query []float32, k int, opts ...vector.SearchOption) ([]vector.Document, error)

Search 搜索相似文档

使用向量相似度在 Milvus 中搜索最近邻文档。 返回结果按相似度降序排列。

func (*Store) Stats

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

Stats 获取统计信息

func (*Store) Update

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

Update 更新文档

Milvus 通过 upsert(插入或更新)实现文档更新。

Jump to

Keyboard shortcuts

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