Skip to content

title: Milvus description: High-performance vector database — Milvus for RAG: collections, indexing algorithms, partitioning, GPU acceleration, and Zilliz Cloud managed service. category: 09-RAG tags: [milvus, vector-store, high-performance, gpu, zilliz, rag] status: ready last_updated: 2026-09-06


Milvus

Milvus is a high-performance vector database built for scale. It supports multiple indexing algorithms (IVF, HNSW, ANNOY), GPU acceleration, and handles billions of vectors. Use Milvus when you need maximum performance at scale — or use Zilliz Cloud for a managed version.

Table of Contents

Installation / Setup

pip install pymilvus                    # or: uv add pymilvus
# docker: docker compose up -d (for local Milvus)
# cloud: https://zilliz.com/cloud/

Core Syntax

Client setup

from pymilvus import MilvusClient

# local (Milvus Lite — in-process)
client = MilvusClient("milvus.db")

# local (Milvus server)
client = MilvusClient(uri="http://localhost:19530")

# Zilliz Cloud
client = MilvusClient(uri="https://xxx.zillizcloud.com:19530", token="YOUR_TOKEN")

Create collection

from pymilvus import DataType

schema = client.create_schema(auto_id=True, enable_dynamic_field=True)
schema.add_field("id", DataType.INT64, is_primary=True)
schema.add_field("vector", DataType.FLOAT_VECTOR, dim=1536)
schema.add_field("text", DataType.VARCHAR, max_length=2000)
schema.add_field("source", DataType.VARCHAR, max_length=200)

client.create_collection(
    collection_name="documents",
    schema=schema,
)

Insert data

client.insert(collection_name="documents", data=[
    {"vector": [0.1]*1536, "text": "Python is great for ML", "source": "tutorial"},
    {"vector": [0.2]*1536, "text": "JavaScript runs in browsers", "source": "web"},
])
results = client.search(
    collection_name="search",
    data=[[0.1]*1536],     # query vectors (supports batch)
    limit=5,
    output_fields=["text", "source"],
)
for hits in results:
    for hit in hits:
        print(f"Score: {hit['distance']:.3f}, Text: {hit['entity']['text']}")

LangChain + Milvus

from langchain_community.vectorstores import Milvus
from langchain_openai import OpenAIEmbeddings

vectorstore = Milvus.from_documents(
    chunks, OpenAIEmbeddings(),
    connection_args={"host": "localhost", "port": "19530"},
    collection_name="documents",
)

docs = vectorstore.similarity_search("query", k=5)

Common Operations

Index creation

from pymilvus import CollectionSchema

# HNSW index (fast, good quality)
client.create_index(
    collection_name="documents",
    field_name="vector",
    index_params={
        "index_type": "HNSW",
        "metric_type": "COSINE",
        "params": {"M": 16, "efConstruction": 256},
    },
)

# IVF_FLAT index (good for large datasets)
client.create_index(
    collection_name="documents",
    field_name="vector",
    index_params={
        "index_type": "IVF_FLAT",
        "metric_type": "COSINE",
        "params": {"nlist": 128},
    },
)

Search with filtering

results = client.search(
    collection_name="documents",
    data=[[0.1]*1536],
    limit=5,
    filter='source == "report" and year > 2024',
    output_fields=["text", "source", "year"],
)

Collection management

# list collections
collections = client.list_collections()

# get collection info
info = client.describe_collection("documents")

# delete collection
client.drop_collection("documents")

Partitioning (for large datasets)

# create partition
client.create_partition("documents", partition_name="2024")

# insert into partition
client.insert("documents", data=[...], partition_name="2024")

# search in partition
results = client.search("documents", data=[[0.1]*1536], partition_names=["2024"])

Practical Examples

Example 1 — Milvus RAG pipeline

from pymilvus import MilvusClient
from langchain_community.vectorstores import Milvus
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough

client = MilvusClient(uri="http://localhost:19530")
vectorstore = Milvus(client, OpenAIEmbeddings(), collection_name="documents")
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})

rag = (
    {"context": retriever | (lambda d: "\n\n".join(x.page_content for x in d)),
     "question": RunnablePassthrough()}
    | ChatPromptTemplate.from_template("Answer:\n{context}\n\nQ: {question}")
    | ChatOpenAI(model="gpt-4o")
    | StrOutputParser()
)

Common Patterns

Pattern: index selection

Dataset size Recommended index Why
<100K HNSW Fast, high quality
100K–10M IVF_FLAT Good balance
>10M IVF_PQ Memory efficient
GPU available GPU_IVF_FLAT Fastest for large scale

Best Practices

  • Use Milvus Lite (milvus.db) for local development.
  • Use Zilliz Cloud for production — fully managed.
  • Choose index based on dataset size — HNSW for small, IVF for large.
  • Use partitions to organize large datasets by date/category.
  • Milvus supports GPU acceleration — use it for very large scale.

Common Mistakes

Mistake Fix
Using Milvus server for local dev Use Milvus Lite (milvus.db)
Not creating an index Always create an index before search
Wrong metric type Match to embedding model: COSINE for normalized

Troubleshooting

Symptom Cause / Fix
Connection refused Milvus server not running
Collection not found Create collection or check name
Slow queries Create proper index; check dataset size
OOM Use IVF_PQ for compression; increase memory

Official Documentation

Last Updated

2026-09-06 — Initial version.