Milvus Vector Database Skill
Operate Milvus vector databases directly through Python code using the pymilvus SDK. Covers the full lifecycle — connecting, schema design, collection management, vector CRUD, search, hybrid search, full-text search, indexing, partitions, databases, and RBAC.
When to Use
Use this skill when the user wants to:
- - Connect to a Milvus instance (local, standalone, cluster, or Milvus Lite)
- Create collections with custom schemas
- Insert, upsert, search, query, get, or delete vectors
- Perform hybrid search with reranking
- Perform full-text search (BM25)
- Manage indexes, partitions, databases
- Set up users, roles, and access control (RBAC)
- Build RAG pipelines, semantic search, or recommendation systems with Milvus
- Iterate over large result sets with search/query iterators
Requirements
- - Python 3.8+
- INLINECODE1 (
pip install pymilvus) - A running Milvus instance, or use Milvus Lite (embedded, file-based) for development
Capabilities Overview
| Area | What You Can Do |
|---|
| Connection | Connect to Milvus Lite, Standalone, Cluster, or Zilliz Cloud |
| Collections |
Create (quick or custom schema), list, describe, drop, rename, truncate, load, release |
|
Vectors | Insert, upsert, search, hybrid search, query, get, delete |
|
Full-Text Search | BM25-based keyword search with sparse vectors |
|
Iterators | Paginated search and query over large datasets |
|
Indexes | Create (AUTOINDEX, HNSW, IVF_FLAT, etc.), list, describe, drop |
|
Partitions | Create, list, load, release, drop |
|
Databases | Create, list, switch, drop |
|
RBAC | Users, roles, privileges management |
Connection
IMPORTANT: Before writing any connection code, you MUST ask the user for their connection details. Ask:
- 1. Deployment type — Milvus Lite (local file), Standalone/Cluster (self-hosted), or Zilliz Cloud (managed)?
- URI — For self-hosted: host and port (e.g.,
http://localhost:19530). For Zilliz Cloud: the endpoint URL. - Authentication — Token, API key, or username/password if required.
- Database name — If not using the default database.
Never assume or hardcode connection parameters. Use Milvus Lite (uri="./milvus.db") only if the user explicitly wants local/embedded mode for development.
CODEBLOCK0
Parameters:
| Parameter | Type | Description |
|---|
| INLINECODE5 | str | INLINECODE6 for Milvus Lite, "http://host:19530" for server |
| INLINECODE8 |
str | API key or
"username:password" |
|
user | str | Username (alternative to token) |
|
password | str | Password (alternative to token) |
|
db_name | str | Target database (default:
"default") |
|
timeout | float | Operation timeout in seconds |
Async Client
CODEBLOCK1
Collection Management
Quick Create (auto schema + auto index + auto load)
CODEBLOCK2
This automatically creates an id field (INT64, primary key, autoid), a vector field (FLOATVECTOR), AUTOINDEX, and auto-loads the collection.
Custom Schema Create
CODEBLOCK3
See references/collection.md for data types, addfield parameters, and all collection operations.
Other Collection Operations
CODEBLOCK4
- - Quick create is best for prototyping; use custom schema for production.
- A collection must be loaded before search or query.
- Use
enable_dynamic_field=True to allow inserting fields not defined in the schema.
Vector Operations
See references/vector.md for hybrid search, full-text search, iterators, filter syntax, and detailed examples.
Insert / Upsert
CODEBLOCK5
Search (vector similarity)
CODEBLOCK6
Query / Get / Delete
CODEBLOCK7
- - Never use fake or placeholder vectors (e.g.,
[0.1, 0.2, ...]). Always generate vectors from an embedding model. - Use
pip install "pymilvus[model]" for built-in embedding functions, or use any embedding model (OpenAI, Cohere, etc.). - Vector dimension in search must match the collection schema exactly.
- The query embedding model must be the same model used to generate the stored vectors.
- For large inserts, batch data into chunks (e.g., 1000 rows per batch).
- For large result sets, use iterators — see references/vector.md.
Index Management
See references/index.md for index types, metric types, and parameters.
CODEBLOCK8
- -
AUTOINDEX is recommended for most use cases. - An index is required before loading a collection.
Additional Features
references/database.md |
| User & Role Management (RBAC) |
references/user-role.md |
| Common Patterns (RAG, Semantic Search) |
references/patterns.md |
General Guidance
- - Always ask the user for connection details (URI, token/credentials) before writing connection code. Never assume or hardcode connection parameters.
- Never generate fake or placeholder vectors. Always use an embedding model to produce real vectors. Suggest
pip install "pymilvus[model]" for built-in embedding functions. - For quick prototyping, use Milvus Lite (
uri="./file.db") — no server needed, but only if the user explicitly requests local/embedded mode. - A collection must be loaded into memory before search/query.
- The vector dimension in search data must exactly match the collection schema.
- The query embedding model must be the same model used to generate the stored vectors.
- Before any destructive operation (drop collection, drop database, delete vectors), always confirm with the user.
- Use
enable_dynamic_field=True when the schema may evolve. - Prefer
AUTOINDEX unless the user has specific performance requirements. - Use
truncate_collection to clear all data without dropping the collection. - For large datasets, use iterators (
search_iterator, query_iterator) instead of increasing limit.
Milvus 向量数据库技能
通过 pymilvus SDK 直接使用 Python 代码操作 Milvus 向量数据库。涵盖完整生命周期——连接、模式设计、集合管理、向量增删改查、搜索、混合搜索、全文搜索、索引、分区、数据库和 RBAC。
何时使用
当用户希望执行以下操作时使用此技能:
- - 连接到 Milvus 实例(本地、单机、集群或 Milvus Lite)
- 使用自定义模式创建集合
- 插入、更新、搜索、查询、获取或删除向量
- 执行带重排序的混合搜索
- 执行全文搜索(BM25)
- 管理索引、分区、数据库
- 设置用户、角色和访问控制(RBAC)
- 使用 Milvus 构建 RAG 流水线、语义搜索或推荐系统
- 使用搜索/查询迭代器遍历大型结果集
要求
- - Python 3.8+
- pymilvus(pip install pymilvus)
- 一个运行中的 Milvus 实例,或使用 Milvus Lite(嵌入式、基于文件)进行开发
功能概览
| 领域 | 可执行操作 |
|---|
| 连接 | 连接到 Milvus Lite、单机版、集群版或 Zilliz Cloud |
| 集合 |
创建(快速或自定义模式)、列出、描述、删除、重命名、清空、加载、释放 |
|
向量 | 插入、更新、搜索、混合搜索、查询、获取、删除 |
|
全文搜索 | 基于 BM25 的关键词搜索(使用稀疏向量) |
|
迭代器 | 对大型数据集进行分页搜索和查询 |
|
索引 | 创建(AUTOINDEX、HNSW、IVF_FLAT 等)、列出、描述、删除 |
|
分区 | 创建、列出、加载、释放、删除 |
|
数据库 | 创建、列出、切换、删除 |
|
RBAC | 用户、角色、权限管理 |
连接
重要:在编写任何连接代码之前,您必须询问用户的连接详情。 询问:
- 1. 部署类型 —— Milvus Lite(本地文件)、单机版/集群版(自托管)或 Zilliz Cloud(托管服务)?
- URI —— 自托管:主机和端口(例如 http://localhost:19530)。Zilliz Cloud:端点 URL。
- 认证 —— 令牌、API 密钥或用户名/密码(如果需要)。
- 数据库名称 —— 如果不使用默认数据库。
切勿假设或硬编码连接参数。 仅在用户明确要求本地/嵌入式模式进行开发时使用 Milvus Lite(uri=./milvus.db)。
python
from pymilvus import MilvusClient
Milvus Lite(嵌入式、基于文件——非常适合开发/测试)
client = MilvusClient(uri=./milvus.db)
单机版 / 集群版 Milvus(询问用户实际的主机:端口和凭据)
client = MilvusClient(uri=<用户
URI>, token=<用户令牌>)
Zilliz Cloud(询问用户的端点和 API 密钥)
client = MilvusClient(uri=<用户
ZILLIZ端点>, token=<用户
API密钥>)
参数:
| 参数 | 类型 | 描述 |
|---|
| uri | str | Milvus Lite 使用 ./file.db,服务器使用 http://host:19530 |
| token |
str | API 密钥或 username:password |
| user | str | 用户名(令牌的替代方案) |
| password | str | 密码(令牌的替代方案) |
| db_name | str | 目标数据库(默认:default) |
| timeout | float | 操作超时时间(秒) |
异步客户端
python
from pymilvus import AsyncMilvusClient
async with AsyncMilvusClient(uri=<用户_URI>) as client:
results = await client.search(collectionname=mycollection, data=[query_vector], limit=10)
集合管理
快速创建(自动模式 + 自动索引 + 自动加载)
python
client.create_collection(
collectionname=mycollection,
dimension=768,
metric_type=COSINE # 可选:COSINE(默认)、L2、IP
)
这将自动创建 id 字段(INT64,主键,自动 ID)、vector 字段(FLOAT_VECTOR)、AUTOINDEX,并自动加载集合。
自定义模式创建
python
from pymilvus import DataType
schema = client.createschema(autoid=False, enabledynamicfield=True)
schema.addfield(id, DataType.INT64, isprimary=True)
schema.addfield(text, DataType.VARCHAR, maxlength=512)
schema.addfield(embedding, DataType.FLOATVECTOR, dim=768)
indexparams = client.prepareindex_params()
indexparams.addindex(fieldname=embedding, indextype=AUTOINDEX, metric_type=COSINE)
client.createcollection(collectionname=mycollection, schema=schema, indexparams=index_params)
有关数据类型、addfield 参数和所有集合操作,请参阅 references/collection.md。
其他集合操作
python
client.list_collections()
client.describecollection(collectionname=my_collection)
client.hascollection(collectionname=my_collection)
client.renamecollection(oldname=old, new_name=new)
client.dropcollection(collectionname=my_collection)
client.truncatecollection(collectionname=my_collection)
client.loadcollection(collectionname=my_collection)
client.releasecollection(collectionname=my_collection)
client.getloadstate(collectionname=mycollection)
client.getcollectionstats(collectionname=mycollection)
- - 快速创建最适合原型开发;生产环境请使用自定义模式。
- 集合必须在搜索或查询之前加载。
- 使用 enabledynamicfield=True 允许插入模式中未定义的字段。
向量操作
有关混合搜索、全文搜索、迭代器、过滤语法和详细示例,请参阅 references/vector.md。
插入 / 更新
python
向量必须来自嵌入模型——切勿使用虚假/占位向量
from pymilvus import model
embeddingfn = model.dense.SentenceTransformerEmbeddingFunction(modelname=all-MiniLM-L6-v2)
docs = [2024年AI进展, 面向初学者的ML基础]
vectors = embeddingfn.encodedocuments(docs)
data = [
{id: 1, text: docs[0], embedding: vectors[0]},
{id: 2, text: docs[1], embedding: vectors[1]},
]
client.insert(collectionname=mycollection, data=data)
client.upsert(collectionname=mycollection, data=data)
搜索(向量相似度)
python
使用相同的嵌入模型对查询进行编码
query
vectors = embeddingfn.encode_queries([什么是人工智能?])
results = client.search(
collectionname=mycollection,
data=query_vectors,
anns_field=embedding,
limit=10,
output_fields=[text, id],
filter=age > 20 and status == active,
searchparams={metrictype: COSINE, params: {nprobe: 10}}
)
查询 / 获取 / 删除
python
按过滤条件查询
client.query(collection
name=mycollection, filter=id in [1, 2, 3], output_fields=[text], limit=100)
按主键获取
client.get(collection
name=mycollection, ids=[1, 2, 3], output_fields=[text])
删除
client.delete(collection
name=mycollection, ids=[1, 2, 3])
client.delete(collection
name=mycollection, filter=status == obsolete)
- - 切勿使用虚假或占位向量(例如 [0.1, 0.2, ...])。始终从嵌入模型生成向量。
- 使用 pip install pymilvus[model] 获取内置嵌入函数,或使用任何嵌入模型(OpenAI、Cohere 等)。
- 搜索中的向量维度必须与集合模式完全匹配。
- 查询嵌入模型必须与生成存储向量的模型相同。
- 对于大量插入,将数据分批处理