文档菜单
文档首页
/ / /
C 驱动程序
/ /

Atlas 搜索索引

本页内容

  • 概述
  • 创建搜索索引
  • 列出搜索索引
  • 更新搜索索引
  • 删除搜索索引
  • 附加信息
  • API 文档

Atlas Search 允许您在 MongoDB Atlas 上托管的集合中执行全文搜索。Atlas Search 索引指定了搜索行为以及要索引的字段。

以下各节提供了代码示例,演示了如何创建、列出、更新和删除 Atlas Search 索引。

您可以将createSearchIndexes 命令传递给 mongoc_collection_command_simple() 函数以创建一个或多个 Atlas Search 索引。

您还可以使用此函数创建 Atlas 向量搜索索引。Atlas 向量搜索允许您在 MongoDB Atlas 中存储的向量嵌入上进行语义搜索。有关此功能的更多信息,请参阅 Atlas 文档中的 Atlas 向量搜索概述

以下代码示例演示了如何创建 Atlas 搜索索引

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"createSearchIndexes" : "%s",
"indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<index name>"} ]
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully created search index\n");
} else {
fprintf (stderr, "Failed to create search index: %s", error.message);
}
bson_destroy (&cmd);

以下代码示例演示了如何创建多个索引

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"createSearchIndexes" : "%s",
"indexes" : [ {"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<first index name>"},
{"definition" : {"mappings" : {"dynamic" : false}}, "name" : "<second index name>"} ]
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully created search indexes\n");
} else {
fprintf (stderr, "Failed to create search indexes: %s", error.message);
}
bson_destroy (&cmd);

有关定义 Atlas Search 索引使用的语法的更多信息,请参阅 Atlas 文档中的 审查 Atlas Search 索引语法 指南。

您可以将$listSearchIndexes聚合阶段传递给mongoc_collection_aggregate()函数,以返回集合中所有Atlas搜索索引。

以下代码示例展示了如何打印集合中搜索索引的列表

bson_t pipeline;
const bson_t *doc;
bson_error_t error;
const char *pipeline_str = BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
bson_init_from_json (&pipeline, pipeline_str, -1, &error);
mongoc_cursor_t *cursor =
mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL);
while (mongoc_cursor_next (cursor, &doc)) {
char *str = bson_as_canonical_extended_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (&pipeline);
mongoc_cursor_destroy (cursor);

您可以将updateSearchIndex命令传递给mongoc_collection_command_simple()函数以更新Atlas搜索索引。

以下代码展示了如何更新搜索索引

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"updateSearchIndex" : "%s",
"definition" : {"mappings" : {"dynamic" : true}}, "name" : "<index name>"}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
bson_free (cmd_str);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully updated search index\n");
} else {
fprintf (stderr, "Failed to create search index: %s", error.message);
}
bson_destroy (&cmd);

您可以将dropSearchIndexes命令传递给mongoc_collection_command_simple()函数以删除Atlas搜索索引。

以下代码展示了如何从集合中删除搜索索引

bson_t cmd;
bson_error_t error;
char *cmd_str = bson_strdup_printf (
BSON_STR ({
"dropSearchIndexes" : "%s",
"index" : "<index name>"
}),
"<collection name>");
bson_init_from_json (&cmd, cmd_str, -1, &error);
if (mongoc_collection_command_simple (collection, &cmd, NULL, NULL, &error)) {
printf ("Successfully deleted search index\n");
} else {
fprintf (stderr, "Failed to delete search index: %s", error.message);
}
bson_destroy (&cmd);

要了解更多关于MongoDB Atlas Search的信息,请参阅Atlas Search 索引文档。

要了解更多关于本指南中讨论的任何函数,请参阅以下API文档

返回

多键索引