Docs 菜单
Docs 主页

使用MongoDB Vector Search 自动生成嵌入

您可以将MongoDB Vector Search 配置为自动生成和管理集群中文本数据的向量嵌入。您可以在 M10 或更高版本的Atlas 集群中创建一键式AI语义搜索索引,并使用 Voyage AI嵌入模型,从而通过向量简化索引、更新和查询。

启用自动嵌入后, MongoDB Vector Search 会在索引时为Atlas集合中的指定文本字段使用指定的嵌入模型自动生成嵌入,并在查询时为针对自动嵌入的索引字段查询中的文本字符串自动生成嵌入。

重要

您可以在云提供商的任何 M10 或更高级别的集群上使用MongoDB Vector Search 自动嵌入。但是,处理生成向量嵌入的推理进程的服务在Google Cloud Platform上运行。 这意味着,无论集群的云提供商如何,您的数据都会发送到Google Cloud Platform进行嵌入生成和检索。 我们提供企业级安全性,因此,您的数据仅存储在您的集群中。

嵌入模型在共享的多租户推理平台上运行。因此,在预览期间,您必须使用文档少于 100k 的数据集,并仅出于功能评估而非负载测试而运行查询。如果您有更高限制的使用案例,请联系您的客户团队。

虽然您的工作负载没有硬性速率限制,但有全局限制。如果您的查询返回速率限制错误(错误 409),请在应用程序代码中执行退避并重试。这使您的应用程序能够从容处理速率限制并确保持续运行。

启用使用自动嵌入启用向量搜索,您必须具备以下条件:

  • M10 或更高集群

  • 一个集合,其中包含您希望为其索引以实现自动嵌入的文本字段。

  • 以下客户端之一:

    • 用于创建索引的 Atlas UI

    • mongosh 用于创建索引和运行查询

    • 节点驱动程序 6.6.0 或更高版本,用于创建索引和运行查询

    • Python驱动程序 4.7 或更高版本,用于创建索引和运行查询

    使用以下下拉菜单为此页面上的过程设立客户端。

以下部分描述了用于启用自动生成文本字段嵌入的MongoDB Vector Search索引语法和字段,并引导您完成为自动嵌入配置索引的步骤。

您需要 Project Data Access Admin 或更高角色才能创建和管理MongoDB Vector Search 索引。

以下是启用自动生成嵌入的语法:

1{
2 "fields": [
3 {
4 "type": "text",
5 "path": "<field-name>",
6 "model": "voyage-3-large | voyage-3.5 | voyage-3.5-lite"
7 }
8 ]
9}

索引定义中需要以下字段:

字段

类型

说明

type

字符串

字段的类型。对于自动嵌入,该值必须为 text

path

字符串

集合中您想要为自动嵌入编制索引的字段的名称。

model

字符串

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

  • voyage-3-large — 跨语言和领域的最高质量检索。

  • voyage-3.5 - 平衡多语言使用和通用检索准确性的模型。

  • voyage-3.5-lite - 轻量级、速度更快的模型针对延迟和费用进行了优化。

创建索引后,如果您随后更改嵌入模型, MongoDB Vector Search 会为数据集生成新的嵌入。在MongoDB Vector Search 生成嵌入时,您可以使用旧的嵌入继续进行查询。当旧的嵌入替换为新的嵌入模型中的嵌入时, MongoDB Vector Search 会删除旧的嵌入。

自动嵌入的索引字段与以下 vector 类型索引字段互斥:

  • numDimensions

  • similarity

  • quantization

如果您的集合已经包含嵌入,则必须使用 vector 类型字段来为嵌入创建索引。要了解有关使用嵌入对字段进行索引的更多信息,请参阅如何对 Vector Search 字段进行索引

如果您想要为自动生成嵌入的文本字段建立索引,并使用您自己的嵌入为字段索引,则可以同时使用 textvector 类型创建索引。MongoDB Vector Search 将仅自动为针对索引为 text 类型的字段的查询生成嵌入。您必须在查询中指定嵌入,才能搜索索引为 vector 类型的字段。

您还可以使用MongoDB Vector Search filter 类型对字段进行索引以预筛选数据。预筛选可减少要运行相似性比较的文档数量,从而减少查询延迟并提高搜索结果的准确性。要学习;了解有关预过滤数据的更多信息,请参阅关于 filter 类型。

以下过程将演练在MongoDB Vector Search索引中启用自动嵌入的步骤。如果加载了 sample_mflix.movies 数据集,则过程中的示例将演示如何为集合中的 fullplot字段启用自动嵌入。

1
  1. 如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。

  2. 如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。

  3. 在侧边栏中,单击 Database 标题下的 Clusters

会显示集群页面。

2

您可以从 Search & Vector Search 选项或 Data Explorer 转到MongoDB搜索页面。

3

在页面上进行以下选择,然后单击 Next

Search Type

选择 Vector Search 索引类型。

Index Name and Data Source

指定以下信息:

  • Index Name: <INDEX-NAME>

  • Database and Collection:

    • <DATABASE-NAME>

    • <COLLECTION-NAME>

Configuration Method

选择 JSON Editor

例子

例如,输入 movies_automated_embeddings 作为索引名称,然后从 Database and Collection 下拉菜单中选择 sample_mflixmovies

4

在 JSON 编辑器中,输入以下索引定义:

1{
2 "fields": [
3 {
4 "type": "text",
5 "path": "<FIELD-NAME>",
6 "model": "voyage-3-large | voyage-3.5 | voyage-3.5-lite"
7 }
8 ]
9}

例子

例如,要创建一个索引,该索引通过使用集合中fullplot字段的voyage-3-large模型来启用自动嵌入,请输入以下内容:

1{
2 "fields": [
3 {
4 "type": "text",
5 "path": "fullplot",
6 "model": "voyage-3-large"
7 }
8 ]
9}
5
  1. 单击 Next(连接)。

  2. 查看索引配置并单击 Create Vector Search Index

注意

索引创建可能需要一些时间才能完成。当索引正在创建时,Atlas UI 中的 Status 列显示 Pending。当索引创建完成后,Atlas UI 中的 Status 列将显示 Ready

1

要学习;了解更多信息,请参阅通过mongosh连接到集群。

2

要切换,请运行以下命令:

use <DATABASE-NAME>

此处,<DATABASE-NAME> 是要使用的数据库的名称。

例子

示例,要切换到包含 movies集合的 sample_mflix数据库,运行以下命令:

use sample_mflix
3

db.collection.createSearchIndex() 方法使用的语法如下:

1db.<COLLECTION-NAME>.createSearchIndex(
2 "<INDEX-NAME>",
3 "vectorSearch",
4 {
5 "fields": [
6 {
7 "type": "text",
8 "path": "<FIELD-NAME>",
9 "model": "voyage-3-large | voyage-3.5 | voyage-3.5-lite"
10 },
11 {
12 "type": "filter",
13 "path": "<FIELD-NAME>"
14 },
15 ...
16 ]
17 }
18);

例子

示例,要通过对集合中的 fullplot字段使用 voyage-3-large 模型来创建支持自动嵌入的索引,请在终端中运行以下命令:

1db.movies.createSearchIndex(
2 "movies_automated_embeddings",
3 "vectorSearch",
4 {
5 "fields": [
6 {
7 "type": "text",
8 "path": "fullplot",
9 "model": "voyage-3-large"
10 }
11 ]
12 }
13);
1

在终端中运行以下命令,创建名为 auto-embeddings-project 的新目录并初始化项目:

mkdir auto-embeddings-project
cd auto-embeddings-project
npm init -y
2
touch <file-name>.js

例子

例如,创建一个名为 create_index.js 的文件。

touch create_index.js
3
1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas deployment
4const uri = "<CONNECTION-STRING>";
5const client = new MongoClient(uri);
6
7async function run() {
8 try {
9 const database = client.db("<DATABASE-NAME>");
10 const collection = database.collection("<COLLECTION-NAME>");
11
12 // define your MongoDB Vector Search index
13 const index = {
14 "name": "<INDEX-NAME>",
15 "type": "vectorSearch",
16 "definition": {
17 "fields": [
18 {
19 "type": "vector",
20 "path": "<FIELD-NAME>",
21 "model": "voyage-3-large | voyage-3.5 | voyage-3.5-lite"
22 },
23 ]
24 }
25 }
26 // run the helper method
27 await collection.createSearchIndex("<INDEX-NAME>", index);
28 } finally {
29 await client.close();
30 }
31}
32run().catch(console.dir);

例子

示例,要通过对 sample_mflix.movies命名空间中的 fullplot字段使用 voyage-3-large 模型来创建支持自动嵌入的索引,请将以下内容复制并粘贴到 create_index.js文件中:

1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas deployment
4const uri = "<CONNECTION-STRING>";
5const client = new MongoClient(uri);
6
7async function run() {
8 try {
9 const database = client.db("sample_mflix");
10 const collection = database.collection("movies");
11
12 // define your MongoDB Vector Search index
13 const index = {
14 "name": "movies_automated_embeddings",
15 "type": "vectorSearch",
16 "definition": {
17 "fields": [
18 {
19 "type": "text",
20 "path": "fullplot",
21 "model": "voyage-3-large"
22 }
23 ]
24 }
25 }
26 // run the helper method
27 await collection.createSearchIndex(index);
28 } finally {
29 await client.close();
30 }
31}
32run().catch(console.dir);
4

<CONNECTION-STRING>

Atlas 集群的连接字符串。

<DATABASE-NAME>

数据库名称,该数据库包含要创建索引的集合。

<COLLECTION-NAME>

要为其创建索引的集合的名称。

<FIELD-NAME>

集合中您想要为自动嵌入编制索引的字段的名称。

<INDEX-NAME>

要创建的索引的名称。

5
node <file-name>.js

例子

例如,要创建在 create_index.js 文件中定义的索引,请在终端运行以下命令:

node create_index.js
1
touch <file-name>.py

例子

例如,创建一个名为 create_index.py 的文件。

touch create_index.py
2
1from pymongo import MongoClient
2from pymongo.operations import SearchIndexModel
3
4client = pymongo.MongoClient("<CONNECTION-STRING>")
5db = client["<DATABASE-NAME>"]
6collection = db["<COLLECTION-NAME>"]
7
8search_index_model = SearchIndexModel(
9 definition={
10 "fields": [
11 {
12 "type": "text",
13 "path": "<FIELD-NAME>",
14 "model": "voyage-3-large | voyage-3.5 | voyage-3.5-lite"
15 }
16 ]
17 },
18 name="<INDEX-NAME>",
19 type="vectorSearch",
20)
21result = collection.create_search_index(model=search_index_model)
22print(result)

例子

示例,要通过对 sample_mflix.movies命名空间中的 fullplot字段使用 voyage-3-large 模型来创建支持自动嵌入的索引,请将以下内容复制并粘贴到 create_index.py文件中:

1from pymongo import MongoClient
2from pymongo.operations import SearchIndexModel
3
4client = MongoClient("<CONNECTION-STRING>")
5db = client["sample_mflix"]
6collection = db["movies"]
7
8search_index_model = SearchIndexModel(
9 definition={
10 "fields": [
11 {
12 "type": "text",
13 "path": "fullplot",
14 "model": "voyage-3-large"
15 }
16 ]
17 },
18 name="movies_automated_embeddings",
19 type="vectorSearch",
20)
21result = collection.create_search_index(model=search_index_model)
22print(result)
3

<CONNECTION-STRING>

Atlas 集群的连接字符串。

<database-name>

数据库名称,该数据库包含要创建索引的集合。

<collection-name>

要为其创建索引的集合的名称。

<field-name>

集合中您想要为自动嵌入编制索引的字段的名称。

<index-name>

要创建的索引的名称。

4
python <file-name>.py

例子

例如,要创建在 create_index.py 文件中定义的索引,请在终端运行以下命令:

python create_index.py

创建具有自动嵌入功能的索引后,您可以对索引字段运行文本查询。MongoDB Vector Search 使用索引中指定的相同嵌入模型自动为查询中的文本字符串生成嵌入。 它使用嵌入在索引中搜索在语义索引与指定查询文本相似的文档。

以下部分描述了$vectorSearch管道语法和字段,用于根据为自动嵌入建立索引的字段为查询文本自动生成嵌入,并演示如何针对为自动嵌入建立索引的字段运行语义搜索查询。

以下语法演示了如何对针对自动嵌入进行了索引的字段运行查询:

1{
2 "$vectorSearch": {
3 "index": "<index-name>",
4 "limit": <number-of-results>,
5 "numCandidates": <number-of-candidates>,
6 "path": "<field-to-search>",
7 "query": "<query-string>"
8 }
9}

使用自动嵌入的MongoDB Vector Search查询需要以下字段:

字段
类型
必要性
说明

exact

布尔

可选的

如果省略 numCandidates,则此字段为必填字段。与 numCandidates 互斥。

指定运行ENN 还是 ANN搜索的标志。值可以是以下之一:

  • false - 运行 ANN 搜索

  • true - 运行 ENN 搜索

如果省略,则默认值为 false

index

字符串

必需

要使用的MongoDB Vector Search索引的名称。

如果索引名称拼写错误或者指定的索引在集群上尚不存在,MongoDB Vector Search 不会返回结果。

limit

数字

必需

要在结果中返回的文档数量(仅限 int)。如果指定 numCandidates,该值不能超过 numCandidates 的值。

numCandidates

数字

可选的

如果 exactfalse 或被省略,则此字段为必填字段。与 exact 互斥。

搜索期间要使用的最近邻数量。值必须小于或等于 (<=) 10000。指定的数字不能小于要返回的文档数量 (limit)。

path

字符串

必需

要搜索的索引向量类型字段。

query

字符串

必需

用于自动生成嵌入并执行语义搜索的文本。

您可以对索引字段运行近似最近邻 (ANN)精确最近邻 (ENN) 查询。如需了解更多信息,请参阅近似最近邻 (ANN) 搜索精确最近邻 (ENN) 搜索。

您不能在查询中针对为自动嵌入建立索引的字段指定向量嵌入。相反,您必须对该字段运行自然语言查询。当您针对自动嵌入的索引字段运行自然语言查询时, MongoDB Vector Search 使用与索引字段相同的嵌入模型自动为查询文本生成嵌入。然后,它使用生成的嵌入对索引字段执行语义搜索。

您可以选择在查询中指定 filter 字段,以对MongoDB Vector Search 执行语义搜索的文档进行预筛选。要学习;了解更多信息,请参阅MongoDB Vector Search 预筛选。

您还可以选择检索结果中文档的分数。要学习;了解更多信息,请参阅MongoDB Vector Search Score。

您无法在Atlas用户界面中运行MongoDB Vector Search 查询。使用 mongosh 或支持的驾驶员运行查询。

1

要学习;了解更多信息,请参阅通过mongosh连接到集群。

2
use <database-name>

例子

例如,要切换到 sample_mflix 数据库,请在终端中运行以下命令:

use sample_mflix
3

db.collection.aggregate() 方法使用的语法如下:

1db.<COLLECTION-NAME>.aggregate([
2 {
3 "$vectorSearch": {
4 "index": "<INDEX-NAME>",
5 "path": "<FIELD-NAME>",
6 "query": "<QUERY-TEXT>",
7 "numCandidates": <NUMBER-OF-CANDIDATES-TO-CONSIDER>,
8 "limit": <NUMBER-OF-DOCUMENTS-TO-RETURN>
9 }
10 }
11])

例子

示例,要针对 movies集合中的 fullplot字段运行查询,以对语义上与 young heroes caught in epic struggles between light and darkness 相似的电影进行语义搜索,请复制、粘贴并运行以下查询。

1db.movies.aggregate([
2 {
3 "$vectorSearch": {
4 "index": "movies_automated_embeddings",
5 "path": "fullplot",
6 "query": "young heroes caught in epic struggles between light and darkness",
7 "numCandidates": 1000,
8 "limit": 10
9 }
10 },
11 {
12 "$project": {
13 "_id": 0,
14 "title": 1,
15 "fullplot": 1,
16 "score": {"$meta": "vectorSearchScore"}
17 }
18 }
19])
1[
2 {
3 title: 'Day Watch',
4 fullplot: 'Anton belongs to the Forces of the Light as well as his powerful girlfriend and apprentice, but his son is a powerful teenager from the Darkness and Anton protects him. When the balance between Light and Darkness is affected by the death of some evil vampires, Anton is framed and accused of the murders, and he chases an ancient chalk that has the power of changing the destiny of its owner.',
5 score: 0.5449697971343994
6 },
7 {
8 title: 'Dungeons & Dragons',
9 fullplot: "The Empire of Izmer has long been a divided land. The Mages - an elite group of magic-users - rule whilst the lowly commoners are powerless. Izmer's young Empress, Savina, wants equality and prosperity for all, but the evil Mage Profion is plotting to depose her and establish his own rule. The Empress possesses a scepter which controls Izmer's Golden Dragons. To challenge her rule, Profion must have the scepter, and tricks the Council of Mages into believing Savina is unfit to hold it. Knowing that Profion will bring death and destruction to Izmer, Savina must find the legendary Rod of Savrille, a mythical rod that has the power to control Red Dragons, a species even mightier than the Gold. Enter two thieves, Ridley and Snails, who unwittingly become instrumental in Savina's search for the Rod. Joined by a feisty Dwarf named Elwood, and helped by the Empress's expert tracker, the Elf Norda, the young heroes go in search of the Rod of Savrille. From the deadly maze of the Thieves Guild at Antius to an Elven Village, secret grotto and abandoned castles, Ridley and his band must outwit Profion's chief henchman Damodar at every turn while, back in Izmer, Profion prepares to do battle with the Empress. All depends on the Rod, but the outcome of the race to reach it first is far from certain, and Izmar's very survival hangs in the balance.",
10 score: 0.5414832830429077
11 },
12 {
13 title: 'Brave Story',
14 fullplot: 'A young boy attempts to change his destiny by entering a magic gateway to another world; but on his quest to find the Tower of Fortune and be granted any wish, he must conjure up all his bravery to battle demons, his friends, and ultimately himself.',
15 score: 0.5404887795448303
16 },
17 {
18 title: 'Justin and the Knights of Valour',
19 fullplot: 'Justin lives in a kingdom where bureaucrats rule and knights have been ousted. His dream is to be become one of the Knights of Valour, like his grandfather was, but his father Reginald, the chief counsel to the Queen, wants his son to follow in his footsteps and become a lawyer. After an inspiring visit to his beloved Grandmother and bidding farewell to his supposed lady-love Lara, Justin leaves home and embarks on a quest to become a knight. Along the way he meets the beautiful, feisty Talia, a quirky wizard called Melquiades, and the handsome Sir Clorex and is mentored by three monks; Blucher, Legantir and Braulio, who teach and test him in the ancient ways of the Knights of Valour. Whilst an unlikely candidate for knighthood, Justin must rise to the challenge quickly when banished former knight Sir Heraclio and his army, lead by Sota, return and threaten to destroy the Kingdom.',
20 score: 0.5374966859817505
21 },
22 {
23 title: 'Forest Warrior',
24 fullplot: 'John McKenna is a spiritual being who is able to transform into bear, wolf or eagle. He lives in the forests of Tanglewood and has dedicated his life to protect them. One day a gang of evil lumberjacks led by Travis Thorne arrive Tanglewood to chop the forest down. McKenna cannot let this happen, and together with his new friends - Lords of the Tanglewood, a band of children who love to play in the forest - he battles against Thorne and his evil gang.',
25 score: 0.5331881642341614
26 },
27 {
28 title: 'Forest Warrior',
29 fullplot: 'John McKenna is a spiritual being who is able to transform into bear, wolf or eagle. He lives in the forests of Tanglewood and has dedicated his life to protect them. One day a gang of evil lumberjacks led by Travis Thorne arrive Tanglewood to chop the forest down. McKenna cannot let this happen, and together with his new friends - Lords of the Tanglewood, a band of children who love to play in the forest - he battles against Thorne and his evil gang.',
30 score: 0.5331881642341614
31 },
32 {
33 title: 'Catatan (Harian) si Boy',
34 fullplot: "A circle of friends risking their Friendship, Trust, Love and Hope in search of a legend. A young and privileged teenager with a golden heart, beset with challenges and tribulations we face today with the goal to open many young people's mind with inspirations and hopes that drive them in achieving their dreams. To get out of their comfort zone and finish what they started.",
35 score: 0.5322973728179932
36 },
37 {
38 title: 'Bionicle: Mask of Light',
39 fullplot: "In a land of living machines, two young ones are chosen to seek the legendary Mask of Light to reveal the savior of all the lands from the dark forces of the Makuta. During the course of their adventure, they will call on the heroes of their people, the great Toa. These Toa, masters of nature's forces such as Fire, Wind, Earth & Water, try to protect the chosen ones as they seek their destiny.",
40 score: 0.5315042734146118
41 },
42 {
43 title: 'Fear No Evil',
44 fullplot: 'High school student turns out to be personification of Lucifer. Two arch angels in human form (as women) take him on.',
45 score: 0.5295513868331909
46 },
47 {
48 title: 'Tales of Vesperia: The First Strike',
49 fullplot: 'In a mythical kingdom, the mighty Imperial Knights harness a magical substance known as Aer to power their weapons and protect humanity from the monsters of the forest. But something strange is afoot. The Aer is somehow changing, causing the wilderness to waste away and stirring the woodland beasts to attack with greater frequency. As danger creeps steadily closer to civilization, two young recruits - Flynn, the rigid son of a fallen hero, and the rebellious and brash Yuri - must ride with their fellow Imperial Knights to distant ruins in hopes of uncovering the truth behind the transforming Aer. Some will not survive the thrilling journey. Some will be betrayed. If Flynn and Yuri cannot overcome their differences and learn to fight together, all will be lost for the people of the realm.',
50 score: 0.5276793241500854
51 }
52]
1

例子

例如,创建一个名为 automated-embeddings-query.js 的文件。

touch automated-embeddings-query.js
2
1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas cluster
4const uri = "<CONNECTION-STRING>";
5
6const client = new MongoClient(uri);
7
8async function run() {
9 try {
10 await client.connect();
11
12 // set namespace
13 const database = client.db("<DATABASE-NAME>");
14 const coll = database.collection("<COLLECTION-NAME>");
15
16 // define pipeline
17 const agg = [
18 {
19 '$vectorSearch': {
20 'index': '<INDEX-NAME>',
21 'path': '<FIELD-NAME>',
22 'query': "<QUERY-TEXT>",
23 'numCandidates': <NUMBER-OF-CANDIDATES-TO-CONSIDER>,
24 'limit': <NUMBER-OF-DOCUMENTS-TO-RETURN>
25 }
26 }
27 ];
28 // run pipeline
29 const result = coll.aggregate(agg);
30
31 // print results
32 await result.forEach((doc) => console.dir(JSON.stringify(doc)));
33 } finally {
34 await client.close();
35 }
36}
37run().catch(console.dir);

例子

示例,在 automated-embeddings-query.js文件中,复制粘贴以下代码以定义针对 movies集合中的 fullplot字段的查询,以对语义上与 young heroes caught in epic struggles between light and darkness 相似的电影进行语义搜索。

1const { MongoClient } = require("mongodb");
2
3// connect to your Atlas cluster
4const uri = "<CONNECTION-STRING>";
5
6const client = new MongoClient(uri);
7
8async function run() {
9 try {
10 await client.connect();
11
12 // set namespace
13 const database = client.db("sample_mflix");
14 const coll = database.collection("movies");
15
16 // define pipeline
17 const agg = [
18 {
19 '$vectorSearch': {
20 'index': 'movies_automated_embeddings',
21 'path': 'fullplot',
22 'query': "young heroes caught in epic struggles between light and darkness",
23 'numCandidates': 1000,
24 'limit': 10
25 }
26 }, {
27 '$project': {
28 '_id': 0,
29 'fullplot': 1,
30 'title': 1,
31 'score': {
32 '$meta': 'vectorSearchScore'
33 }
34 }
35 }
36 ];
37 // run pipeline
38 const result = coll.aggregate(agg);
39
40 // print results
41 await result.forEach((doc) => console.dir(JSON.stringify(doc)));
42 } finally {
43 await client.close();
44 }
45}
46run().catch(console.dir);
3

<CONNECTION-STRING>

您对连接字符串集群。

<DATABASE-NAME>

包含该集合的数据库名称。

<COLLECTION-NAME>

包含索引字段的集合名称。

<INDEX-NAME>

索引名称。

<FIELD-NAME>

索引字段的名称。

<QUERY-TEXT>

用于生成嵌入并在语义搜索中使用的文本字符串。

<NUMBER-OF-CANDIDATES-TO-CONSIDER>

要考虑的最近邻数量。

<NUMBER-OF-DOCUMENTS-TO-RETURN>

结果中要返回的文档数。

4
node <FILE-NAME>.js

这里的 <FILE-NAME> 是您创建的 .js文件的名称。

例子

示例,要运行automated-embeddings-query.js文件中的示例查询,运行以下命令:

node automated-embeddings-query.js
'{"title":"Day Watch","fullplot":"Anton belongs to the Forces of the Light as well as his powerful girlfriend and apprentice, but his son is a powerful teenager from the Darkness and Anton protects him. When the balance between Light and Darkness is affected by the death of some evil vampires, Anton is framed and accused of the murders, and he chases an ancient chalk that has the power of changing the destiny of its owner.","score":0.5449697971343994}'
`{"title":"Dungeons & Dragons","fullplot":"The Empire of Izmer has long been a divided land. The Mages - an elite group of magic-users - rule whilst the lowly commoners are powerless. Izmer's young Empress, Savina, wants equality and prosperity for all, but the evil Mage Profion is plotting to depose her and establish his own rule. The Empress possesses a scepter which controls Izmer's Golden Dragons. To challenge her rule, Profion must have the scepter, and tricks the Council of Mages into believing Savina is unfit to hold it. Knowing that Profion will bring death and destruction to Izmer, Savina must find the legendary Rod of Savrille, a mythical rod that has the power to control Red Dragons, a species even mightier than the Gold. Enter two thieves, Ridley and Snails, who unwittingly become instrumental in Savina's search for the Rod. Joined by a feisty Dwarf named Elwood, and helped by the Empress's expert tracker, the Elf Norda, the young heroes go in search of the Rod of Savrille. From the deadly maze of the Thieves Guild at Antius to an Elven Village, secret grotto and abandoned castles, Ridley and his band must outwit Profion's chief henchman Damodar at every turn while, back in Izmer, Profion prepares to do battle with the Empress. All depends on the Rod, but the outcome of the race to reach it first is far from certain, and Izmar's very survival hangs in the balance.","score":0.5414832830429077}`
'{"title":"Brave Story","fullplot":"A young boy attempts to change his destiny by entering a magic gateway to another world; but on his quest to find the Tower of Fortune and be granted any wish, he must conjure up all his bravery to battle demons, his friends, and ultimately himself.","score":0.5404887795448303}'
'{"title":"Justin and the Knights of Valour","fullplot":"Justin lives in a kingdom where bureaucrats rule and knights have been ousted. His dream is to be become one of the Knights of Valour, like his grandfather was, but his father Reginald, the chief counsel to the Queen, wants his son to follow in his footsteps and become a lawyer. After an inspiring visit to his beloved Grandmother and bidding farewell to his supposed lady-love Lara, Justin leaves home and embarks on a quest to become a knight. Along the way he meets the beautiful, feisty Talia, a quirky wizard called Melquiades, and the handsome Sir Clorex and is mentored by three monks; Blucher, Legantir and Braulio, who teach and test him in the ancient ways of the Knights of Valour. Whilst an unlikely candidate for knighthood, Justin must rise to the challenge quickly when banished former knight Sir Heraclio and his army, lead by Sota, return and threaten to destroy the Kingdom.","score":0.5374966859817505}'
'{"title":"Forest Warrior","fullplot":"John McKenna is a spiritual being who is able to transform into bear, wolf or eagle. He lives in the forests of Tanglewood and has dedicated his life to protect them. One day a gang of evil lumberjacks led by Travis Thorne arrive Tanglewood to chop the forest down. McKenna cannot let this happen, and together with his new friends - Lords of the Tanglewood, a band of children who love to play in the forest - he battles against Thorne and his evil gang.","score":0.5331881642341614}'
'{"title":"Forest Warrior","fullplot":"John McKenna is a spiritual being who is able to transform into bear, wolf or eagle. He lives in the forests of Tanglewood and has dedicated his life to protect them. One day a gang of evil lumberjacks led by Travis Thorne arrive Tanglewood to chop the forest down. McKenna cannot let this happen, and together with his new friends - Lords of the Tanglewood, a band of children who love to play in the forest - he battles against Thorne and his evil gang.","score":0.5331881642341614}'
`{"title":"Catatan (Harian) si Boy","fullplot":"A circle of friends risking their Friendship, Trust, Love and Hope in search of a legend. A young and privileged teenager with a golden heart, beset with challenges and tribulations we face today with the goal to open many young people's mind with inspirations and hopes that drive them in achieving their dreams. To get out of their comfort zone and finish what they started.","score":0.5322973728179932}`
`{"title":"Bionicle: Mask of Light","fullplot":"In a land of living machines, two young ones are chosen to seek the legendary Mask of Light to reveal the savior of all the lands from the dark forces of the Makuta. During the course of their adventure, they will call on the heroes of their people, the great Toa. These Toa, masters of nature's forces such as Fire, Wind, Earth & Water, try to protect the chosen ones as they seek their destiny.","score":0.5315042734146118}`
'{"title":"Fear No Evil","fullplot":"High school student turns out to be personification of Lucifer. Two arch angels in human form (as women) take him on.","score":0.5295513868331909}'
'{"title":"Tales of Vesperia: The First Strike","fullplot":"In a mythical kingdom, the mighty Imperial Knights harness a magical substance known as Aer to power their weapons and protect humanity from the monsters of the forest. But something strange is afoot. The Aer is somehow changing, causing the wilderness to waste away and stirring the woodland beasts to attack with greater frequency. As danger creeps steadily closer to civilization, two young recruits - Flynn, the rigid son of a fallen hero, and the rebellious and brash Yuri - must ride with their fellow Imperial Knights to distant ruins in hopes of uncovering the truth behind the transforming Aer. Some will not survive the thrilling journey. Some will be betrayed. If Flynn and Yuri cannot overcome their differences and learn to fight together, all will be lost for the people of the realm.","score":0.5276793241500854}'
1

例子

例如,创建一个名为 automated-embeddings-query.js 的文件。

touch automated-embeddings-query.py
2
1import pymongo
2
3# connect to your Atlas cluster
4client = pymongo.MongoClient("<CONNECTION-STRING>")
5
6# define pipeline
7pipeline = [
8 {
9 '$vectorSearch': {
10 'index': '<INDEX-NAME>',
11 'path': '<FIELD-NAME>',
12 'query': '<QUERY-TEXT>',
13 'numCandidates': <NUMBER-OF-CANDIDATES-TO-CONSIDER>,
14 'limit': <NUMBER-OF-DOCUMENTS-TO-RETURN>
15 }
16 }
17]
18
19# run pipeline
20result = client["<DATABASE-NAME>"]["<COLLECTION-NAME>"].aggregate(pipeline)
21
22# print results
23for i in result:
24 print(i)
25

例子

示例,在 automated-embeddings-query.py文件中,复制粘贴以下代码以定义针对 movies集合中的 fullplot字段的查询,以对语义上与 young heroes caught in epic struggles between light and darkness 相似的电影进行语义搜索。

1import pymongo
2
3# connect to your Atlas cluster
4client = pymongo.MongoClient("<CONNECTION-STRING>")
5
6# define pipeline
7pipeline = [
8 {
9 '$vectorSearch': {
10 'index': 'movies_automated_embeddings',
11 'path': 'fullplot',
12 'query': "young heroes caught in epic struggles between light and darkness",
13 'numCandidates': 1000,
14 'limit': 10
15 },
16 },
17 {
18 '$project': {
19 '_id': 0,
20 'title': 1,
21 'fullplot': 1,
22 'score': {'$meta': 'vectorSearchScore'}
23 }
24 }
25]
26
27# run pipeline
28result = client["sample_mflix"]["movies"].aggregate(pipeline)
29
30# print results
31for i in result:
32 print(i)
33
3

<CONNECTION-STRING>

您对连接字符串集群。

<DATABASE-NAME>

包含该集合的数据库名称。

<COLLECTION-NAME>

包含索引字段的集合名称。

<INDEX-NAME>

索引名称。

<FIELD-NAME>

索引字段的名称。

<QUERY-TEXT>

用于生成嵌入并在语义搜索中使用的文本字符串。

<NUMBER-OF-CANDIDATES-TO-CONSIDER>

要考虑的最近邻数量。

<NUMBER-OF-DOCUMENTS-TO-RETURN>

结果中要返回的文档数。

4
python <FILE-NAME>.py

这里的 <FILE-NAME> 是您创建的 .py文件的名称。

例子

示例,要运行automated-embeddings-query.py文件中的示例查询,运行以下命令:

python automated-embeddings-query.js
{'title': 'Day Watch', 'fullplot': 'Anton belongs to the Forces of the Light as well as his powerful girlfriend and apprentice, but his son is a powerful teenager from the Darkness and Anton protects him. When the balance between Light and Darkness is affected by the death of some evil vampires, Anton is framed and accused of the murders, and he chases an ancient chalk that has the power of changing the destiny of its owner.', 'score': 0.5449697971343994}
{'title': 'Dungeons & Dragons', 'fullplot': "The Empire of Izmer has long been a divided land. The Mages - an elite group of magic-users - rule whilst the lowly commoners are powerless. Izmer's young Empress, Savina, wants equality and prosperity for all, but the evil Mage Profion is plotting to depose her and establish his own rule. The Empress possesses a scepter which controls Izmer's Golden Dragons. To challenge her rule, Profion must have the scepter, and tricks the Council of Mages into believing Savina is unfit to hold it. Knowing that Profion will bring death and destruction to Izmer, Savina must find the legendary Rod of Savrille, a mythical rod that has the power to control Red Dragons, a species even mightier than the Gold. Enter two thieves, Ridley and Snails, who unwittingly become instrumental in Savina's search for the Rod. Joined by a feisty Dwarf named Elwood, and helped by the Empress's expert tracker, the Elf Norda, the young heroes go in search of the Rod of Savrille. From the deadly maze of the Thieves Guild at Antius to an Elven Village, secret grotto and abandoned castles, Ridley and his band must outwit Profion's chief henchman Damodar at every turn while, back in Izmer, Profion prepares to do battle with the Empress. All depends on the Rod, but the outcome of the race to reach it first is far from certain, and Izmar's very survival hangs in the balance.", 'score': 0.5414832830429077}
{'title': 'Brave Story', 'fullplot': 'A young boy attempts to change his destiny by entering a magic gateway to another world; but on his quest to find the Tower of Fortune and be granted any wish, he must conjure up all his bravery to battle demons, his friends, and ultimately himself.', 'score': 0.5404887795448303}
{'title': 'Justin and the Knights of Valour', 'fullplot': 'Justin lives in a kingdom where bureaucrats rule and knights have been ousted. His dream is to be become one of the Knights of Valour, like his grandfather was, but his father Reginald, the chief counsel to the Queen, wants his son to follow in his footsteps and become a lawyer. After an inspiring visit to his beloved Grandmother and bidding farewell to his supposed lady-love Lara, Justin leaves home and embarks on a quest to become a knight. Along the way he meets the beautiful, feisty Talia, a quirky wizard called Melquiades, and the handsome Sir Clorex and is mentored by three monks; Blucher, Legantir and Braulio, who teach and test him in the ancient ways of the Knights of Valour. Whilst an unlikely candidate for knighthood, Justin must rise to the challenge quickly when banished former knight Sir Heraclio and his army, lead by Sota, return and threaten to destroy the Kingdom.', 'score': 0.5374966859817505}
{'title': 'Forest Warrior', 'fullplot': 'John McKenna is a spiritual being who is able to transform into bear, wolf or eagle. He lives in the forests of Tanglewood and has dedicated his life to protect them. One day a gang of evil lumberjacks led by Travis Thorne arrive Tanglewood to chop the forest down. McKenna cannot let this happen, and together with his new friends - Lords of the Tanglewood, a band of children who love to play in the forest - he battles against Thorne and his evil gang.', 'score': 0.5331881642341614}
{'title': 'Forest Warrior', 'fullplot': 'John McKenna is a spiritual being who is able to transform into bear, wolf or eagle. He lives in the forests of Tanglewood and has dedicated his life to protect them. One day a gang of evil lumberjacks led by Travis Thorne arrive Tanglewood to chop the forest down. McKenna cannot let this happen, and together with his new friends - Lords of the Tanglewood, a band of children who love to play in the forest - he battles against Thorne and his evil gang.', 'score': 0.5331881642341614}
{'title': 'Catatan (Harian) si Boy', 'fullplot': "A circle of friends risking their Friendship, Trust, Love and Hope in search of a legend. A young and privileged teenager with a golden heart, beset with challenges and tribulations we face today with the goal to open many young people's mind with inspirations and hopes that drive them in achieving their dreams. To get out of their comfort zone and finish what they started.", 'score': 0.5322973728179932}
{'title': 'Bionicle: Mask of Light', 'fullplot': "In a land of living machines, two young ones are chosen to seek the legendary Mask of Light to reveal the savior of all the lands from the dark forces of the Makuta. During the course of their adventure, they will call on the heroes of their people, the great Toa. These Toa, masters of nature's forces such as Fire, Wind, Earth & Water, try to protect the chosen ones as they seek their destiny.", 'score': 0.5315042734146118}
{'title': 'Fear No Evil', 'fullplot': 'High school student turns out to be personification of Lucifer. Two arch angels in human form (as women) take him on.', 'score': 0.5295513868331909}
{'title': 'Tales of Vesperia: The First Strike', 'fullplot': 'In a mythical kingdom, the mighty Imperial Knights harness a magical substance known as Aer to power their weapons and protect humanity from the monsters of the forest. But something strange is afoot. The Aer is somehow changing, causing the wilderness to waste away and stirring the woodland beasts to attack with greater frequency. As danger creeps steadily closer to civilization, two young recruits - Flynn, the rigid son of a fallen hero, and the rebellious and brash Yuri - must ride with their fellow Imperial Knights to distant ruins in hopes of uncovering the truth behind the transforming Aer. Some will not survive the thrilling journey. Some will be betrayed. If Flynn and Yuri cannot overcome their differences and learn to fight together, all will be lost for the people of the realm.', 'score': 0.5276793241500854}

在此页面上