Milvus 向量数据库
Milvus 是面向大规模向量检索的分布式数据库,支持 IVF、HNSW、DiskANN 等索引,依赖 etcd、对象存储和消息队列。适合亿级向量的语义搜索和推荐。
Docker Compose 启动单机:
wget https://github.com/milvus-io/milvus/releases/download/v2.4.15/milvus-standalone-docker-compose.yml -O docker-compose.yml
docker compose up -d
使用 Python SDK 创建集合:
from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection
connections.connect(host="127.0.0.1", port="19530")
fields = [
FieldSchema(name="id", dtype=DataType.INT64, is_primary=True),
FieldSchema(name="vector", dtype=DataType.FLOAT_VECTOR, dim=1536),
FieldSchema(name="source", dtype=DataType.VARCHAR, max_length=128),
]
collection = Collection("docs", CollectionSchema(fields))
collection.create_index("vector", {"index_type": "HNSW", "metric_type": "COSINE", "params": {"M": 16, "efConstruction": 200}})
插入和搜索:
collection.insert([
[1, [0.1, 0.2], "faq"],
])
collection.load()
collection.search([[0.1, 0.2]], "vector", {"metric_type": "COSINE"}, limit=5)
查看集合状态:
from pymilvus import utility
utility.list_collections()
collection.num_entities
load(),搜索报 collection not loaded。nprobe/ef 设置过低,召回率下降。合上页面,先回答,再展开对照。
collection.load()。