Milvus 上手教程
关联项目:Milvus 前置要求:Linux/macOS/WSL 或 Windows(Docker Desktop);Docker 与 Docker Compose;Python 3.9+;了解向量相似度、ANN 与 embedding 的基本概念。 预计耗时:Standalone 部署约 20–40 分钟;跑通首个检索示例约 30 分钟;集群与调优另计。 官方仓库:github.com/milvus-io/milvus
本教程带你完成从环境准备到跑通核心工作流的完整过程。命令与示例针对主流环境给出,实际版本参数请以官方文档为准。
一、环境准备
本地开发使用 Docker + Python 客户端:
docker --version
docker compose version
python3 -m pip install "pymilvus>=2.4"
python3 -c "import pymilvus; print(pymilvus.__version__)"
版本要求以官方文档为准;Milvus Lite 只需 pip install pymilvus 且无需 Docker。
二、安装部署
官方提供 standalone 启动脚本:
curl -sfL https://raw.githubusercontent.com/milvus-io/milvus/master/scripts/standalone_embed.sh -o standalone_embed.sh
bash standalone_embed.sh start
docker ps --filter name=milvus
预期输出:出现名为 milvus 且状态为 Up 的容器,并映射 19530 服务端口;具体字段以官方文档为准。停止用 bash standalone_embed.sh stop。
三、快速上手:跑通第一个示例
用 MilvusClient 建集合、写入并检索(服务端需先启动):
from pymilvus import MilvusClient
client = MilvusClient(uri="http://localhost:19530")
client.create_collection(collection_name="demo", dimension=3)
client.insert(
collection_name="demo",
data=[
{"id": 1, "vector": [0.1, 0.2, 0.3], "text": "hello milvus"},
{"id": 2, "vector": [0.9, 0.1, 0.1], "text": "another doc"},
],
)
hits = client.search(
collection_name="demo",
data=[[0.1, 0.2, 0.3]],
limit=2,
output_fields=["text"],
)
print(hits)
dimension 必须与 embedding 维度一致;data 是待查询向量列表,limit 为返回条数,output_fields 指定回传的标量字段。返回结果结构与具体参数以所装 pymilvus 版本的官方文档为准。
四、核心工作流
任务:为一个小型知识库建立可过滤的语义检索
- 建集合:定义主键、向量维度与标量字段。
from pymilvus import MilvusClient, DataType
from pymilvus import milvus_client
client = MilvusClient(uri="http://localhost:19530")
schema = client.create_schema()
schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True, auto_id=False)
schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=3)
schema.add_field(field_name="text", datatype=DataType.VARCHAR, max_length=256)
schema.add_field(field_name="source", datatype=DataType.VARCHAR, max_length=64)
client.create_collection(collection_name="kb", schema=schema)
预期:MilvusClient 返回空结果并无异常,集合出现在 client.list_collections()。
- 建索引:为向量字段指定索引类型与度量。
index_params = client.prepare_index_params()
index_params.add_index(field_name="vector", index_type="HNSW", metric_type="COSINE", params={"M": 16, "efConstruction": 200})
client.create_index(collection_name="kb", index_params=index_params)
预期:client.describe_index 能看到新建索引;参数含义以官方文档为准。
- 插入数据:批量写入带元数据的文档向量。
client.insert(collection_name="kb", data=[
{"id": 1, "vector": [0.1, 0.2, 0.3], "text": "向量数据库入门", "source": "blog"},
{"id": 2, "vector": [0.2, 0.1, 0.4], "text": "RAG 架构说明", "source": "doc"},
])
client.flush(collection_name="kb")
预期:insert 返回插入计数。
- 过滤检索:只召回指定来源。
res = client.search(
collection_name="kb",
data=[[0.1, 0.2, 0.3]],
filter="source == 'doc'",
limit=5,
output_fields=["text", "source"],
)
print(res)
预期:结果中只含 source=doc 的记录;过滤表达式语法以官方文档为准。
- 清理:删除集合释放资源。
client.drop_collection(collection_name="kb")
五、进阶实践
- Milvus Lite:
client = MilvusClient(uri="./milvus_demo.db")无需服务端,适合单测与教程。 - 分区裁剪:
client.create_partition(collection_name="kb", partition_name="tenant_a"),检索时用partition_names=["tenant_a"]。 - 混合检索:新版本支持 dense 与 sparse(BM25)两路召回后重排,配置字段与参数以官方文档为准。
- 一致性级别:
search(..., consistency_level="Bounded")在延迟与新鲜度之间权衡,取值以官方文档为准。 - 可视化管理:Attu 提供集合浏览、数据导入与索引监控,启动命令见其仓库文档。
六、常见问题
Q:Milvus、Milvus Lite 和 Zilliz Cloud 有什么区别?
A:Milvus 指可自托管的开源服务(Standalone/Cluster);Milvus Lite 是嵌入式本地模式;Zilliz Cloud 是官方托管服务,服务端并非都在开源仓库中。
Q:create_collection 报维度不匹配?
A:检查 dimension 是否与 embedding 模型输出一致,且检索向量长度相同;修改维度需重建集合。
Q:为什么刚插入的数据搜不到?
A:数据写入后索引构建与可见性有延迟,可调用 flush() 或调整 consistency_level,具体语义以官方文档为准。
Q:Docker 启动后连接 19530 被拒绝?
A:确认容器状态、端口映射与本机防火墙;Standalone 首次启动需要等待依赖组件就绪。
Q:可以把 Milvus 直接用于生产多租户吗?
A:可以,但需自行设计租户隔离(Partition 或独立 Collection)、鉴权、备份与容量规划。