Overview
MongoDB搜索功能使您能够对Atlas上托管的集合执行全文搜索。在执行MongoDB 搜索查询之前,您必须创建索引,指定要索引的字段以及索引的方式。
要了解有关 MongoDB Search 的更多信息,请参阅 MongoDB Search 文档中的 MongoDB Search 概述。
样本数据
本指南中的示例使用来自 Atlas 示例数据集的 sample_mflix 数据库中的 movies 集合。要学习如何创建免费的MongoDB Atlas 集群并加载示例数据集,请参阅MongoDB 入门指南。
要从C++应用程序管理sample_mflix集合上的MongoDB搜索索引,请先实例化连接到Atlas集群的mongocxx::client,然后为db和collection变量分配以下值:
auto db = client["sample_mflix"]; auto collection = db["movies"];
然后,对 collection 变量调用 search_indexes() 方法,在集合上实例化 mongocxx::search_index_view:
auto siv = collection.search_indexes();
mongocxx::search_index_view 类包含以下允许您与 MongoDB 搜索索引进行交互的节点函数:
create_one(): Creates a MongoDB Search index with the specified configuration
create_many():使用指定配置创建多个MongoDB 搜索索引
list(): Returns a
mongocxx::cursorinstance that points to a list of MongoDB Search indexes on the collectionupdate_one(): Updates the definition of the specified MongoDB Search index
drop_one() :从集合中删除指定索引
注意
MongoDB 搜索索引管理是异步的
MongoDB C++驱动程序异步管理MongoDB 搜索索引。以下部分中描述的库方法会立即返回服务器响应,但对MongoDB搜索索引的更改会在背景进行,可能要稍后一段时间才能完成。
以下部分提供的代码示例演示了如何使用每种MongoDB搜索索引管理方法。
创建MongoDB搜索索引
要在集合上创建单个MongoDB搜索索引,请在 mongocxx::search_index_view 实例上调用 create_one() 方法,并传入指定要创建索引的 mongoxcc::search_index_model 实例。
mongocxx::search_index_model 构造函数需要以下参数:
name:指定索引索引的字符串definition:包含mappings字段的文档,指定如何配置索引中的字段
提示
有关可在MongoDB搜索索引中配置的字段的完整列表,请参阅MongoDB Search文档中的查看MongoDB搜索索引语法指南。
以下部分介绍如何创建使用静态或动态映射的单个MongoDB搜索索引。
使用静态映射创建MongoDB搜索索引
在MongoDB 搜索索引中使用静态映射来指定要索引的字段,并为各个字段配置索引选项。
要创建使用静态映射的单个MongoDB搜索索引,请先创建一个包含 mappings 字段的 definition 文档。在 mappings字段中,指定包含以下字段和值的文档:
dynamic:设置为false。fields: Document specifying the names of the fields that you want to index and their index configurations. To learn more aboutmappings.fieldsoptions, see the Static Mapping Example in the MongoDB Search documentation.
然后,将 name string 和 definition 文档传递给 mongocxx::search_index_model 构造函数以实例化 mongocxx::search_index_model。将此 mongocxx::search_index_model 实例传递给 create_one() 方法,以将指定的 MongoDB 搜索索引 添加到您的集合中。
以下示例展示了如何创建使用静态映射的单个MongoDB搜索索引:
// Create an index model with your index name and definition containing the fields you want to index auto name = "myStaticIndex"; auto fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.standard"))), kvp("year", make_document(kvp("type","number")))); auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", fields)))); auto model = mongocxx::search_index_model(name, definition.view()); // Create the search index auto result = siv.create_one(model); std::cout << "New index name: " << result << std::endl;
重要
您无法对字段名称开头包含美元 ($) 符号的字段创建索引。
要了解有关何时使用静态映射的更多信息,请参阅 MongoDB 搜索文档中的静态映射部分。
使用动态映射创建MongoDB搜索索引
在MongoDB 搜索索引中使用动态映射,自动为受支持类型的所有字段索引。有关支持的BSON数据类型的列表,请参阅MongoDB搜索文档中的数据类型部分。
要创建使用动态映射的单个MongoDB 搜索索引,请先创建一个包含 mappings字段的 definition文档。在 mappings字段中,指定包含 dynamic字段的文档,并将其值设立为 true。然后,将 name string 和 definition 文档传递给 mongocxx::search_index_model 构造函数以实例化 mongocxx::search_index_model。将此 mongocxx::search_index_model 实例传递给 create_one() 方法,以将指定的 MongoDB 搜索索引添加到集合中。
The following example shows how to to create a single MongoDB Search index that uses dynamic mappings:
// Create an index model with your index name and definition auto name = "myDynamicIndex"; auto definition = make_document(kvp("mappings", make_document(kvp("dynamic", true)))); auto model = mongocxx::search_index_model(name, definition.view()); // Create the search index auto result = siv.create_one(model); std::cout << "New index name: " << result << std::endl;
要学习;了解有关何时使用动态映射的更多信息,请参阅 MongoDB 搜索文档中的动态映射部分。
创建多个MongoDB搜索索引
要创建多个MongoDB搜索索引,请对 mongocxx::search_index_view 实例调用 create_many() 方法,并传入指定要创建的MongoDB搜索索引的 mongocxx::search_index_model 实例向量。
以下示例展示了如何创建多个MongoDB搜索索引:
// Create a vector to store Search index models std::vector<mongocxx::search_index_model> models; // Add an index model with dynamic mappings to the input vector auto name_1 = "myDynamicIndex"; auto definition_1 = make_document(kvp("mappings", make_document(kvp("dynamic", true)))); auto model_1 = mongocxx::search_index_model(name_1, definition_1.view()); models.push_back(model_1); // Add an index model with static mappings to the input vector auto name_2 = "myStaticIndex"; auto fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.standard"))), kvp("year", make_document(kvp("type","number")))); auto definition_2 = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", fields)))); auto model_2 = mongocxx::search_index_model(name_2, definition_2.view()); models.push_back(model_2); // Create the search indexes auto result = siv.create_many(models); // Print the search index names std::cout << "New index names:" << std::endl; for (const std::string& name : result) { std::cout << name << std::endl; }
列出MongoDB 搜索索引
要列出集合上的MongoDB 搜索索引,请在 mongocxx::search_index_view实例上调用 list() 方法。此方法返回一个 mongocxx::cursor 实例,您可以使用该实例迭代集合的MongoDB搜索索引。
以下示例通过迭代指向创建多个MongoDB搜索索引:中索引的 cursor::iterator 实例来打印MongoDB搜索索引列表:
auto cursor = siv.list(); for (mongocxx::cursor::iterator it = cursor.begin(); it != cursor.end(); ++it) { std::cout << bsoncxx::to_json(*it) << std::endl; }
或者,您可以通过将索引名称传递到 list() 方法来列出特定的MongoDB搜索索引。这将返回一个 mongocxx::cursor 实例,该实例包含仅含指定索引的结果集的点。
以下示例使用 list() 方法打印名称为 myDynamicIndex 的索引:
auto cursor = siv.list("myDynamicIndex"); for (mongocxx::cursor::iterator it = cursor.begin(); it != cursor.end(); ++it) { std::cout << bsoncxx::to_json(*it) << std::endl; }
更新 MongoDB 搜索索引
要更新 MongoDB 搜索索引,请在 mongocxx::search_index_view 实例上调用 update_one() 方法,并传入要更新的搜索索引名称以及要更新到的搜索索引的定义。
以下示例展示了如何更新使用静态映射创建搜索索引中的 MongoDB Search 索引,以在 title 字段上使用简单分析器:
auto update_fields = make_document(kvp("title", make_document(kvp("type", "string"), kvp("analyzer","lucene.simple"))), kvp("year", make_document(kvp("type","number")))); auto update_definition = make_document(kvp("mappings", make_document(kvp("dynamic", false), kvp("fields", update_fields)))); siv.update_one("myStaticIndex", update_definition.view());
删除MongoDB搜索索引
要从集合中删除MongoDB 搜索索引,请在 mongocxx::search_index_view实例上调用 drop_one() 方法并传入要删除的索引的名称。
以下示例展示如何删除名为 myDynamicIndex 的MongoDB搜索索引:
siv.drop_one("myDynamicIndex");
更多信息
要查看演示如何管理索引的可运行示例,请参阅使用索引优化查询。
有关如何使用MongoDB搜索功能和定义MongoDB 搜索索引的更详细指南,请参阅以下MongoDB 搜索文档页面:
API 文档
要学习;了解有关本指南中讨论的方法的更多信息,请参阅以下API文档: