

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 向量索引
<a name="indexes-vector"></a>

矢量索引是一种特殊类型的索引，旨在有效地查询和管理存储在文档集合中的矢量数据。Amazon DocumentDB 支持分层可导航小世界 (HNSW) 和带平面压缩的倒置文件 (ivfFLat) 索引。

有关更多信息，请参阅 [Amazon DocumentDB 向量搜索](vector-search.md)。

向量索引有利于机器学习和生成式 AI 用例，例如：
+ 语义搜索
+ 产品推荐
+ 个性化
+ 聊天机器人
+ 欺诈检测
+ 异常检测

## 支持的索引属性
<a name="indexes-vector-properties"></a>


| Option | 3.6 | 4.0 | 5.0 | 8.0 | 弹性集群 | 
| --- | --- | --- | --- | --- | --- | 
| [name](index-property-name.md) | 否 | 否 | 是 | 是 | 否 | 

## 创建向量索引
<a name="indexes-vector-creating"></a>

使用带有`runCommand()`方法的 createIndex 命令来创建向量索引。语法如下：

```
db.runCommand({
  "createIndexes": "<collection>", 
  "indexes": [{
    "key": {
      "<field>": "vector"
    },
    "name": "<name>",
    "vectorOptions": {
      "type": "<hnsw> | <ivfflat>",
      "dimensions": <number of dimensions>,
      "similarity": "<euclidean>|<cosine>|<dotProduct>",
      "lists": <number_of_lists> [applicable for IVFFlat],
      "m": <max number of connections> [applicable for HNSW],
      "efConstruction": <size of the dynamic list for index build> [applicable for HNSW]
    }
  }] 
})
```

关键参数是一个 JSON 文档，用于指定字段和向量索引类型：

```
{
  "<field>": "vector"
}
```

有关创建矢量[索引的示例，请参阅索引属性](index-properties.md)。