通过将MongoDB用作向量数据库,您可以使用MongoDB Vector Search 无缝搜索向量数据和其他MongoDB数据并为其索引。MongoDB Vector Search 使您能够根据数据的语义查询,将向量搜索与全文搜索相结合,并过滤集合中其他字段的查询,以便检索与您的使用案例最相关的结果。
Use MongoDB Vector Search to power your AI applications and agentic systems by implementing retrieval-augmented generation (RAG). You can also integrate MongoDB Vector Search with popular AI frameworks and services to quickly build AI applications and agents.
通过自动嵌入,您可以一键启用语义搜索。MongoDB Vector Search 自动为文本数据生成和管理 vector embeddings,因此您无需写入嵌入代码或管理 model 基础架构。
重要
The feature described on this page is not currently supported on Atlas Infinite clusters during public preview. For the features supported in public preview, see Public Preview Availability.
注意
MongoDB Vector Search 支持在运行MongoDB v6.0.11 的Atlas集群上进行 近似最近邻 (ANN)搜索,v7.0.2 或更高版本,精确最近邻 (ENN) 会在运行MongoDB v6.0.16 的集群上搜索、v7.0.10,v7.3.2,或更高版本。您还可以将MongoDB Vector Search 与使用自管理或本地Atlas部署结合使用。
什么是向量搜索?
Vector search is a search method that returns results based on your data's semantic, or underlying, meaning. Unlike traditional full-text search which finds text matches, vector search finds vectors that are close to your search query in multi-dimensional space. The closer the vectors are to your query, the more similar they are in meaning.
通过解释搜索查询和数据的含义,向量搜索可让您考虑搜索者的意图和搜索上下文,以检索出更相关的结果。
例如,如果您搜索了词语“红色水果”,则全文搜索仅返回显式包含这些关键字的数据。但是,语义搜索可能会返回含义相似的数据,例如苹果或草莓等红色水果。
用例
MongoDB Vector Search 支持以下向量搜索使用案例:
语义搜索:使用近似最近邻 (ANN) 或精确最近邻 (ENN) 搜索算法,根据语义相似性查询向量嵌入。
要了解更多信息,请参阅如何执行语义搜索和运行向量搜索近似最近邻 (ANN) 和精确最近邻 (ENN) 查询。
Hybrid Search: Combine results from multiple search queries, including vector search and full-text search. To learn more, see How to Perform Hybrid Search.
生成式搜索:使用MongoDB Vector Search 通过实施检索增强生成 (RAG)来检索生成式AI应用程序的相关数据。
要学习;了解如何实现RAG,请参阅结合MongoDB Vector Search 的 RAG。要学习;了解如何使用向量搜索构建AI助手(包括实施代理 RAG),请参阅使用MongoDB构建AI助手。
自动嵌入
MongoDB Vector Search 中的自动嵌入服务是一种一键式方式,可对集合中的文本数据启用语义搜索。使用自动嵌入,您无需自行生成、存储或管理vector embeddings。相反, MongoDB Vector Search 自动嵌入服务会在为集合中的指定文本字段索引时和在查询文本的查询时使用 Voyage AI embedding model 生成嵌入,并在数据发生变化时使嵌入保持同步。
要启用自动嵌入,请创建 autoEmbed 类型的MongoDB Vector Search索引并选择 embedding model。创建索引后, MongoDB Vector Search 会原生处理嵌入的生成、更新和查询。
要学习;了解更多信息,请参阅自动嵌入概述。
AI 集成
您可以将MongoDB Vector Search 与任何AI提供商的嵌入和生成模型一起使用。MongoDB和合作伙伴还提供特定的产品集成,帮助您在AI支持的应用程序中利用MongoDB Vector Search。这些集成包括框架、平台和工具,启用您能够快速实现向量搜索、RAG 和AI代理。
To learn more, see MongoDB AI Integrations and Partners.
关键概念
- 向量
向量是一组数字,代表多个维度的数据。向量可以表示任何类型的数据,包括文本、图像、音频数据以及非结构化数据。通过测量向量之间的距离,可以确定语义相似性。
向量维数是指数组中元素的数量,因此它也指在其中绘制向量的向量空间内的维度数量。
具体来说, MongoDB Vector Search 使用密集向量,密集向量是一种高维向量,有利于较小的存储和丰富的语义。与稀疏向量不同,密集向量可以容纳更多数据,这使得MongoDB Vector Search 能够捕获更复杂的关系。
- 向量嵌入
Vector embeddings are vectors you use to represent your data. These embeddings capture meaningful relationships in your data and enable tasks like semantic search and retrieval. You create vector embeddings by passing your data through an embedding model, and you can store these embeddings in a MongoDB collection as a field in a document.
MongoDB Vector Search 通过识别与查询向量距离最近的向量嵌入来确定语义相似度。
要学习;了解更多信息,请参阅如何手动创建向量嵌入。
- 内嵌模型
嵌入模型是用于将数据转换为向量嵌入的算法。为此,嵌入模型使用 LLM(在大量数据上训练的机器学习模型)来生成捕获数据语义的向量嵌入。
您选择的嵌入模型决定了向量嵌入的维度。您必须将这些维度指定为MongoDB Vector Search索引中的字段。
嵌入模型因模型的训练方式而异。因此,根据您的数据和使用案例,不同的模型具有不同的优势。要学习;了解更多信息,请参阅选择嵌入模型。对于最先进的嵌入模型,请使用 Voyage AI。
MongoDB Vector Search 索引
要对MongoDB中的数据执行向量搜索,必须创建MongoDB 向量搜索索引。MongoDB Vector Search 搜索索引与其他数据库索引分开,用于在查询时高效地检索包含向量嵌入的文档。在MongoDB Vector Search索引定义中,您可以对集合中包含嵌入的字段索引,以启用针对这些字段的向量搜索。MongoDB Vector Search 支持长度小于和等于 8192 维的嵌入。
您还可以对集合中要运行MongoDB Vector Search 查询的其他字段建立索引,以对数据进行预过滤。您可以过滤布尔值、日期、ObjectId、数值、字符串和 UUID 值,包括这些类型的数组。 筛选数据可缩小搜索范围,并确保某些向量嵌入不会被考虑进行比较。
要学习;了解如何为MongoDB Vector Search 建立字段索引,请参阅如何为 Vector Search 建立字段索引。
MongoDB Vector Search 查询
MongoDB 向量搜索支持使用 Hierarchical Navigable Small Worlds 算法进行近似最近邻 ( ANN) 搜索,以及精确最近邻 ( ENN) 搜索。
为了找到最相似的向量, MongoDB Vector Search 执行 ANN 搜索而无需扫描每个向量嵌入,并对所有索引的向量嵌入执行详尽的 ENN 搜索。要学习;了解更多信息,请参阅 vectorSearch 定义。
MongoDB Vector Search queries consist of aggregation pipeline stages where the $vectorSearch stage is the first stage in the pipeline. The process for a basic MongoDB Vector Search query is as follows:
您选择近似最近邻 (ANN) 或精确最近邻 (ENN) 搜索,并指定查询向量,后者是表示搜索查询的向量嵌入。
MongoDB Vector Search 查找数据中与查询向量最接近的向量嵌入。
MongoDB Vector Search 返回包含最相似向量的文档。
To customize your vector search query, you can pre-filter your data on fields that you've indexed by using an MQL match expression with supported query or aggregation operators, or you can add additional aggregation stages to further process and organize your results.
要学习如何创建和运行 MongoDB 向量搜索 搜索查询,请参阅运行 向量搜索 近似最近邻 (ANN) 和精确最近邻 (ENN) 查询。
后续步骤
For a hands-on experience creating MongoDB Vector Search indexes and running MongoDB Vector Search queries against sample data, try the MongoDB Vector Search Course on MongoDB University and the tutorials in the following pages:
For optimal performance, we recommend deploying separate search nodes for workload isolation. Search Nodes support concurrent query execution to improve individual query latency. To learn more, see Review Deployment Options.