项目文件夹

文件
2026-07-13 21:37:11 +08:00

13 KiB

name, description, version, author, license, dependencies, platforms, metadata
name description version author license dependencies platforms metadata
qdrant-vector-search 高性能向量相似度搜索引擎,适用于 RAG 和语义搜索。当需要构建生产级 RAG 系统,要求快速最近邻搜索、带过滤条件的混合搜索,或需要可扩展的 Rust 高性能向量存储时使用。 1.0.0 Orchestra Research MIT
qdrant-client>=1.12.0
linux
macos
windows
hermes
tags
RAG
Vector Search
Qdrant
Semantic Search
Embeddings
Similarity Search
HNSW
Production
Distributed

Qdrant - 向量相似度搜索引擎

基于 Rust 编写的高性能向量数据库,适用于生产级 RAG 和语义搜索。

何时使用 Qdrant

使用 Qdrant 的场景:

  • 构建需要低延迟的生产级 RAG 系统
  • 需要混合搜索(向量 + 元数据过滤)
  • 需要支持分片/复制的水平扩展
  • 希望本地部署并完全掌控数据
  • 每条记录需要多向量存储(稠密 + 稀疏)
  • 构建实时推荐系统

主要特性:

  • Rust 驱动:内存安全,高性能
  • 丰富过滤:搜索时可按任意 payload 字段过滤
  • 多向量支持:每个点支持稠密、稀疏、多稠密向量
  • 量化:标量、乘积、二进制量化,节省内存
  • 分布式Raft 共识、分片、复制
  • REST + gRPC:两种 API 功能完全对等

备选方案:

  • Chroma:安装更简单,适用于嵌入式场景
  • FAISS:追求最高原始速度,适用于研究/批量处理
  • Pinecone:全托管,适合零运维偏好
  • Weaviate:偏好 GraphQL,内置向量化器

快速开始

安装

# Python 客户端
pip install qdrant-client

# Docker(推荐用于开发)
docker run -p 6333:6333 -p 6334:6334 qdrant/qdrant

# Docker 持久化存储
docker run -p 6333:6333 -p 6334:6334 \
    -v $(pwd)/qdrant_storage:/qdrant/storage \
    qdrant/qdrant

基本用法

from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct

# 连接 Qdrant
client = QdrantClient(host="localhost", port=6333)

# 创建集合
client.create_collection(
    collection_name="documents",
    vectors_config=VectorParams(size=384, distance=Distance.COSINE)
)

# 插入向量及 payload
client.upsert(
    collection_name="documents",
    points=[
        PointStruct(
            id=1,
            vector=[0.1, 0.2, ...],  # 384 维向量
            payload={"title": "Doc 1", "category": "tech"}
        ),
        PointStruct(
            id=2,
            vector=[0.3, 0.4, ...],
            payload={"title": "Doc 2", "category": "science"}
        )
    ]
)

# 带过滤条件的搜索
results = client.search(
    collection_name="documents",
    query_vector=[0.15, 0.25, ...],
    query_filter={
        "must": [{"key": "category", "match": {"value": "tech"}}]
    },
    limit=10
)

for point in results:
    print(f"ID: {point.id}, Score: {point.score}, Payload: {point.payload}")

核心概念

Points - 基本数据单元

from qdrant_client.models import PointStruct

# Point = ID + 向量 + Payload
point = PointStruct(
    id=123,                              # 整数或 UUID 字符串
    vector=[0.1, 0.2, 0.3, ...],        # 稠密向量
    payload={                            # 任意 JSON 元数据
        "title": "Document title",
        "category": "tech",
        "timestamp": 1699900000,
        "tags": ["python", "ml"]
    }
)

# 批量 upsert(推荐)
client.upsert(
    collection_name="documents",
    points=[point1, point2, point3],
    wait=True  # 等待索引完成
)

Collections - 向量容器

from qdrant_client.models import VectorParams, Distance, HnswConfigDiff

# 创建集合并配置 HNSW
client.create_collection(
    collection_name="documents",
    vectors_config=VectorParams(
        size=384,                        # 向量维度
        distance=Distance.COSINE         # COSINE, EUCLID, DOT, MANHATTAN
    ),
    hnsw_config=HnswConfigDiff(
        m=16,                            # 每节点连接数(默认 16
        ef_construct=100,                # 构建时精度(默认 100
        full_scan_threshold=10000        # 低于此值切换为暴力搜索
    ),
    on_disk_payload=True                 # payload 存储到磁盘
)

