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+
pymilvus (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:
- 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.
from pymilvus import MilvusClient
# Milvus Lite (embedded, file-based — great for dev/test)
client = MilvusClient(uri="./milvus.db")
# Standalone / Cluster Milvus (ask user for actual host:port and credentials)
client = MilvusClient(uri="<USER_URI>", token="<USER_TOKEN>")
# Zilliz Cloud (ask user for endpoint and API key)
client = MilvusClient(uri="<USER_ZILLIZ_ENDPOINT>", token="<USER_API_KEY>")
Parameters:
| Parameter |
Type |
Description |
uri |
str |
"./file.db" for Milvus Lite, "http://host:19530" for server |
token |
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
from pymilvus import AsyncMilvusClient
async with AsyncMilvusClient(uri="<USER_URI>") as client:
results = await client.search(collection_name="my_collection", data=[query_vector], limit=10)
Collection Management
Quick Create (auto schema + auto index + auto load)
client.create_collection(
collection_name="my_collection",
dimension=768,
metric_type="COSINE" # Optional: "COSINE" (default), "L2", "IP"
)
This automatically creates an id field (INT64, primary key, auto_id), a vector field (FLOAT_VECTOR), AUTOINDEX, and auto-loads the collection.
Custom Schema Create
from pymilvus import DataType
schema = client.create_schema(auto_id=False, enable_dynamic_field=True)
schema.add_field("id", DataType.INT64, is_primary=True)
schema.add_field("text", DataType.VARCHAR, max_length=512)
schema.add_field("embedding", DataType.FLOAT_VECTOR, dim=768)
index_params = client.prepare_index_params()
index_params.add_index(field_name="embedding", index_type="AUTOINDEX", metric_type="COSINE")
client.create_collection(collection_name="my_collection", schema=schema, index_params=index_params)
See references/collection.md for data types, add_field parameters, and all collection operations.
Other Collection Operations
client.list_collections()
client.describe_collection(collection_name="my_collection")
client.has_collection(collection_name="my_collection")
client.rename_collection(old_name="old", new_name="new")
client.drop_collection(collection_name="my_collection")
client.truncate_collection(collection_name="my_collection")
client.load_collection(collection_name="my_collection")
client.release_collection(collection_name="my_collection")
client.get_load_state(collection_name="my_collection")
client.get_collection_stats(collection_name="my_collection")
- 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
# Vectors must come from an embedding model — never use fake/placeholder vectors
from pymilvus import model
embedding_fn = model.dense.SentenceTransformerEmbeddingFunction(model_name="all-MiniLM-L6-v2")
docs = ["AI advances in 2024", "ML basics for beginners"]
vectors = embedding_fn.encode_documents(docs)
data = [
{"id": 1, "text": docs[0], "embedding": vectors[0]},
{"id": 2, "text": docs[1], "embedding": vectors[1]},
]
client.insert(collection_name="my_collection", data=data)
client.upsert(collection_name="my_collection", data=data)
Search (vector similarity)
# Use the same embedding model to encode the query
query_vectors = embedding_fn.encode_queries(["What is artificial intelligence?"])
results = client.search(
collection_name="my_collection",
data=query_vectors,
anns_field="embedding",
limit=10,
output_fields=["text", "id"],
filter='age > 20 and status == "active"',
search_params={"metric_type": "COSINE", "params": {"nprobe": 10}}
)
Query / Get / Delete
# Query by filter
client.query(collection_name="my_collection", filter='id in [1, 2, 3]', output_fields=["text"], limit=100)
# Get by primary key
client.get(collection_name="my_collection", ids=[1, 2, 3], output_fields=["text"])
# Delete
client.delete(collection_name="my_collection", ids=[1, 2, 3])
client.delete(collection_name="my_collection", filter='status == "obsolete"')
- 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.
index_params = client.prepare_index_params()
index_params.add_index(
field_name="embedding",
index_type="HNSW",
metric_type="COSINE",
params={"M": 16, "efConstruction": 256}
)
client.create_index(collection_name="my_collection", index_params=index_params)
client.list_indexes(collection_name="my_collection")
client.describe_index(collection_name="my_collection", index_name="my_index")
client.drop_index(collection_name="my_collection", index_name="my_index")
AUTOINDEX is recommended for most use cases.
- An index is required before loading a collection.
Additional Features
| Feature |
Reference |
| Partition Management |
references/partition.md |
| Database Management |
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.
1---2name: milvus3description: Operate Milvus vector database with pymilvus Python SDK. Use when the user wants to connect to Milvus, create collections, insert vectors, perform similarity search, hybrid search, full-text search, manage indexes, partitions, databases, or RBAC via Python code.4license: Apache-2.05---67# Milvus Vector Database Skill89Operate [Milvus](https://milvus.io/) 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.1011## When to Use1213Use this skill when the user wants to:14- Connect to a Milvus instance (local, standalone, cluster, or Milvus Lite)15- Create collections with custom schemas16- Insert, upsert, search, query, get, or delete vectors17- Perform hybrid search with reranking18- Perform full-text search (BM25)19- Manage indexes, partitions, databases20- Set up users, roles, and access control (RBAC)21- Build RAG pipelines, semantic search, or recommendation systems with Milvus22- Iterate over large result sets with search/query iterators2324## Requirements2526- Python 3.8+27- `pymilvus` (`pip install pymilvus`)28- A running Milvus instance, or use Milvus Lite (embedded, file-based) for development2930## Capabilities Overview3132| Area | What You Can Do |33|------|----------------|34| **Connection** | Connect to Milvus Lite, Standalone, Cluster, or Zilliz Cloud |35| **Collections** | Create (quick or custom schema), list, describe, drop, rename, truncate, load, release |36| **Vectors** | Insert, upsert, search, hybrid search, query, get, delete |37| **Full-Text Search** | BM25-based keyword search with sparse vectors |38| **Iterators** | Paginated search and query over large datasets |39| **Indexes** | Create (AUTOINDEX, HNSW, IVF_FLAT, etc.), list, describe, drop |40| **Partitions** | Create, list, load, release, drop |41| **Databases** | Create, list, switch, drop |42| **RBAC** | Users, roles, privileges management |4344---4546## Connection4748> **IMPORTANT: Before writing any connection code, you MUST ask the user for their connection details.** Ask:49> 1. **Deployment type** — Milvus Lite (local file), Standalone/Cluster (self-hosted), or Zilliz Cloud (managed)?50> 2. **URI** — For self-hosted: host and port (e.g., `http://localhost:19530`). For Zilliz Cloud: the endpoint URL.51> 3. **Authentication** — Token, API key, or username/password if required.52> 4. **Database name** — If not using the default database.53>54> **Never assume or hardcode connection parameters.** Use Milvus Lite (`uri="./milvus.db"`) only if the user explicitly wants local/embedded mode for development.5556```python57from pymilvus import MilvusClient5859# Milvus Lite (embedded, file-based — great for dev/test)60client = MilvusClient(uri="./milvus.db")6162# Standalone / Cluster Milvus (ask user for actual host:port and credentials)63client = MilvusClient(uri="<USER_URI>", token="<USER_TOKEN>")6465# Zilliz Cloud (ask user for endpoint and API key)66client = MilvusClient(uri="<USER_ZILLIZ_ENDPOINT>", token="<USER_API_KEY>")67```6869**Parameters:**7071| Parameter | Type | Description |72|-----------|------|-------------|73| `uri` | str | `"./file.db"` for Milvus Lite, `"http://host:19530"` for server |74| `token` | str | API key or `"username:password"` |75| `user` | str | Username (alternative to token) |76| `password` | str | Password (alternative to token) |77| `db_name` | str | Target database (default: `"default"`) |78| `timeout` | float | Operation timeout in seconds |7980### Async Client8182```python83from pymilvus import AsyncMilvusClient8485async with AsyncMilvusClient(uri="<USER_URI>") as client:86 results = await client.search(collection_name="my_collection", data=[query_vector], limit=10)87```8889---9091## Collection Management9293### Quick Create (auto schema + auto index + auto load)9495```python96client.create_collection(97 collection_name="my_collection",98 dimension=768,99 metric_type="COSINE" # Optional: "COSINE" (default), "L2", "IP"100)101```102103This automatically creates an `id` field (INT64, primary key, auto_id), a `vector` field (FLOAT_VECTOR), AUTOINDEX, and auto-loads the collection.104105### Custom Schema Create106107```python108from pymilvus import DataType109110schema = client.create_schema(auto_id=False, enable_dynamic_field=True)111schema.add_field("id", DataType.INT64, is_primary=True)112schema.add_field("text", DataType.VARCHAR, max_length=512)113schema.add_field("embedding", DataType.FLOAT_VECTOR, dim=768)114115index_params = client.prepare_index_params()116index_params.add_index(field_name="embedding", index_type="AUTOINDEX", metric_type="COSINE")117118client.create_collection(collection_name="my_collection", schema=schema, index_params=index_params)119```120121**See [references/collection.md](references/collection.md) for data types, add_field parameters, and all collection operations.**122123### Other Collection Operations124125```python126client.list_collections()127client.describe_collection(collection_name="my_collection")128client.has_collection(collection_name="my_collection")129client.rename_collection(old_name="old", new_name="new")130client.drop_collection(collection_name="my_collection")131client.truncate_collection(collection_name="my_collection")132client.load_collection(collection_name="my_collection")133client.release_collection(collection_name="my_collection")134client.get_load_state(collection_name="my_collection")135client.get_collection_stats(collection_name="my_collection")136```137138- Quick create is best for prototyping; use custom schema for production.139- A collection must be **loaded** before search or query.140- Use `enable_dynamic_field=True` to allow inserting fields not defined in the schema.141142---143144## Vector Operations145146**See [references/vector.md](references/vector.md) for hybrid search, full-text search, iterators, filter syntax, and detailed examples.**147148### Insert / Upsert149150```python151# Vectors must come from an embedding model — never use fake/placeholder vectors152from pymilvus import model153154embedding_fn = model.dense.SentenceTransformerEmbeddingFunction(model_name="all-MiniLM-L6-v2")155156docs = ["AI advances in 2024", "ML basics for beginners"]157vectors = embedding_fn.encode_documents(docs)158159data = [160 {"id": 1, "text": docs[0], "embedding": vectors[0]},161 {"id": 2, "text": docs[1], "embedding": vectors[1]},162]163client.insert(collection_name="my_collection", data=data)164client.upsert(collection_name="my_collection", data=data)165```166167### Search (vector similarity)168169```python170# Use the same embedding model to encode the query171query_vectors = embedding_fn.encode_queries(["What is artificial intelligence?"])172173results = client.search(174 collection_name="my_collection",175 data=query_vectors,176 anns_field="embedding",177 limit=10,178 output_fields=["text", "id"],179 filter='age > 20 and status == "active"',180 search_params={"metric_type": "COSINE", "params": {"nprobe": 10}}181)182```183184### Query / Get / Delete185186```python187# Query by filter188client.query(collection_name="my_collection", filter='id in [1, 2, 3]', output_fields=["text"], limit=100)189190# Get by primary key191client.get(collection_name="my_collection", ids=[1, 2, 3], output_fields=["text"])192193# Delete194client.delete(collection_name="my_collection", ids=[1, 2, 3])195client.delete(collection_name="my_collection", filter='status == "obsolete"')196```197198- **Never use fake or placeholder vectors** (e.g., `[0.1, 0.2, ...]`). Always generate vectors from an embedding model.199- Use `pip install "pymilvus[model]"` for built-in embedding functions, or use any embedding model (OpenAI, Cohere, etc.).200- Vector dimension in search must match the collection schema exactly.201- The query embedding model must be the **same model** used to generate the stored vectors.202- For large inserts, batch data into chunks (e.g., 1000 rows per batch).203- For large result sets, use iterators — see [references/vector.md](references/vector.md).204205---206207## Index Management208209**See [references/index.md](references/index.md) for index types, metric types, and parameters.**210211```python212index_params = client.prepare_index_params()213index_params.add_index(214 field_name="embedding",215 index_type="HNSW",216 metric_type="COSINE",217 params={"M": 16, "efConstruction": 256}218)219client.create_index(collection_name="my_collection", index_params=index_params)220221client.list_indexes(collection_name="my_collection")222client.describe_index(collection_name="my_collection", index_name="my_index")223client.drop_index(collection_name="my_collection", index_name="my_index")224```225226- `AUTOINDEX` is recommended for most use cases.227- An index is required before loading a collection.228229---230231## Additional Features232233| Feature | Reference |234|---------|-----------|235| Partition Management | [references/partition.md](references/partition.md) |236| Database Management | [references/database.md](references/database.md) |237| User & Role Management (RBAC) | [references/user-role.md](references/user-role.md) |238| Common Patterns (RAG, Semantic Search) | [references/patterns.md](references/patterns.md) |239240---241242## General Guidance243244- **Always ask the user for connection details** (URI, token/credentials) before writing connection code. Never assume or hardcode connection parameters.245- **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.246- For quick prototyping, use **Milvus Lite** (`uri="./file.db"`) — no server needed, but only if the user explicitly requests local/embedded mode.247- A collection must be **loaded into memory** before search/query.248- The vector dimension in search data must **exactly match** the collection schema.249- The query embedding model must be the **same model** used to generate the stored vectors.250- Before any destructive operation (drop collection, drop database, delete vectors), always confirm with the user.251- Use `enable_dynamic_field=True` when the schema may evolve.252- Prefer `AUTOINDEX` unless the user has specific performance requirements.253- Use `truncate_collection` to clear all data without dropping the collection.254- For large datasets, use iterators (`search_iterator`, `query_iterator`) instead of increasing limit.