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
- type Option
- func WithAddress(address string) Option
- func WithAuth(username, password string) Option
- func WithCollection(collection string) Option
- func WithDimension(dim int) Option
- func WithHTTPClient(client *http.Client) Option
- func WithIndexType(indexType string) Option
- func WithMetricType(metricType string) Option
- 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) CreateIndex(ctx context.Context) error
- func (s *Store) Delete(ctx context.Context, ids []string) error
- func (s *Store) DropIndex(ctx context.Context) error
- func (s *Store) Flush(ctx context.Context) error
- func (s *Store) Get(ctx context.Context, id string) (*vector.Document, error)
- func (s *Store) LoadCollection(ctx context.Context) error
- func (s *Store) ReleaseCollection(ctx context.Context) error
- func (s *Store) Search(ctx context.Context, query []float32, k int, opts ...vector.SearchOption) ([]vector.Document, error)
- func (s *Store) Stats(ctx context.Context) (map[string]any, error)
- func (s *Store) Update(ctx context.Context, docs []vector.Document) error
Constants ¶
const DefaultAddress = "http://localhost:19530"
DefaultAddress 默认 Milvus HTTP 地址
const DefaultCollection = "hexagon_documents"
DefaultCollection 默认集合名称
const DefaultDimension = 1536
DefaultDimension 默认向量维度
const DefaultIndexType = "HNSW"
DefaultIndexType 默认索引类型
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 ¶
WithAddress 设置 Milvus HTTP 地址
地址格式应为 "http://host:port" 或 "https://host:port"。 如果省略 scheme,自动添加 "http://"。
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store Milvus 向量存储(HTTP REST API 实现)
通过 Milvus RESTful API v2 (POST /v2/vectordb/...) 实现完整的向量存储功能。 线程安全,所有方法均可并发调用。
func NewStore ¶
NewStore 创建 Milvus 向量存储
连接到 Milvus HTTP REST API,自动创建集合(如果不存在)并加载到内存。
参数:
- ctx: 上下文
- opts: 配置选项
返回:
- *Store: Milvus 存储实例
- error: 连接或初始化失败时返回错误
func (*Store) CreateIndex ¶
CreateIndex 创建向量索引
func (*Store) LoadCollection ¶
LoadCollection 加载集合到内存
func (*Store) ReleaseCollection ¶
ReleaseCollection 释放集合
func (*Store) Search ¶
func (s *Store) Search(ctx context.Context, query []float32, k int, opts ...vector.SearchOption) ([]vector.Document, error)
Search 搜索相似文档
使用向量相似度在 Milvus 中搜索最近邻文档。 返回结果按相似度降序排列。