# 查看集合信息
info = client.get_collection("documents")
print(f"点数: {info.points_count}, 向量数: {info.vectors_count}")

距离度量

度量方式 适用场景 范围
COSINE 文本嵌入、归一化向量 0 到 2
EUCLID 空间数据、图像特征 0 到 ∞
DOT 推荐系统、非归一化向量 -∞ 到 ∞
MANHATTAN 稀疏特征、离散数据 0 到 ∞

搜索操作

基本搜索

# 简单最近邻搜索
results = client.search(
    collection_name="documents",
    query_vector=[0.1, 0.2, ...],
    limit=10,
    with_payload=True,
    with_vectors=False  # 不返回向量(更快)
)

带过滤条件的搜索

from qdrant_client.models import Filter, FieldCondition, MatchValue, Range

# 复杂过滤
results = client.search(
    collection_name="documents",
    query_vector=query_embedding,
    query_filter=Filter(
        must=[
            FieldCondition(key="category", match=MatchValue(value="tech")),
            FieldCondition(key="timestamp", range=Range(gte=1699000000))
        ],
        must_not=[
            FieldCondition(key="status", match=MatchValue(value="archived"))
        ]
    ),
    limit=10
)

# 简化过滤语法
results = client.search(
    collection_name="documents",
    query_vector=query_embedding,
    query_filter={
        "must": [
            {"key": "category", "match": {"value": "tech"}},
            {"key": "price", "range": {"gte": 10, "lte": 100}}
        ]
    },
    limit=10
)

批量搜索

from qdrant_client.models import SearchRequest

# 一次请求执行多个查询
results = client.search_batch(
    collection_name="documents",
    requests=[
        SearchRequest(vector=[0.1, ...], limit=5),
        SearchRequest(vector=[0.2, ...], limit=5, filter={"must": [...]}),
        SearchRequest(vector=[0.3, ...], limit=10)
    ]
)

RAG 集成

配合 sentence-transformers

from sentence_transformers import SentenceTransformer
from qdrant_client import QdrantClient
from qdrant_client.models import VectorParams, Distance, PointStruct

# 初始化
encoder = SentenceTransformer("all-MiniLM-L6-v2")
client = QdrantClient(host="localhost", port=6333)

# 创建集合
client.create_collection(
    collection_name="knowledge_base",
    vectors_config=VectorParams(size=384, distance=Distance.COSINE)
)

# 索引文档
documents = [
    {"id": 1, "text": "Python is a programming language", "source": "wiki"},
    {"id": 2, "text": "Machine learning uses algorithms", "source": "textbook"},
]

points = [
    PointStruct(
        id=doc["id"],
        vector=encoder.encode(doc["text"]).tolist(),
        payload={"text": doc["text"], "source": doc["source"]}
    )
    for doc in documents
]
client.upsert(collection_name="knowledge_base", points=points)

# RAG 检索
def retrieve(query: str, top_k: int = 5) -> list[dict]:
    query_vector = encoder.encode(query).tolist()
    results = client.search(
        collection_name="knowledge_base",
        query_vector=query_vector,
        limit=top_k
    )
    return [{"text": r.payload["text"], "score": r.score} for r in results]

# 在 RAG 流水线中使用
context = retrieve("What is Python?")
prompt = f"Context: {context}\n\nQuestion: What is Python?"

配合 LangChain

from langchain_community.vectorstores import Qdrant
from langchain_community.embeddings import HuggingFaceEmbeddings

embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = Qdrant.from_documents(documents, embeddings, url="http://localhost:6333", collection_name="docs")
retriever = vectorstore.as_retriever(search_kwargs={"k": 5})

配合 LlamaIndex

from llama_index.vector_stores.qdrant import QdrantVectorStore
from llama_index.core import VectorStoreIndex, StorageContext

vector_store = QdrantVectorStore(client=client, collection_name="llama_docs")
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(documents, storage_context=storage_context)
query_engine = index.as_query_engine()

多向量支持

命名向量(不同嵌入模型)

