跳转至

使用模式

GoVector 支持多种使用模式,以适应不同的应用场景。本文档解释了不同的模式以及如何有效地使用它们。

🚀 嵌入式模式

嵌入式模式是使用 GoVector 最轻量级的方式。在这种模式下,向量数据库直接在您的应用程序进程中运行,消除了网络开销。

关键特性

  • 零网络开销:所有操作都在内存中执行
  • 简单集成:作为依赖项添加到您的 Go 项目中
  • 高性能:直接访问数据结构
  • 单进程部署:不需要单独的服务器

快速开始

  1. 安装依赖
go get github.com/yourusername/govector
  1. 初始化数据库
import (
    "github.com/yourusername/govector/core"
)

// 使用默认配置创建新集合
collection, err := core.NewCollection(core.CollectionConfig{
    Name:       "my-collection",
    VectorLen:  768, // 嵌入维度
    Metric:     core.Cosine, // 距离度量
    IndexType:  core.HNSW, // 索引类型
    Quantize:   false, // 禁用量化
})
if err != nil {
    log.Fatalf("创建集合失败: %v", err)
}
  1. 执行操作
// 写入点
points := []core.PointStruct{
    {
        ID:     "doc_1",
        Vector: []float32{0.1, 0.2, 0.3, /* ... */},
        Payload: core.Payload{
            "category": "tech",
        },
    },
    // 添加更多点...
}

if err := collection.Upsert(points); err != nil {
    log.Fatalf("写入点失败: %v", err)
}

// 搜索
queryVector := []float32{0.1, 0.2, 0.3, /* ... */}
results, err := collection.Search(queryVector, nil, 10)
if err != nil {
    log.Fatalf("搜索失败: %v", err)
}

for _, result := range results {
    fmt.Printf("ID: %s, Score: %.4f\n", result.ID, result.Score)
}
  1. 持久化和加载
// 保存到磁盘
if err := collection.Save("/path/to/collection"); err != nil {
    log.Fatalf("保存集合失败: %v", err)
}

// 从磁盘加载
loadedCollection, err := core.LoadCollection("/path/to/collection")
if err != nil {
    log.Fatalf("加载集合失败: %v", err)
}

使用场景

  • 边缘设备:直接在资源有限的设备上运行
  • 单用户应用:个人工具的简化部署
  • 高性能服务:消除关键操作的网络延迟
  • 测试和开发:本地开发的简单设置

🌐 微服务模式

微服务模式允许 GoVector 作为独立服务器运行,可通过 HTTP API 访问。这种模式与 Qdrant 的 API 兼容,使它易于与现有的 Qdrant 客户端集成。

关键特性

  • Qdrant 兼容 API:使用现有的 Qdrant 客户端和工具
  • 网络可访问性:从多个应用程序访问
  • 可扩展部署:作为独立服务运行
  • 标准 HTTP 接口:易于与任何编程语言集成

快速开始

  1. 启动服务器
# 从源代码
 go run cmd/server/main.go --host 0.0.0.0 --port 6333 --data ./data

# 或使用编译后的二进制文件
govector-server --host 0.0.0.0 --port 6333 --data ./data
  1. API 端点

GoVector 实现了与 Qdrant 相同的 API 端点,包括:

  • POST /collections - 创建集合
  • GET /collections - 列出集合
  • DELETE /collections/{collection_name} - 删除集合
  • PUT /collections/{collection_name}/points - 写入点
  • POST /collections/{collection_name}/points/search - 搜索点
  • DELETE /collections/{collection_name}/points - 删除点

  • 使用 API

# 创建集合
curl -X POST http://localhost:6333/collections \
  -H "Content-Type: application/json" \
  -d '{
    "name": "my-collection",
    "vectors": {
      "size": 768,
      "distance": "Cosine"
    }
  }'

# 写入点
curl -X PUT http://localhost:6333/collections/my-collection/points \
  -H "Content-Type: application/json" \
  -d '{
    "points": [
      {
        "id": "doc_1",
        "vector": [0.1, 0.2, 0.3, /* ... */],
        "payload": {
          "category": "tech"
        }
      }
    ]
  }'

# 搜索
curl -X POST http://localhost:6333/collections/my-collection/points/search \
  -H "Content-Type: application/json" \
  -d '{
    "vector": [0.1, 0.2, 0.3, /* ... */],
    "limit": 10
  }'
  1. 使用 Qdrant 客户端

