Chapter 3: Milvus, Qdrant, and Others
Milvus
High-performance open source vector database:
from pymilvus import connections, Collection, FieldSchema, DataType
# Connect
connections.connect(host="localhost", port="19530")
# Define schema
fields = [
FieldSchema(name="id", dtype=DataType.INT64, is_primary=True),
FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=1536),
FieldSchema(name="text", dtype=DataType.VARCHAR, max_length=65535)
]
# Create collection
collection = Collection(name="documents", schema=CollectionSchema(fields))
# Insert
collection.insert([[1], [embedding], ["text..."]])
# Create index
collection.create_index("embedding", {"index_type": "HNSW", "metric_type": "COSINE"})
# Search
collection.search(
data=[query_embedding],
anns_field="embedding",
param={"metric_type": "COSINE", "params": {"ef": 64}},
limit=5
)
Best for: Large-scale, high-performance needs
Qdrant
Modern, Rust-based vector database:
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct
# Connect
client = QdrantClient(host="localhost", port=6333)
# Create collection
client.create_collection(
collection_name="documents",
vectors_config=VectorParams(size=1536, distance=Distance.COSINE)
)
# Insert
client.upsert(
collection_name="documents",
points=[
PointStruct(id=1, vector=embedding, payload={"source": "doc.pdf"})
]
)
# Search
results = client.search(
collection_name="documents",
query_vector=query_embedding,
limit=5,
query_filter={"must": [{"key": "source", "match": {"value": "doc.pdf"}}]}
)
Best for: Production deployments, excellent filtering
PostgreSQL + pgvector
Use your existing PostgreSQL:
-- Enable extension
CREATE EXTENSION vector;
-- Create table
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
content TEXT,
embedding vector(1536),
metadata JSONB
);
-- Create index
CREATE INDEX ON documents USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100);
-- Search
SELECT id, content, 1 - (embedding <=> query_embedding) as similarity
FROM documents
ORDER BY embedding <=> query_embedding
LIMIT 5;
Best for: Teams already using PostgreSQL
FAISS (In-Memory)
Facebook's similarity search library:
import faiss
import numpy as np
# Create index
dimension = 1536
index = faiss.IndexFlatIP(dimension) # Inner product
# Add vectors
vectors = np.array(embeddings).astype('float32')
faiss.normalize_L2(vectors)
index.add(vectors)
# Search
query = np.array([query_embedding]).astype('float32')
faiss.normalize_L2(query)
distances, indices = index.search(query, k=5)
Best for: High-speed in-memory search, research
Comparison Summary
| Database | Hosting | Performance | Ease of Use | Production Ready |
|---|---|---|---|---|
| Milvus | Self-host | Excellent | Medium | Yes |
| Qdrant | Both | Excellent | Easy | Yes |
| pgvector | Self-host | Good | Easy | Yes |
| FAISS | In-memory | Excellent | Medium | Limited |
Decision Guide
Next Steps
Test your knowledge with the Module 4 Quiz!