from qdrant_client.models import VectorParams, Distance

# 包含多种向量类型的集合
client.create_collection(
    collection_name="hybrid_search",
    vectors_config={
        "dense": VectorParams(size=384, distance=Distance.COSINE),
        "sparse": VectorParams(size=30000, distance=Distance.DOT)
    }
)

# 使用命名向量插入
client.upsert(
    collection_name="hybrid_search",
    points=[
        PointStruct(
            id=1,
            vector={
                "dense": dense_embedding,
                "sparse": sparse_embedding
            },
            payload={"text": "document text"}
        )
    ]
)

# 搜索指定向量
results = client.search(
    collection_name="hybrid_search",
    query_vector=("dense", query_dense),  # 指定向量名称
    limit=10
)

稀疏向量(BM25, SPLADE

from qdrant_client.models import SparseVectorParams, SparseIndexParams, SparseVector

# 创建支持稀疏向量的集合
client.create_collection(
    collection_name="sparse_search",
    vectors_config={},
    sparse_vectors_config={"text": SparseVectorParams(index=SparseIndexParams(on_disk=False))}
)

# 插入稀疏向量
client.upsert(
    collection_name="sparse_search",
    points=[PointStruct(id=1, vector={"text": SparseVector(indices=[1, 5, 100], values=[0.5, 0.8, 0.2])}, payload={"text": "document"})]
)

量化(内存优化)

from qdrant_client.models import ScalarQuantization, ScalarQuantizationConfig, ScalarType

# 标量量化(内存减少 4 倍)
client.create_collection(
    collection_name="quantized",
    vectors_config=VectorParams(size=384, distance=Distance.COSINE),
    quantization_config=ScalarQuantization(
        scalar=ScalarQuantizationConfig(
            type=ScalarType.INT8,
            quantile=0.99,        # 裁剪异常值
            always_ram=True      # 量化数据常驻 RAM
        )
    )
)

# 带重评分的搜索
results = client.search(
    collection_name="quantized",
    query_vector=query,
    search_params={"quantization": {"rescore": True}},  # 对顶部结果重新评分
    limit=10
)

Payload 索引

from qdrant_client.models import PayloadSchemaType

# 创建 payload 索引以加速过滤
client.create_payload_index(
    collection_name="documents",
    field_name="category",
    field_schema=PayloadSchemaType.KEYWORD
)

client.create_payload_index(
    collection_name="documents",
    field_name="timestamp",
    field_schema=PayloadSchemaType.INTEGER
)

# 索引类型:KEYWORD, INTEGER, FLOAT, GEO, TEXT(全文搜索), BOOL

生产部署

Qdrant Cloud

from qdrant_client import QdrantClient

# 连接到 Qdrant Cloud
client = QdrantClient(
    url="https://your-cluster.cloud.qdrant.io",
    api_key="your-api-key"
)

性能调优

# 优化搜索速度(更高召回率)
client.update_collection(
    collection_name="documents",
    hnsw_config=HnswConfigDiff(ef_construct=200, m=32)
)

# 优化索引速度(批量加载)
client.update_collection(
    collection_name="documents",
    optimizer_config={"indexing_threshold": 20000}
)

最佳实践

  1. 批量操作 - 使用批量 upsert/search 提高效率
  2. Payload 索引 - 为过滤字段建立索引
  3. 量化 - 大集合(>100 万向量)建议启用
  4. 分片 - 集合超过 1000 万向量时使用
  5. 磁盘存储 - 大 payload 时启用 on_disk_payload
  6. 连接池 - 复用客户端实例

常见问题

带过滤条件的搜索速度慢:

# 为过滤字段创建 payload 索引
client.create_payload_index(
    collection_name="docs",
    field_name="category",
    field_schema=PayloadSchemaType.KEYWORD
)

内存不足:

# 启用量化和磁盘存储
client.create_collection(
    collection_name="large_collection",
    vectors_config=VectorParams(size=384, distance=Distance.COSINE),
    quantization_config=ScalarQuantization(...),
    on_disk_payload=True
)

连接问题:

# 设置超时和重试
client = QdrantClient(
    host="localhost",
    port=6333,
    timeout=30,
    prefer_grpc=True  # gRPC 提供更好性能
)

参考文档

资源