对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

MongoDB搜索索引

MongoDB搜索功能使您能够对Atlas上托管的集合执行全文搜索。在执行MongoDB 搜索查询之前,您必须创建索引,指定要索引的字段以及索引的方式。

要了解有关 MongoDB 搜索的更多信息,请参阅 MongoDB 搜索文档中的 MongoDB 搜索概述

The examples in this guide use the movies collection in the sample_mflix database from the Atlas sample datasets. To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the MongoDB Get Started guide.

要从C++应用程序管理sample_mflix集合上的MongoDB搜索索引,请先实例化连接到Atlas集群的mongocxx::client,然后为dbcollection变量分配以下值:

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():使用指定配置创建MongoDB 搜索索引

  • create_many():使用指定配置创建多个MongoDB 搜索索引

  • list():返回一个 mongocxx::cursor实例,该实例指向集合上的MongoDB 搜索索引列表

  • update_one():更新指定MongoDB搜索索引的定义

  • drop_one() :从集合中删除指定索引

注意

MongoDB 搜索索引管理是异步的

MongoDB C++驱动程序异步管理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搜索索引,请先创建一个包含 mappings 字段的 definition 文档。在 mappings字段中,指定包含以下字段和值的文档:

  • dynamic:设置为false

  • fields:指定要索引的字段名称及其索引配置的文档。要学习;了解有关 mappings.fields 选项的更多信息,请参阅MongoDB Search 文档中的静态映射示例

然后,将 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;
New index name: myStaticIndex

重要

您无法对字段名称开头包含美元 ($) 符号的字段创建索引。

要了解有关何时使用静态映射的更多信息,请参阅 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;
New index name: myDynamicIndex

要学习;了解有关何时使用动态映射的更多信息,请参阅 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;
}
New index names:
myDynamicIndex
myStaticIndex

要列出集合上的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;
}
{ "id" : ..., "name" : "myDynamicIndex", "type" : "search", ...,
"latestDefinition" : { "mappings" : { "dynamic" : true } }, ...}
{ "id" : ..., "name" : "myStaticIndex", "type" : "search", ...,
"latestDefinition" : { "mappings" : { "dynamic" : false, "fields" : { "title" : { "type" : "string", "analyzer" : "lucene.standard" }, "year" : { "type" : "number" } } } }, ...}

或者,您可以通过将索引名称传递到 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;
}
{ "id" : ..., "name" : "myDynamicIndex", "type" : "search", ...,
"latestDefinition" : { "mappings" : { "dynamic" : true } }, ...}

要更新 MongoDB 搜索索引,请在 mongocxx::search_index_view 实例上调用 update_one() 方法,并传入要更新的搜索索引名称以及要更新到的搜索索引的定义。

以下示例展示了如何更新静态映射创建搜索索引中的MongoDB搜索索引,以在 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 搜索索引,请在 mongocxx::search_index_view实例上调用 drop_one() 方法并传入要删除的索引的名称。

以下示例展示如何删除名为 myDynamicIndex 的MongoDB搜索索引:

siv.drop_one("myDynamicIndex");

有关演示如何在C++驱动程序中管理索引的示例应用程序,请参阅:ref:`索引 <cpp-indexes>`指南。

有关如何使用MongoDB搜索功能和定义MongoDB 搜索索引的更详细指南,请参阅以下MongoDB 搜索文档页面:

要学习;了解有关本指南中讨论的方法的更多信息,请参阅以下API文档: