空间

在向量搜索中,空间(space)定义了如何计算两个向量之间的距离(或相似度)。空间的选择会影响在搜索操作中如何确定最近邻。

距离计算

空间定义了用于测量两点之间距离的函数,以便确定 k-最近邻。在 k-NN 搜索中,分数越低表示结果越接近、越好。这与 UDB-SX 的评分方式相反,在 UDB-SX 中,分数越高表示结果越好。UDB-SX 支持以下空间类型。

并非每个方法/引擎组合都支持所有空间类型。有关支持的空间列表,请参阅方法文档中特定引擎的章节。

| 空间类型 | 搜索类型 | 距离函数 ($$d$$ ) | UDB-SX 分数 | | :— | :— | :— | | l1 | 近似、精确 | $$ d(\mathbf{x}, \mathbf{y}) = \sum_{i=1}^n \lvert x_i - y_i \rvert $$ | $$ score = {1 \over {1 + d} } $$ | | l2 | 近似、精确 | $$ d(\mathbf{x}, \mathbf{y}) = \sum_{i=1}^n (x_i - y_i)^2 $$ | $$ score = {1 \over 1 + d } $$ | | linf | 近似、精确 | $$ d(\mathbf{x}, \mathbf{y}) = max(\lvert x_i - y_i \rvert) $$ | $$ score = {1 \over 1 + d } $$ | | cosinesimil | 近似、精确 | $$ d(\mathbf{x}, \mathbf{y}) = 1 - cos { \theta } = 1 - {\mathbf{x} \cdot \mathbf{y} \over \lVert \mathbf{x}\rVert \cdot \lVert \mathbf{y}\rVert}$$$$ = 1 - {\sum_{i=1}^n x_i y_i \over \sqrt{\sum_{i=1}^n x_i^2} \cdot \sqrt{\sum_{i=1}^n y_i^2}}$$,
其中 $$\lVert \mathbf{x}\rVert$$ 和 $$\lVert \mathbf{y}\rVert$$ 分别表示向量 $$\mathbf{x}$$ 和 $$\mathbf{y}$$ 的范数。 | $$ score = {2 - d \over 2} $$ | | innerproduct (UDB-SX 2.13 及更高版本中 Lucene 支持) | 近似 | NMSLIBFaiss
$$ d(\mathbf{x}, \mathbf{y}) = - {\mathbf{x} \cdot \mathbf{y}} = - \sum_{i=1}^n x_i y_i $$

Lucene
$$ d(\mathbf{x}, \mathbf{y}) = {\mathbf{x} \cdot \mathbf{y}} = \sum_{i=1}^n x_i y_i $$ | NMSLIBFaiss
$$ \text{如果} d \ge 0, score = {1 \over 1 + d }$$
$$ \text{如果} d < 0, score = −d + 1$$

Lucene:
$$ \text{如果} d > 0, score = d + 1 $$
$$ \text{如果} d \le 0, score = {1 \over 1 + (-1 \cdot d) }$$ | | innerproduct | 精确 | $$ d(\mathbf{x}, \mathbf{y}) = - {\mathbf{x} \cdot \mathbf{y}} = - \sum_{i=1}^n x_i y_i $$ | $$ \text{如果} d \ge 0, score = {1 \over 1 + d }$$
$$ \text{如果} d < 0, score = −d + 1$$ | | hamming (UDB-SX 支持二进制向量) | 近似、精确 | $$ d(\mathbf{x}, \mathbf{y}) = \text{countSetBits}(\mathbf{x} \oplus \mathbf{y})$$ | $$ score = {1 \over 1 + d } $$ | | hammingbit (支持二进制和长整型向量) | 精确 | $$ d(\mathbf{x}, \mathbf{y}) = \text{countSetBits}(\mathbf{x} \oplus \mathbf{y})$$ | $$ score = {1 \over 1 + d } $$ |

余弦相似度公式不包含 1 - 前缀。然而,由于相似度搜索库将较低的分数等同于更接近的结果,因此对于余弦相似度空间,它们返回 1 - cosineSimilarity——这就是为什么距离函数中包含了 1 -

对于余弦相似度,传入零向量([0, 0, ...])作为输入是无效的。这是因为此类向量的大小为 0,在相应的公式中会引发 除以 0 异常。包含零向量的请求将被拒绝,并抛出相应的异常。

hamming 空间类型在 UDB-SX 2.16 及更高版本中支持二进制向量。有关更多信息,请参阅二进制 k-NN 向量

指定空间类型

空间类型在创建索引时指定。

您可以在字段映射的顶层指定空间类型:

PUT /test-index
{
  "settings": {
    "index": {
      "knn": true
    }
  },
  "mappings": {
    "properties": {
      "my_vector1": {
        "type": "knn_vector",
        "dimension": 3,
        "space_type": "l2"
      }
    }
  }
}

或者,如果在定义方法,您可以在 method 对象内部指定空间类型:

PUT test-index
{
  "settings": {
    "index": {
      "knn": true,
      "knn.algo_param.ef_search": 100
    }
  },
  "mappings": {
    "properties": {
      "my_vector1": {
        "type": "knn_vector",
        "dimension": 1024,
        "method": {
          "name": "hnsw",
          "space_type": "l2",
          "engine": "nmslib",
          "parameters": {
            "ef_construction": 128,
            "m": 24
          }
        }
      }
    }
  }
}