您可以使用任何 Qdrant 客户端库与 GoVector 交互:

# 使用 Python 客户端
from qdrant_client import QdrantClient

client = QdrantClient(host="localhost", port=6333)

# 创建集合
client.create_collection(
    collection_name="my-collection",
    vectors_config={"size": 768, "distance": "Cosine"}
)

# 写入点
client.upsert(
    collection_name="my-collection",
    points=[
        {
            "id": "doc_1",
            "vector": [0.1, 0.2, 0.3, /* ... */],
            "payload": {"category": "tech"}
        }
    ]
)

# 搜索
results = client.search(
    collection_name="my-collection",
    query_vector=[0.1, 0.2, 0.3, /* ... */],
    limit=10
)

使用场景

  • 多应用系统:在多个服务之间共享向量数据库
  • 语言无关应用:与非 Go 编程语言一起使用
  • 集中式向量存储:向量数据的单一事实来源
  • 现有 Qdrant 迁移:Qdrant 的直接替代品

🖥️ CLI 模式

GoVector 提供命令行界面,用于基本操作和管理任务。

关键特性

  • 交互式 shell:探索和管理集合
  • 批量操作:导入/导出数据
  • 集合管理:创建、列出和删除集合
  • 状态监控:检查数据库健康和统计信息

快速开始

  1. 启动 CLI
# 从源代码
 go run cmd/cli/main.go

# 或使用编译后的二进制文件
govector-cli
  1. 基本命令
# 创建集合
create-collection --name my-collection --vector-len 768 --metric cosine

# 从文件写入点
upsert --collection my-collection --file points.json

# 搜索
search --collection my-collection --vector "[0.1, 0.2, 0.3]" --limit 10

# 列出集合
list-collections

# 删除集合
delete-collection --name my-collection
  1. 交互模式
$ govector-cli

GoVector CLI v1.0.0
Type 'help' for available commands

> create-collection --name my-collection --vector-len 768
Collection 'my-collection' created successfully

> upsert --collection my-collection --points '{"id": "doc_1", "vector": [0.1, 0.2, 0.3], "payload": {"category": "tech"}}'
1 point upserted successfully

> search --collection my-collection --vector "[0.1, 0.2, 0.3]" --limit 5
Results:
ID: doc_1, Score: 1.0000, Payload: map[category:tech]

> exit

使用场景

  • 管理任务:管理集合和数据
  • 数据迁移:在系统之间导入/导出
  • 测试和调试:验证功能
  • 快速原型:无需编写代码即可测试向量搜索

📊 模式比较

特性 嵌入式模式 微服务模式 CLI 模式
网络开销 HTTP 请求 无(本地)
集成 仅 Go 代码 任何语言 命令行
部署 单进程 独立服务 本地工具
性能 最高 中等 中等
设置难度 非常简单 简单 非常简单
可扩展性 有限 有限

💡 最佳实践

选择合适的模式

  • 嵌入式模式:最适合单应用、高性能场景
  • 微服务模式:最适合多应用、语言无关场景
  • CLI 模式:最适合管理任务和调试

性能考虑

  • 嵌入式模式
  • 对大型数据集使用 HNSW 索引
  • 启 SQ8 量化以优化内存
  • 对批量插入使用批处理操作

  • 微服务模式

  • 使用适当的 HTTP 客户端配置(超时、重试)
  • 批处理请求以减少网络开销
  • 考虑高流量场景的负载均衡

安全考虑

  • 微服务模式
  • 在生产环境中使用 HTTPS
  • 如果暴露在外部网络中,实现身份验证
  • 限制对可信客户端的网络访问

🚩 常见问题

嵌入式模式

  • 内存使用:大型集合可能消耗大量内存
  • 解决方案:启 SQ8 量化,使用适当的索引参数

  • 并发:多个 goroutine 访问同一个集合

  • 解决方案:使用适当的同步,考虑只读副本

微服务模式

  • 网络延迟:频繁操作的 HTTP 开销
  • 解决方案:批处理请求,使用持久连接

  • API 兼容性:与 Qdrant API 的差异

  • 解决方案:参考 API 参考了解支持的端点

CLI 模式

  • 大型数据集:导入/导出性能
  • 解决方案:使用批处理,分割大文件

🔗 相关文档