对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

How to Index Fields for Automated Embedding

接口

You can use the autoEmbed type on all cluster tiers to index text fields for automated embedding. When you index a field as the autoEmbed type, MongoDB Search uses the Voyage AI embedding model that you specify to generate vector embeddings for the text in the source field.

You can use the vectorSearch operator in your $search aggregation pipeline to query fields indexed as the autoEmbed type. You can combine the query with a lexical prefilter on other fields in the same index.

选项
类型
必要性
说明

type

字符串

必需

标识此字段类型的人类可读标签。值必须是 autoEmbed。

modality

字符串

必需

Type of data in the source field. Value must be text.

model

字符串

必需

用于生成嵌入的 Voyage AI嵌入模型。您可以指定以下模型之一:

  • voyage-4-lite

  • voyage-4

  • voyage-4-large

  • voyage-code-4

  • voyage-code-3

To learn more, see Automated Embedding Models.

numDimensions

Int

Optional

Number of vector dimensions that MongoDB Search enforces at index-time and query-time. You can specify one of the following values:

  • 256

  • 512

  • 1024

  • 2048

如果省略,则默认为 1024 个向量 dimensions。

similarity

字符串

Optional

用于搜索前 K 个最近邻的向量相似度函数。您可以指定以下值之一:

  • euclidean — 测量向量两端之间的距离。

  • cosine — 根据向量之间的角度测量相似度。

  • dotProduct - 测量类似于 cosine 的相似度,但考虑了向量的幅度。

If omitted, the default depends on the quantization value:

  • dotProduct for float and scalar

  • euclidean for binary and binaryNoRescore

要学习;了解更多信息,请参阅关于相似度函数。

quantization

字符串

Optional

用于存储嵌入的数据类型。您可以指定以下值之一:

  • float - 使用 float (4 字节) 向量值构建索引。

  • scalar - 使用标量(1 字节)向量值构建索引。

  • binary - 构建一个具有二进制(1 位)向量值的索引,并使用浮点(全精度)向量值进行重新评分。

  • binaryNoRescore - 构建具有二进制向量值的索引。与 binary(带重打分的二进制量化)相比,此选项可确保更快的查询速度并降低存储费用,但准确性较低。

如果省略,则默认为 scalar 量化。要了解更多信息,请参阅关于量化。

indexingMethod

字符串

Optional

向量字段的索引结构。值可以是:

  • hnsw - 用于基于图表的索引,其中连接了相似的向量

  • flat - 适用于平面、非图表、索引

如果省略,则默认为 hnsw。要了解更多,请参阅 关于索引方法。

hnswOptions.

对象

Optional

Parameters to use for Hierarchical Navigable Small Worlds graph construction. This is mutually exclusive with the flat indexing method. If omitted, uses the default values for the maxEdges and numEdgeCandidates parameters.

NOTE: We recommend using the default values. Specify hnswOptions only if you are experiencing suboptimal recall on large indexes. While higher values provide better recall, they also increase memory usage and slow down indexing and search speed.

要学习更多内容,请参阅关于索引方法。

hnswOptions.
maxEdges

Int

Optional

在分层可导航小世界图中,一个节点最多可以拥有的边(或连接)数量。值可以在 16 到 64 之间(含)。如果未指定,则默认值为 16。例如,对于值 16,在分层可导航小世界图的每一层,每个节点最多可以有十六个出边。

A higher number improves recall (accuracy of search results) because the graph is better connected. However, this slows down query speed because of the number of neighbors to evaluate per graph node, increases the memory for the Hierarchical Navigable Small Worlds graph because each node stores more connections, and slows down indexing because MongoDB Search evaluates more neighbors and adjusts for every new node added to the graph.

hnswOptions.
numEdgeCandidates

Int

Optional

类似于查询时的 numCandidates,此参数控制要评估的最大节点数,以找到连接到新节点的最近邻居。值可以在 100 到 3200 之间(含)。如果未指定,值默认为 100。

较高的数值可以生成具有高质量连接的图,从而提升搜索质量(召回率),但也可能对查询延迟产生负面影响。

注意

The Atlas UI JSON Editor schema validation doesn't recognize the indexingMethod and hnswOptions fields for the autoEmbed type and might flag them as errors. You can still save an index definition that includes these fields.

The following index definitions use the sample_mflix.movies collection in the sample data. After you load the collection, you can use the following examples to index the plot field for automated embedding.

适用以下限制:

  • You can't use a field name that begins with _autoEmbed at the top level of the index mappings. This namespace is reserved for internal use.

  • You can't enable dynamic mappings at any level in index definitions that contain the autoEmbed type. This applies when you create or update the index. Use static mappings instead.

  • You can't use the autoEmbed type in a typeSet definition. To learn which types a type set accepts, see Configure a typeSet.

  • You can't use storedSource in index definitions that contain the autoEmbed type.

  • You can't index fields inside an embeddedDocuments type field as the autoEmbed type. You can index fields inside a document type field as the autoEmbed type.

  • You can't specify a path option in an autoEmbed type field definition. The field name in the index mappings identifies the source field. For example, MongoDB Search rejects the following fields mapping:

    { "title": { "type": "autoEmbed", "path": "title", "model": "voyage-4", "modality": "text" }}
  • You can't declare the same source path for more than one autoEmbed type field. This includes a literal dotted field name, such as parent.child, and the same path nested inside a document type field. For example, MongoDB Search rejects the following fields mapping:

    { "parent.child": { "type": "autoEmbed", "model": "voyage-4-lite", "modality": "text" }, "parent": { "type": "document", "dynamic": false, "fields": { "child": { "type": "autoEmbed", "model": "voyage-4", "modality": "text" } } }}

    To index the nested field, remove the parent.child field and keep only the nested document type definition:

    { "parent": { "type": "document", "dynamic": false, "fields": { "child": { "type": "autoEmbed", "model": "voyage-4", "modality": "text" } } }}
  • You can't remove all autoEmbed type fields from an index.

  • You can't index a source field as the autoEmbed type and as the document, embeddedDocuments, or geo type at the same path. You can index a source field as the autoEmbed type and as a scalar type, such as string or token, at the same path. For example, the following fields mapping indexes the plot field as both the token type and the autoEmbed type:

    { "plot": [ { "type": "token" }, { "type": "autoEmbed", "model": "voyage-4", "modality": "text" } ]}

    For a complete example, see Index the Source Field as Multiple Types.

  • You can't sort on a field that you index as the autoEmbed type.

  • You can't create an index for the autoEmbed type from the Atlas UI Visual Editor. Instead, use the JSON Editor.

  • If your index definition contains an autoEmbed type field, the exists operator might not match documents on a field that you declare without a type, such as "b": []. MongoDB Search doesn't carry declarations that add no type into the index that it builds for automated embedding. For example, the following fields mapping declares the b field without a type:

    { "a": { "type": "document", "dynamic": false, "fields": { "b": [], "c": { "type": "autoEmbed", "model": "voyage-4", "modality": "text" } } }}

    Because the b field declaration adds no type, the index that MongoDB Search builds for automated embedding doesn't record a.b. As a result, an exists query on a.b might not match a document such as { "a": { "b": "x" } }. Without the autoEmbed type field, the same query matches the document.

    To make exists queries match, declare a type for the field. For example, replace "b": [] with "b": { "type": "string" }.

  • You can't index autoEmbed type fields and vector type fields in the same vectorSearch type index definition for MongoDB Vector Search $vectorSearch queries.

    You can index autoEmbed type fields and vector type fields in the same MongoDB Search index definition. For example, the following fields mapping indexes the plot_embedding_voyage_4_large field as the vector type and the plot field as the autoEmbed type:

    { "plot_embedding_voyage_4_large": { "type": "vector", "numDimensions": 1024, "similarity": "cosine" }, "plot": { "type": "autoEmbed", "model": "voyage-4", "modality": "text" }}