在 pgvector 中指定相似度搜索方法
在 pgvector 中指定相似度搜索方法
pgvector 支持多种向量相似度搜索方法,您可以根据需求选择最适合的算法。以下是详细的使用指南:
1. 基本相似度操作符
pgvector 提供了三种基本相似度计算操作符:
操作符 | 计算方法 | 排序方向 | 适合场景 |
---|---|---|---|
<-> | 欧氏距离 (L2) | ASC | 越小越相似 |
<#> | 负内积 | DESC | 越大越相似 |
<=> | 余弦相似度距离 | ASC | 1 - 余弦相似度,越小越相似 |
使用示例
-- 欧氏距离 (L2) - 适合大多数常规向量
SELECT id, content, embedding <-> '[0.1, 0.2, ..., 0.3]' AS distance
FROM documents
ORDER BY distance
LIMIT 10;-- 负内积 - 适合标准化后的向量
SELECT id, content, embedding <#> '[0.1, 0.2, ..., 0.3]' AS negative_inner_product
FROM documents
ORDER BY negative_inner_product
LIMIT 10;-- 余弦距离 - 适合文本相似度
SELECT id, content, 1 - (embedding <=> '[0.1, 0.2, ..., 0.3]') AS cosine_similarity
FROM documents
ORDER BY embedding <=> '[0.1, 0.2, ..., 0.3]'
LIMIT 10;
2. 创建索引时指定搜索方法
创建索引时需要明确指定使用的相似度方法:
-- 为欧氏距离(L2)创建索引
CREATE INDEX ON documents USING hnsw (embedding vector_l2_ops);-- 为内积创建索引
CREATE INDEX ON documents USING hnsw (embedding vector_ip_ops);-- 为余弦相似度创建索引
CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops);
3. 高级搜索方法配置
调整HNSW索引参数
-- 创建索引时调整参数
CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 100);-- 查询时调整搜索范围
SET hnsw.ef_search = 200; -- 默认40,增大可提高召回率但降低速度
调整IVFFlat索引参数
-- 创建IVFFlat索引
CREATE INDEX ON documents USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 1000);-- 查询时调整探测数量
SET ivfflat.probes = 20; -- 默认1,增大可提高召回率
4. 混合搜索策略
结合多种相似度方法:
-- 先使用欧氏距离粗筛,再用余弦距离精排
WITH rough_candidates AS (SELECT id, embedding <-> query_vec AS l2_distanceFROM documentsORDER BY l2_distanceLIMIT 1000
)
SELECT id, 1 - (embedding <=> query_vec) AS cosine_sim
FROM rough_candidates
ORDER BY cosine_sim DESC
LIMIT 10;
5. 实际应用示例
-- 示例1:语义搜索(Qwen3嵌入)
SET hnsw.ef_search = 100;
SELECT id, title, 1 - (embedding <=> '[0.12, -0.34, ..., 0.56]') AS similarity
FROM articles
WHERE 1 - (embedding <=> '[0.12, -0.34, ..., 0.56]') > 0.7
ORDER BY similarity DESC
LIMIT 5;-- 示例2:结合关键词过滤
SELECT id, title,(embedding <#> query_vec) * 0.7 + ts_rank_cd(to_tsvector(content), plainto_tsquery('人工智能')) * 0.3 AS score
FROM documents
ORDER BY score DESC
LIMIT 10;
选择建议
- 文本嵌入(Qwen3等):优先使用
余弦相似度
(<=>) - 图像/音频嵌入:可尝试
欧氏距离
(<->)或内积
(<#>) - 大规模数据集:HNSW索引通常优于IVFFlat
- 高召回率需求:增加ef_search/probes参数
记得为您的查询方法创建对应的索引以获得最佳性能!