Milvus Vector Database Skill
Operate Milvus vector databases directly through Python code using the pymilvus SDK. This skill covers the full lifecycle — connecting, schema design, collection management, vector CRUD, search, hybrid 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
- Manage indexes, partitions, databases
- Set up users, roles, and access control (RBAC)
- Build RAG pipelines, semantic search, or recommendation systems with Milvus
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, load, release |
|
Vectors | Insert, upsert, search, hybrid search, query, get, delete |
|
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
CODEBLOCK0
Parameters:
| Parameter | Type | Description |
|---|
| INLINECODE3 | str | INLINECODE4 for Milvus Lite, "http://host:19530" for server |
| INLINECODE6 |
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:
- -
id field (INT64, primary key, autoid) - INLINECODE14 field (FLOATVECTOR, dim=dimension)
- AUTOINDEX on vector field
- Collection is auto-loaded
Custom Schema Create
CODEBLOCK3
Supported Data Types
Scalar types:
| DataType | Notes |
|---|
| INLINECODE15 | Boolean |
INLINECODE16 / INT16 / INT32 / INLINECODE19 |
Integers |
|
DataType.FLOAT /
DOUBLE | Floating point |
|
DataType.VARCHAR | String (requires
max_length) |
|
DataType.JSON | JSON object |
|
DataType.ARRAY | Array (requires
element_type,
max_capacity) |
Vector types:
| DataType | Notes |
|---|
| INLINECODE28 | Float32 vector (requires dim) |
| INLINECODE30 |
Float16 vector (requires
dim) |
|
DataType.BFLOAT16_VECTOR | BFloat16 vector (requires
dim) |
|
DataType.BINARY_VECTOR | Binary vector (requires
dim) |
|
DataType.SPARSE_FLOAT_VECTOR | Sparse vector (no
dim needed) |
add_field Parameters
CODEBLOCK4
Other Collection Operations
CODEBLOCK5
Collection Guidance
- - Quick create is best for prototyping; use custom schema for production.
- A collection must be loaded before search or query operations.
- Before dropping a collection, confirm with the user — this deletes all data.
- Use
enable_dynamic_field=True to allow inserting fields not defined in the schema.
Vector Operations
Target collection must exist and be loaded.
Insert
CODEBLOCK6
Upsert (insert or update if PK exists)
CODEBLOCK7
Search (vector similarity)
CODEBLOCK8
Hybrid Search (multi-vector with reranking)
CODEBLOCK9
Query (filter-based retrieval)
CODEBLOCK10
Get (by primary key)
CODEBLOCK11
Delete
CODEBLOCK12
Filter Expression Syntax
| Expression | Example |
|---|
| Comparison | INLINECODE39 |
| Equality |
status == "active" |
| IN list |
id in [1, 2, 3] |
| AND/OR |
age > 20 and status == "active" |
| String match |
text like "hello%" |
| Array contains |
ARRAY_CONTAINS(tags, "ml") |
| JSON field |
json_field["key"] > 100 |
| Match all |
id > 0 |
Vector Guidance
- - The
data parameter in search must match the collection's vector dimension exactly. - For text-to-vector search, convert text to vectors using an embedding model first.
- For large inserts, batch data into chunks (e.g., 1000 rows per batch).
- Always specify
output_fields to control which fields are returned.
Index Management
Create Index
CODEBLOCK13
Common Index Types
| Index Type | For | Key Params | Notes |
|---|
| INLINECODE49 | Dense vectors | Auto-tuned | Recommended for most cases |
| INLINECODE50 |
Dense vectors | None | Brute force, 100% recall |
|
IVF_FLAT | Dense vectors |
nlist | Good balance |
|
IVF_SQ8 | Dense vectors |
nlist | Compressed, less memory |
|
HNSW | Dense vectors |
M,
efConstruction | High recall, more memory |
|
DISKANN | Dense vectors | None | Disk-based, large datasets |
|
SPARSE_INVERTED_INDEX | Sparse vectors |
drop_ratio_build | For sparse vectors |
|
SPARSE_WAND | Sparse vectors |
drop_ratio_build | Faster sparse search |
Metric Types
| Metric | Description | Use With |
|---|
| INLINECODE63 | Cosine similarity (larger = more similar) | Dense vectors |
| INLINECODE64 |
Euclidean distance (smaller = more similar) | Dense vectors |
|
"IP" | Inner product (larger = more similar) | Dense & Sparse vectors |
Other Index Operations
CODEBLOCK14
Index Guidance
- -
AUTOINDEX is recommended for most use cases. - An index is required before loading a collection.
- After creating an index, load the collection before searching.
- Sparse vectors only support
"IP" metric type.
Partition Management
CODEBLOCK15
Partition Guidance
- - Every collection has a
_default partition. - Use
is_partition_key=True on a field to enable automatic partitioning by field value. - A partition must be loaded before search.
- Before dropping a partition, confirm with the user — all data in it will be deleted.
Database Management
CODEBLOCK16
Database Guidance
- - Every Milvus instance has a
"default" database. - Before dropping a database, all collections in it must be dropped first.
User & Role Management (RBAC)
User Operations
CODEBLOCK17
Role Operations
CODEBLOCK18
Built-in Privilege Groups
| Group | Scope |
|---|
| INLINECODE71 | Full cluster access |
| INLINECODE72 |
Read-only cluster access |
|
ClusterReadWrite | Read-write cluster access |
|
DatabaseAdmin | Full database access |
|
DatabaseReadOnly | Read-only database access |
|
DatabaseReadWrite | Read-write database access |
|
CollectionAdmin | Full collection access |
|
CollectionReadOnly | Read-only collection access |
|
CollectionReadWrite | Read-write collection access |
Common Individual Privileges
INLINECODE80 , Query, Insert, Delete, Upsert, CreateIndex, DropIndex, CreateCollection, DropCollection, Load, Release, CreatePartition, INLINECODE92
RBAC Guidance
- - Recommended workflow: create role → grant privileges → create user → assign role.
- Use
"*" for collectionname/dbname to grant on all resources. - Before dropping a user or role, confirm with the user.
Common Patterns
RAG Pipeline Pattern
CODEBLOCK19
Quick Semantic Search Pattern
CODEBLOCK20
General Guidance
- - Always check if pymilvus is installed:
pip install pymilvus. - For quick prototyping, use Milvus Lite (
uri="./file.db") — no server needed. - A collection must be loaded into memory before search/query.
- The vector dimension in search data must exactly match the collection schema.
- For text queries, users need an embedding model to convert text to vectors first. Suggest
pymilvus[model] for built-in embedding support. - 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. - For large-scale inserts, batch data into chunks of ~1000 rows.
- Prefer
AUTOINDEX unless the user has specific performance requirements.
Milvus 向量数据库技能
通过使用 pymilvus SDK 的 Python 代码直接操作 Milvus 向量数据库。本技能涵盖完整生命周期——连接、模式设计、集合管理、向量增删改查、搜索、混合搜索、索引、分区、数据库和 RBAC。
使用时机
当用户想要以下操作时使用本技能:
- - 连接到 Milvus 实例(本地、单机、集群或 Milvus Lite)
- 使用自定义模式创建集合
- 插入、更新插入、搜索、查询、获取或删除向量
- 执行带重排序的混合搜索
- 管理索引、分区、数据库
- 设置用户、角色和访问控制(RBAC)
- 使用 Milvus 构建 RAG 流水线、语义搜索或推荐系统
要求
- - Python 3.8+
- pymilvus(pip install pymilvus)
- 运行中的 Milvus 实例,或使用 Milvus Lite(嵌入式、基于文件)进行开发
能力概览
| 领域 | 可执行操作 |
|---|
| 连接 | 连接到 Milvus Lite、单机、集群或 Zilliz Cloud |
| 集合 |
创建(快速或自定义模式)、列出、描述、删除、重命名、加载、释放 |
|
向量 | 插入、更新插入、搜索、混合搜索、查询、获取、删除 |
|
索引 | 创建(AUTOINDEX、HNSW、IVF_FLAT 等)、列出、描述、删除 |
|
分区 | 创建、列出、加载、释放、删除 |
|
数据库 | 创建、列出、切换、删除 |
|
RBAC | 用户、角色、权限管理 |
连接
python
from pymilvus import MilvusClient
Milvus Lite(嵌入式、基于文件——非常适合开发/测试)
client = MilvusClient(uri=./milvus_demo.db)
单机 / 集群 Milvus
client = MilvusClient(uri=http://localhost:19530, token=root:Milvus)
Zilliz Cloud
client = MilvusClient(
uri=https://in03-xxxx.api.gcp-us-west1.zillizcloud.com:19530,
token=your
apikey
)
参数:
| 参数 | 类型 | 描述 |
|---|
| uri | str | Milvus Lite 使用 ./file.db,服务器使用 http://host:19530 |
| token |
str | API 密钥或 username:password |
| user | str | 用户名(token 的替代方案) |
| password | str | 密码(token 的替代方案) |
| db_name | str | 目标数据库(默认:default) |
| timeout | float | 操作超时时间(秒) |
异步客户端
python
from pymilvus import AsyncMilvusClient
async with AsyncMilvusClient(uri=http://localhost:19530) as client:
results = await client.search(...)
集合管理
快速创建(自动模式 + 自动索引 + 自动加载)
python
client.create_collection(
collectionname=mycollection,
dimension=768,
metric_type=COSINE # 可选:COSINE(默认)、L2、IP
)
这将自动创建:
- - id 字段(INT64,主键,自动 ID)
- vector 字段(FLOAT_VECTOR,维度=dimension)
- 向量字段上的 AUTOINDEX
- 集合自动加载
自定义模式创建
python
from pymilvus import DataType
步骤 1:定义模式
schema = client.create
schema(autoid=False, enable
dynamicfield=True)
schema.add
field(id, DataType.INT64, isprimary=True)
schema.add
field(text, DataType.VARCHAR, maxlength=512)
schema.add
field(embedding, DataType.FLOATVECTOR, dim=768)
步骤 2:定义索引
index
params = client.prepareindex_params()
index
params.addindex(
field_name=embedding,
index_type=AUTOINDEX,
metric_type=COSINE
)
步骤 3:创建集合
client.create_collection(
collection
name=mycollection,
schema=schema,
index
params=indexparams
)
支持的数据类型
标量类型:
| DataType | 说明 |
|---|
| DataType.BOOL | 布尔值 |
| DataType.INT8 / INT16 / INT32 / INT64 |
整数 |
| DataType.FLOAT / DOUBLE | 浮点数 |
| DataType.VARCHAR | 字符串(需要 max_length) |
| DataType.JSON | JSON 对象 |
| DataType.ARRAY | 数组(需要 element
type、maxcapacity) |
向量类型:
| DataType | 说明 |
|---|
| DataType.FLOATVECTOR | Float32 向量(需要 dim) |
| DataType.FLOAT16VECTOR |
Float16 向量(需要 dim) |
| DataType.BFLOAT16_VECTOR | BFloat16 向量(需要 dim) |
| DataType.BINARY_VECTOR | 二进制向量(需要 dim) |
| DataType.SPARSE
FLOATVECTOR | 稀疏向量(不需要 dim) |
add_field 参数
python
schema.add_field(
fieldname=myfield,
datatype=DataType.VARCHAR,
is_primary=False,
auto_id=False,
max_length=256, # VARCHAR 必需
dim=768, # 向量类型必需(稀疏向量除外)
element_type=DataType.INT64, # ARRAY 必需
max_capacity=100, # ARRAY 必需
nullable=False,
default_value=None,
ispartitionkey=False,
description=
)
其他集合操作
python
列出所有集合
collections = client.list_collections()
描述集合
info = client.describe
collection(collectionname=my_collection)
检查集合是否存在
exists = client.has
collection(collectionname=my_collection)
重命名集合
client.rename
collection(oldname=old
name, newname=new_name)
删除集合
client.drop
collection(collectionname=my_collection)
将集合加载到内存(搜索/查询前必需)
client.load
collection(collectionname=my_collection)
从内存释放集合
client.release
collection(collectionname=my_collection)
获取加载状态
state = client.get
loadstate(collection
name=mycollection)
获取集合统计信息
stats = client.get
collectionstats(collection
name=mycollection)
集合指导
- - 快速创建最适合原型开发;生产环境使用自定义模式。
- 集合必须在搜索或查询操作前加载。
- 删除集合前,请与用户确认——这将删除所有数据。
- 使用 enabledynamicfield=True 允许插入模式中未定义的字段。
向量操作
目标集合必须存在且已加载。
插入
python
data = [
{id: 1, text: AI advances, embedding: [0.1, 0.2, ...]},
{id: 2, text: ML basics, embedding: [0.3, 0.4, ...]},
]
res = client.insert(collectionname=mycollection, data=data)
返回:{insert_count: 2, ids: [1, 2]}
更新插入(如果主键存在则插入或更新)
python
res = client.upsert(collectionname=mycollection, data=data)
返回:{upsert_count: 2}
搜索(向量相似度)
python
results = client.search(
collectionname=mycollection,
data=[[0.1, 0.2, ...]], # 查询向量列表
anns_field=embedding, # 向量字段名称
limit=10, # Top-K
output_fields=[text, id], # 要返回的字段
filter=age > 20 and status == active, # 可选的标量过滤器
search_params={
metric_type: COSINE,
params: {nprobe: 10} # 索引特定参数
}
)
返回:List[List[dict]]
每个命中结果:{id: ..., distance: