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.
Use this skill when the user wants to:
pymilvus ( pip install pymilvus)AreaWhat You Can DoConnectionConnect to Milvus Lite, Standalone, Cluster, or Zilliz CloudCollectionsCreate (quick or custom schema), list, describe, drop, rename, truncate, load, releaseVectorsInsert, upsert, search, hybrid search, query, get, deleteFull-Text SearchBM25-based keyword search with sparse vectorsIteratorsPaginated search and query over large datasetsIndexesCreate (AUTOINDEX, HNSW, IVF_FLAT, etc.), list, describe, dropPartitionsCreate, list, load, release, dropDatabasesCreate, list, switch, dropRBACUsers, roles, privileges management
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.
python
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:
ParameterTypeDescriptionuristr"./file.db" for Milvus Lite, "http://host:19530" for servertokenstrAPI key or "username:password"``userstrUsername (alternative to token)passwordstrPassword (alternative to token)db_namestrTarget database (default: "default")timeoutfloatOperation timeout in seconds
python
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)
python
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.
python
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.
python
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")
enable_dynamic_field=True to allow inserting fields not defined in the schema.See references/vector.md for hybrid search, full-text search, iterators, filter syntax, and detailed examples.
python
# 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)
python
# 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}}
)
python
# 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"')
[0.1, 0.2, ...]). Always generate vectors from an embedding model.pip install "pymilvus[model]" for built-in embedding functions, or use any embedding model (OpenAI, Cohere, etc.).See references/index.md for index types, metric types, and parameters.
python
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.FeatureReferencePartition Managementreferences/partition.mdDatabase Managementreferences/database.mdUser & Role Management (RBAC)references/user-role.mdCommon Patterns (RAG, Semantic Search)references/patterns.md
pip install "pymilvus[model]" for built-in embedding functions.uri="./file.db") — no server needed, but only if the user explicitly requests local/embedded mode.enable_dynamic_field=True when the schema may evolve.AUTOINDEX unless the user has specific performance requirements.truncate_collection to clear all data without dropping the collection.search_iterator, query_iterator) instead of increasing limit.