Overview
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 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 类型以提高存储效率。
API 文档
要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: