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

MongoDB Vector Search

In this guide, you can learn how to use the MongoDB Vector Search feature in the Kotlin driver. The Aggregates builders class provides the the vectorSearch() helper method that you can use to create a $vectorSearch pipeline stage. This pipeline stage allows you to perform a semantic search on your documents. A semantic search is a type of search which locates information that is similar in meaning, but not necessarily identical, to your provided search term or phrase.

重要

功能兼容性

要了解哪些版本的 MongoDB Atlas 支持此功能,请参阅 MongoDB Atlas 文档中的限制

要使用此功能,您必须创建向量搜索索引并为向量索引。要学习如何以编程方式创建向量搜索索引,请参阅索引指南中的MongoDB Search和MongoDB Vector Search 索引部分。要学习;了解有关向量嵌入的更多信息,请参阅Atlas文档中的如何为向量搜索编制向量嵌入索引

在向量嵌入上创建向量搜索索引后,您可以在管道阶段引用此索引,如下节所示。

本部分中的示例使用通过以下 Kotlin 数据类建模的数据:

data class MovieAlt(
val title: String,
val year: Int,
val plot: String,
val plotEmbedding: List<Double>
)

此示例演示如何构建聚合管道,该管道使用vectorSearch()方法执行符合以下规范的精确向量搜索:

  • 使用字符串值的向量嵌入搜索plotEmbedding字段值

  • 使用mflix_movies_embedding_index向量搜索索引

  • 返回 1 个文档

  • 筛选year值至少为2016的文档

Aggregates.vectorSearch(
SearchPath.fieldPath(MovieAlt::plotEmbedding.name),
BinaryVector.floatVector(floatArrayOf(0.0001f, 1.12345f, 2.23456f, 3.34567f, 4.45678f)),
"mflix_movies_embedding_index",
1.toLong(),
exactVectorSearchOptions().filter(Filters.gte(MovieAlt::year.name, 2016))
)

提示

查询向量类型

前面的示例创建了一个 BinaryVector实例提供服务查询向量,但您也可以创建 Double 实例的 List。 但是,我们建议您使用 BinaryVector 类型以提高存储效率。

提示

Kotlin向量搜索示例

访问Atlas文档,查找有关使用Kotlin驱动程序执行MongoDB Vector Search 的更多教程。

要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: