您可以将MongoDB Vector Search 配置为自动生成和管理集群中文本数据的向量嵌入。您可以在 M10 或更高版本的Atlas 集群中创建一键式AI语义搜索索引,并使用 Voyage AI嵌入模型,从而通过向量简化索引、更新和查询。
启用自动嵌入后, MongoDB Vector Search 会在索引时为Atlas集合中的指定文本字段使用指定的嵌入模型自动生成嵌入,并在查询时为针对自动嵌入的索引字段查询中的文本字符串自动生成嵌入。
Considerations
重要
您可以在云提供商的任何 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 索引
以下部分描述了用于启用自动生成文本字段嵌入的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 }
索引字段
索引定义中需要以下字段:
字段 | 类型 | 说明 |
| 字符串 | 字段的类型。对于自动嵌入,该值必须为 |
| 字符串 | 集合中您想要为自动嵌入编制索引的字段的名称。 |
| 字符串 | 用于为索引生成嵌入的 Voyage AI 嵌入模型。您可以指定以下模型之一:
创建索引后,如果您随后更改嵌入模型, MongoDB Vector Search 会为数据集生成新的嵌入。在MongoDB Vector Search 生成嵌入时,您可以使用旧的嵌入继续进行查询。当旧的嵌入替换为新的嵌入模型中的嵌入时, MongoDB Vector Search 会删除旧的嵌入。 |
Considerations
自动嵌入的索引字段与以下 vector 类型索引字段互斥:
numDimensionssimilarityquantization
如果您的集合已经包含嵌入,则必须使用 vector 类型字段来为嵌入创建索引。要了解有关使用嵌入对字段进行索引的更多信息,请参阅如何对 Vector Search 字段进行索引。
如果您想要为自动生成嵌入的文本字段建立索引,并使用您自己的嵌入为字段索引,则可以同时使用 text 和 vector 类型创建索引。MongoDB Vector Search 将仅自动为针对索引为 text 类型的字段的查询生成嵌入。您必须在查询中指定嵌入,才能搜索索引为 vector 类型的字段。
您还可以使用MongoDB Vector Search filter 类型对字段进行索引以预筛选数据。预筛选可减少要运行相似性比较的文档数量,从而减少查询延迟并提高搜索结果的准确性。要学习;了解有关预过滤数据的更多信息,请参阅关于 filter 类型。
为自动化嵌入创建索引
以下过程将演练在MongoDB Vector Search索引中启用自动嵌入的步骤。如果加载了 sample_mflix.movies 数据集,则过程中的示例将演示如何为集合中的 fullplot字段启用自动嵌入。
AtlasGoClusters在Atlas中,Go项目的 页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含所需项目的组织。
如果尚未显示,请从导航栏的Projects菜单中选择所需的项目。
在侧边栏中,单击 Database 标题下的 Clusters。
会显示集群页面。
在Atlas中,转到集群的Search & Vector Search 页面。
您可以从 Search & Vector Search 选项或 Data Explorer 转到MongoDB搜索页面。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Search & Vector Search。
如果没有集群,则请单击 Create cluster 来创建一个。要了解更多信息,请参阅创建集群。
如果您的项目有多个集群,请从 Select cluster 下拉列表中选择要使用的集群,然后单击 Go to Atlas Search。
如果尚未显示,请从导航栏上的 Organizations 菜单中选择包含项目的组织。
如果尚未显示,请从导航栏的 Projects 菜单中选择您的项目。
在侧边栏中,单击 Database 标题下的 Data Explorer。
展开数据库并选择集合。
单击该集合的 Indexes 标签页。
将显示 Atlas Search 页面。
开始您的索引配置。
在页面上进行以下选择,然后单击 Next。
Search Type | 选择 Vector Search 索引类型。 |
Index Name and Data Source | 指定以下信息:
|
Configuration Method | 选择 JSON Editor 。 |
例子
例如,输入 movies_automated_embeddings 作为索引名称,然后从 Database and Collection 下拉菜单中选择 sample_mflix 和 movies。
定义索引。
在 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 }
使用 mongosh 连接到 Atlas 集群。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
运行 db.collection.createSearchIndex() 方法。
db.collection.createSearchIndex() 方法使用的语法如下:
1 db.<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 模型来创建支持自动嵌入的索引,请在终端中运行以下命令:
1 db.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 );
在.js 文件中定义索引。
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<CONNECTION-STRING>"; 5 const client = new MongoClient(uri); 6 7 async 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 } 32 run().catch(console.dir);
例子
示例,要通过对 sample_mflix.movies命名空间中的 fullplot字段使用 voyage-3-large 模型来创建支持自动嵌入的索引,请将以下内容复制并粘贴到 create_index.js文件中:
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas deployment 4 const uri = "<CONNECTION-STRING>"; 5 const client = new MongoClient(uri); 6 7 async 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 } 32 run().catch(console.dir);
在.py 文件中定义索引。
1 from pymongo import MongoClient 2 from pymongo.operations import SearchIndexModel 3 4 client = pymongo.MongoClient("<CONNECTION-STRING>") 5 db = client["<DATABASE-NAME>"] 6 collection = db["<COLLECTION-NAME>"] 7 8 search_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 ) 21 result = collection.create_search_index(model=search_index_model) 22 print(result)
例子
示例,要通过对 sample_mflix.movies命名空间中的 fullplot字段使用 voyage-3-large 模型来创建支持自动嵌入的索引,请将以下内容复制并粘贴到 create_index.py文件中:
1 from pymongo import MongoClient 2 from pymongo.operations import SearchIndexModel 3 4 client = MongoClient("<CONNECTION-STRING>") 5 db = client["sample_mflix"] 6 collection = db["movies"] 7 8 search_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 ) 21 result = collection.create_search_index(model=search_index_model) 22 print(result)
具有自动嵌入功能的MongoDB Vector Search Query
创建具有自动嵌入功能的索引后,您可以对索引字段运行文本查询。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查询需要以下字段:
字段 | 类型 | 必要性 | 说明 |
|---|---|---|---|
| 布尔 | 可选的 | 如果省略 指定运行ENN 还是 ANN搜索的标志。值可以是以下之一:
如果省略,则默认值为 |
| 字符串 | 必需 | 要使用的MongoDB Vector Search索引的名称。 如果索引名称拼写错误或者指定的索引在集群上尚不存在,MongoDB Vector Search 不会返回结果。 |
| 数字 | 必需 | 要在结果中返回的文档数量(仅限 |
| 数字 | 可选的 | 如果 搜索期间要使用的最近邻数量。值必须小于或等于 ( |
| 字符串 | 必需 | 要搜索的索引向量类型字段。 |
| 字符串 | 必需 | 用于自动生成嵌入并执行语义搜索的文本。 |
Considerations
您可以对索引字段运行近似最近邻 (ANN) 或精确最近邻 (ENN) 查询。如需了解更多信息,请参阅近似最近邻 (ANN) 搜索和精确最近邻 (ENN) 搜索。
您不能在查询中针对为自动嵌入建立索引的字段指定向量嵌入。相反,您必须对该字段运行自然语言查询。当您针对自动嵌入的索引字段运行自然语言查询时, MongoDB Vector Search 使用与索引字段相同的嵌入模型自动为查询文本生成嵌入。然后,它使用生成的嵌入对索引字段执行语义搜索。
您可以选择在查询中指定 filter 字段,以对MongoDB Vector Search 执行语义搜索的文档进行预筛选。要学习;了解更多信息,请参阅MongoDB Vector Search 预筛选。
您还可以选择检索结果中文档的分数。要学习;了解更多信息,请参阅MongoDB Vector Search Score。
使用自动嵌入运行查询
您无法在Atlas用户界面中运行MongoDB Vector Search 查询。使用 mongosh 或支持的驾驶员运行查询。
使用 mongosh 连接到 Atlas 集群。
要学习;了解更多信息,请参阅通过mongosh连接到集群。
db.collection.aggregate()在$vectorSearch 阶段运行 方法。
db.collection.aggregate() 方法使用的语法如下:
1 db.<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 相似的电影进行语义搜索,请复制、粘贴并运行以下查询。
1 db.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 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas cluster 4 const uri = "<CONNECTION-STRING>"; 5 6 const client = new MongoClient(uri); 7 8 async 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 } 37 run().catch(console.dir);
例子
示例,在 automated-embeddings-query.js文件中,复制粘贴以下代码以定义针对 movies集合中的 fullplot字段的查询,以对语义上与 young heroes caught in epic struggles
between light and darkness 相似的电影进行语义搜索。
1 const { MongoClient } = require("mongodb"); 2 3 // connect to your Atlas cluster 4 const uri = "<CONNECTION-STRING>"; 5 6 const client = new MongoClient(uri); 7 8 async 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 } 46 run().catch(console.dir);
运行此查询。
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 import pymongo 2 3 # connect to your Atlas cluster 4 client = pymongo.MongoClient("<CONNECTION-STRING>") 5 6 # define pipeline 7 pipeline = [ 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 20 result = client["<DATABASE-NAME>"]["<COLLECTION-NAME>"].aggregate(pipeline) 21 22 # print results 23 for i in result: 24 print(i) 25
例子
示例,在 automated-embeddings-query.py文件中,复制粘贴以下代码以定义针对 movies集合中的 fullplot字段的查询,以对语义上与 young heroes caught in epic struggles
between light and darkness 相似的电影进行语义搜索。
1 import pymongo 2 3 # connect to your Atlas cluster 4 client = pymongo.MongoClient("<CONNECTION-STRING>") 5 6 # define pipeline 7 pipeline = [ 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 28 result = client["sample_mflix"]["movies"].aggregate(pipeline) 29 30 # print results 31 for i in result: 32 print(i) 33
运行此查询。
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}