You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用向量数据库匹配相似用户?为何Pinecone相似度全为0?

为什么Pinecone中所有匹配的相似度score都是0?

我开发了一款应用,用户注册后需填写包含16个滑块问题的资料,每个问题答案范围为-3至3(或0至7)。我希望借助Pinecone向量数据库查找相似用户,但实验中所有匹配的相似度score都为0。以下是我的操作:

索引代码

const index = await initIndex()

const vectors = [
  {
    id: '1',
    values: [-3, -3, -3, -3, -3]
  },
  {
    id: '2',
    values: [-1, -1, -1, -1, -1]
  },
  {
    id: '3',
    values: [0, 0, 0, 0, 0]
  },
  {
    id: '4',
    values: [1, 1, 1, 1, 1]
  },
  {
    id: '5',
    values: [3, 3, 3, 3, 3]
  }
] as Vector[]

const upsertRequest: UpsertRequest = {
  vectors
}

await index.upsert({
  upsertRequest,
})

搜索代码

const index = await initIndex()

const queryRequest = {
  topK: 10,
  vector: [0, 0, 0, 0, 0],
  includeValues: true
}

const queryResponse = await index.query({ queryRequest })

查询结果

{
    "queryResponse": {
        "results": [],
        "matches": [
            {
                "id": "2",
                "score": 0,
                "values": [
                    -1,
                    -1,
                    -1,
                    -1,
                    -1
                ]
            },
            {
                "id": "1",
                "score": 0,
                "values": [
                    -3,
                    -3,
                    -3,
                    -3,
                    -3
                ]
            },
            {
                "id": "3",
                "score": 0,
                "values": [
                    0,
                    0,
                    0,
                    0,
                    0
                ]
            },
            {
                "id": "5",
                "score": 0,
                "values": [
                    3,
                    3,
                    3,
                    3,
                    3
                ]
            },
            {
                "id": "4",
                "score": 0,
                "values": [
                    1,
                    1,
                    1,
                    1,
                    1
                ]
            }
        ],
        "namespace": ""
    }
}

问题原因

Pinecone默认使用余弦相似度作为度量标准,而你使用了零向量[0,0,0,0,0]作为查询向量,这直接导致所有匹配的score为0:

  • 余弦相似度计算公式为:cosθ = (A·B) / (||A|| * ||B||),其中A·B是向量点积,||A||代表向量的模长。
  • 当查询向量A是零向量时,A·B的结果必然为0,无论向量B的取值是什么,最终余弦相似度都会是0。
  • 即使是与查询向量完全相同的零向量(id=3),由于模长为0,余弦相似度无数学定义,Pinecone也会返回0分。

你预期的0.5相似度是错误的——零向量和任何非零向量的余弦相似度都是0,而非你设想的数值。

解决方案

  1. 避免用零向量查询:如果是测试,改用非零的用户向量作为查询条件。比如用id=2的[-1,-1,-1,-1,-1]查询,会得到id=2的score为1.0(完全匹配)、id=1的score≈0.94(高度相似)、id=4的score=-1.0(完全相反)的合理结果。
  2. 归一化向量:你的原始数据范围是-3到3(或0到7),建议先将每个向量归一化为单位向量(模长为1),这样余弦相似度能更准确反映用户间的相似性。例如,[-3,-3,-3,-3,-3]归一化后为[-3/√45, -3/√45, -3/√45, -3/√45, -3/√45]。
  3. 切换相似度度量:如果余弦相似度不符合需求,可以在创建Pinecone索引时指定其他度量(如欧氏距离、点积)。例如,使用点积作为度量时,非零向量的查询会得到合理数值,但零向量查询仍会返回0。

内容的提问来源于stack exchange,提问作者Andre Zimpel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 00:22:13