Docs 菜单
Docs 主页
/
MongoDB Atlas
/ /

查看 Atlas Search 索引

在此页面上

  • 必需的访问权限
  • 步骤
  • 搜索索引
  • 审查索引状态
  • 查看状态详情

重要

如果您使用 $out 聚合阶段修改具有 Atlas Search 索引的集合,则必须删除并重新创建搜索索引。如果可能,请考虑使用 $merge 而不是 $out

您可以在 Index Overview页面查看 Atlas Search 搜索索引定义。对于每个索引, Index Overview页面显示以下内容:

  • 索引的命名空间

  • 索引和搜索 分析器,如索引定义中所指定。

  • 动态或静态映射。

  • 索引定义中的字段映射(如有),包括字段名称、数据类型以及是否为单个字段启用动态映射。

本页介绍如何在Index Overview页面中查看索引定义。 您还可以在 页面中使用可视化编辑器或 JSON 编辑器 编辑 Atlas Search IndexIndex Overview

下表列出了每个角色支持的访问模式。

角色
操作
Atlas UI
Atlas API
Atlas Search API
Atlas CLI

Project Data Access Read Only 或更高级别的角色

用于查看 Atlas Search 分析器和索引。

Project Data Access Admin 或更高级别的角色

创建和管理 Atlas Search 分析器和索引,并为您的 API 密钥分配角色。

为 API 密钥创建访问列表条目,并从显示在 API 密钥访问列表中的客户端发送请求。

使用 Atlas 用户界面或 API 创建、查看、编辑和删除 Atlas Search 索引。

您可以在 Atlas 用户界面中检索 Atlas Search 索引,也可以使用您首选语言的mongosh 、Atlas CLI、 API或支持的MongoDB 驱动程序以编程方式检索。

注意

您可以使用mongosh 命令或驾驶员辅助方法检索所有AtlasAtlas Search Atlas集群层上的Atlas Search索引。有关支持的驾驶员版本的列表,请参阅MongoDB驱动程序。

您必须至少拥有对包含索引的数据库的 readAnyDatabase 角色或 read 访问权限。如需了解更多信息,请参阅内置角色特定权限


➤ 使用选择语言下拉菜单设置本节中示例的语言。


要通过 API 检索 Atlas Search 索引,请执行以下操作:

1

使用您想要检索的 Atlas Search 索引的唯一 ID 或名称向 search/indexes/ 端点发送一个 GET 请求。

curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ --header "Accept: application/json" \
--include \
--request GET "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{indexId}"

要进一步了解任一端点的语法和参数,请参阅按名称获取一个按 ID 获取一个。

2

要检索集合的所有 Atlas Search 索引,请执行以下操作:

1

search/indexes/ 端点发送 GET 请求,其中包含要检索其索引的集合的名称。

curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ --header "Accept: application/json" \
--include \
--request GET "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes/{databaseName}/{collectionName}"
2

要检索集群的所有 Atlas Search 索引:

1

要检索集群的索引,请将 GET 请求发送到 search/indexes/ 端点,该请求包含要检索其索引的集群的名称。

curl --user "{PUBLIC-KEY}:{PRIVATE-KEY}" --digest \ --header "Accept: application/json" \
--include \
--request GET "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/clusters/{clusterName}/search/indexes"
2

要使用 Atlas CLI 列出集群的所有搜索索引,请运行以下命令:

atlas clusters search indexes list [options]

要使用 Atlas CLI 返回您指定的搜索索引的详细信息,请运行以下命令:

atlas clusters search indexes describe <indexId> [options]

要了解有关上述命令的语法和参数的更多信息,请参阅Atlas CLI Atlas集群Atlas Search 索引列表Atlas 集群Atlas Search 索引描述 的 文档。

提示

请参阅:相关链接

要使用 Atlas CLI 描述指定部署的指定搜索索引,请运行以下命令:

atlas deployments search indexes describe [indexId] [options]

要使用 Atlas CLI 列出指定部署的所有搜索索引,请运行以下命令:

atlas deployments search indexes list [options]

要进一步了解上一个命令的语法和参数,请参阅 Atlas CLI 文档 Atlas 部署搜索索引说明 Atlas 部署搜索索引列表。

提示

请参阅:相关链接

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

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

  3. 如果尚未出现,请单击侧边栏中的 Clusters(集群)。

    会显示集群页面。

2

您可以从侧边栏、 Data Explorer 或集群详细信息页面转到 Atlas Search 页面。

  1. 在侧边栏中,单击 Services 标题下的 Atlas Search

  2. Select data source 下拉菜单中选择您的集群并单击 Go to Atlas Search

    将显示 Atlas Search 页面。

  1. 单击集群的对应 Browse Collections 按钮。

  2. 展开数据库并选择集合。

  3. 单击该集合的 Search Indexes 标签页。

    将显示 Atlas Search 页面。

  1. 单击集群的名称。

  2. 单击 Atlas Search 标签页。

    将显示 Atlas Search 页面。

3

要通过 mongosh 检索 Atlas Search 索引,请使用 db.collection.getSearchIndexes() 方法。

该命令采用以下语法。如果省略索引名称,Atlas Search 将返回集合上的所有索引。

db.<collection>.getSearchIndexes("<index-name>")

以下命令从 movies 集合中检索名为 default 的搜索索引。结果应类似于示例输出:

db.movies.getSearchIndexes("default")
[
{
id: '648b4ad4d697b73bf9d2e5e0',
name: 'default',
status: 'READY',
queryable: true,
latestDefinition: { mappings: { dynamic: true } }
}
]

要使用 C 驱动程序检索 Atlas Search 索引,请使用 mongoc_collection_aggregate() 方法创建一个包含 $listSearchIndexes 阶段的聚合管道。

1
2

以下示例应用程序指定聚合管道中的 $listSearchIndexes 阶段。然后,应用程序将管道和目标集合传递给 mongoc_collection_aggregate() 方法。此方法返回一个游标,代码可以从中访问并打印每个 Atlas Search 索引:

#include <mongoc/mongoc.h>
#include <stdlib.h>
int main (void)
{
mongoc_client_t *client = NULL;
mongoc_collection_t *collection = NULL;
mongoc_database_t *database = NULL;
bson_error_t error;
bson_t cmd = BSON_INITIALIZER;
bool ok = true;
bson_t pipeline = BSON_INITIALIZER;
mongoc_cursor_t *cursor = NULL;
mongoc_init();
// Connect to your Atlas deployment
client = mongoc_client_new("<connectionString>");
if (!client) {
fprintf(stderr, "Failed to create a MongoDB client.\n");
ok = false;
goto cleanup;
}
// Access your database and collection
database = mongoc_client_get_database(client, "<databaseName>");
collection = mongoc_database_get_collection(database, "<collectionName>");
// Create an aggregation pipeline with the $listSearchIndexes stage
const char *pipeline_str =
BSON_STR ({"pipeline" : [ {"$listSearchIndexes" : {}} ]});
// Convert your aggregation pipeline to BSON
if (!bson_init_from_json(&pipeline, pipeline_str, -1, &error)) {
fprintf(stderr, "Failed to parse command: %s\n", error.message);
ok = false;
goto cleanup;
}
// Run the aggregation operation and iterate through the indexes returned
cursor = mongoc_collection_aggregate (collection,
MONGOC_QUERY_NONE,
&pipeline,
NULL,
NULL);
const bson_t *got;
char *str;
while (mongoc_cursor_next (cursor, &got)) {
str = bson_as_canonical_extended_json (got, NULL);
printf ("%s\n", str);
bson_free (str);
}
if (mongoc_cursor_error (cursor, &error)) {
fprintf (stderr, "Failed to iterate all documents: %s\n", error.message);
ok = false;
goto cleanup;
}
cleanup:
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_database_destroy(database);
mongoc_client_destroy(client);
bson_destroy(&pipeline);
bson_destroy(&cmd);
mongoc_cleanup ();
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
3
  • Atlas 连接字符串。要了解更多信息,请参阅通过驱动程序连接

  • 您要检索索引的数据库和集合。

4
gcc -o view-index view-index.c $(pkg-config --libs --cflags libmongoc-1.0)
./view-index

要使用 C++ 驱动程序检索您的 Atlas 搜索索引,请在搜索索引视图上调用 list() 方法。

1
2

以下示例应用程序在目标集合上使用 search_indexes() 方法来实例化搜索索引视图。然后,应用程序会调用视图上的 list() 方法。此方法返回一个游标,代码从该游标访问并打印每个 Atlas Search 索引。

#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/search_index_view.hpp>
using namespace mongocxx;
int main()
{
mongocxx::instance instance{};
try
{
// Connect to your Atlas deployment
mongocxx::uri uri("<connectionString>");
mongocxx::client client(uri);
// Access your database and collection
auto db = client["<databaseName>"];
auto collection = db["<collectionName>"];
// Access and print the indexes in your collection
auto siv = collection.search_indexes();
auto cursor = siv.list();
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
};
}
catch (const std::exception& e)
{
std::cout<< "Exception: " << e.what() << std::endl;
}
return 0;
}
3
  • Atlas 连接字符串。要了解更多信息,请参阅通过驱动程序连接

  • 您要检索索引的数据库和集合。

4
g++ -o view-index view-index.cpp $(pkg-config --cflags --libs libmongocxx)
./view-index

要使用 .NET/C# 驱动程序检索 Atlas Search 索引,请使用 List()ListAsync() 方法。

以下示例应用程序返回集合上的索引。指定以下值:

  • Atlas 连接字符串。要了解更多信息,请参阅通过驱动程序连接

  • 包含要检索的搜索索引的数据库和集合。

注意

List() 方法返回的是游标,而不是索引本身。要访问索引,请使用游标范例,例如 MoveNext() 方法。

Program.cs
using MongoDB.Bson;
using MongoDB.Driver;
// connect to your Atlas deployment
var uri = "<connection-string>";
var client = new MongoClient(uri);
var db = client.GetDatabase("<databaseName>");
var collection = db.GetCollection<BsonDocument>("<collectionName>");
// list your Atlas Search indexes
var result = collection.SearchIndexes.List().ToList();
foreach (var index in result)
{
Console.WriteLine(index);
}

若要运行示例应用程序,请创建一个名为 csharp-list-indexes 的新 .NET 控制台项目,并将上一个代码示例复制到 Program.cs 文件中。然后,使用以下命令运行项目:

dotnet run csharp-list-indexes.csproj
<indexes for this collection>

提示

API 文档

要使用 Java 驱动程序检索集合上的 Atlas 搜索索引,请使用 listSearchIndexes() 方法。您必须有 Java 驱动程序 v 4.11.0 或更高版本。

1
2

以下示例应用程序检索给定集合上的所有 Atlas Search 索引。

1import com.mongodb.client.MongoClient;
2import com.mongodb.client.MongoClients;
3import com.mongodb.client.MongoCollection;
4import com.mongodb.client.MongoCursor;
5import com.mongodb.client.MongoDatabase;
6import org.bson.Document;
7public class ViewIndex {
8 public static void main(String[] args) {
9 // connect to your Atlas cluster
10 String uri = "<connection-string>";
11 try (MongoClient mongoClient = MongoClients.create(uri)) {
12 // set namespace
13 MongoDatabase database = mongoClient.getDatabase("<database-name>");
14 MongoCollection<Document> collection = database.getCollection("<collection-name>");
15 // retrieve indexes
16 try (MongoCursor<Document> resultsCursor = collection.listSearchIndexes().iterator()) {
17 while (resultsCursor.hasNext()) {
18 System.out.println(resultsCursor.next());
19 }
20 }
21 }
22 }
23}
3
  • <connection-string> - Atlas 连接字符串。要了解更多信息,请参阅通过驱动程序连接

  • <database-name> — 包含该集合的数据库的名称。

  • <collection-name> — 要检索其索引的集合的名称。

4
javac ViewIndex.java
java ViewIndex

提示

另请参阅:

要通过节点驱动程序检索 Atlas Search 索引,请使用listSearchIndexes辅助方法。

您可以使用以下名为 list-indexes.js 的示例应用程序返回集合上的索引。指定以下值:

  • Atlas 连接字符串。要了解更多信息,请参阅通过驱动程序连接

  • 包含要检索的搜索索引的数据库和集合。

  • 如果要检索特定索引,则为索引名称。 要返回集合上的所有索引,请省略此值。

注意

listSearchIndexes命令会返回一个游标。 因此,它不会立即返回与该命令匹配的索引。 要访问结果,请使用游标范例,例如toArray()方法。 要了解详情,请参阅从游标访问数据。

list-indexes.js
import { MongoClient } from "mongodb";
// connect to your Atlas deployment
const uri = "<connection-string>";
const client = new MongoClient(uri);
async function run() {
try {
const database = client.db("<databaseName>");
const collection = database.collection("<collectionName>");
// run the helper method
const result = await collection.listSearchIndexes("<index-name>").toArray();
console.log(result);
} finally {
await client.close();
}
}
run().catch(console.dir);

要运行样本应用程序,请使用以下命令。 结果应类似于示例输出:

node list-indexes.js
[
{
id: '648b4ad4d697b73bf9d2e5e0',
name: 'default',
status: 'READY',
queryable: true,
latestDefinition: { mappings: { dynamic: true } }
},
{
id: '648b4ad4d697b73bf9d2e5e1',
name: 'example-index',
status: 'PENDING',
queryable: false,
latestDefinition: {
mappings: { dynamic: false, fields: { text: { type: 'string' } } }
}
}
]

要使用 Python 驱动程序 检索 Atlas Search 索引,请在集合上调用 list_search_indexes() 方法。

1
2

下面的示例应用程序在一个集合上调用了 list_search_indexes() 方法。此方法返回一个游标,代码可以从中访问并打印每个 Atlas Search 索引:

from pymongo.mongo_client import MongoClient
def view_index():
# Connect to your Atlas deployment
uri = "<connectionString>"
client = MongoClient(uri)
# Access your database and collection
database = client["<databaseName>"]
collection = database["<collectionName>"]
# Get a list of the collection's search indexes and print them
cursor = collection.list_search_indexes()
for index in cursor:
print(index)
3
  • Atlas 连接字符串。要了解更多信息,请参阅通过驱动程序连接

  • 您要检索索引的数据库和集合。

4
python view-index.py

在 Atlas 用户界面中,Atlas Search 页面显示有关集合的 Atlas Search 索引的信息。该页面显示集群上每个索引的以下详细信息。

说明

Database

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

Collection

索引集合的名称。

Index Name

索引名称。

Type

索引的类型。值可以是以下类型的索引之一:

Index Fields

索引字段。如果启用了动态映射,则值为dynamic 。对于静态映射,该列显示所有索引字段。

Status

索引的状态。有关更多信息,请参阅 Atlas Search 索引状态

Queryable

用于标识集合是否可使用索引进行查询的图标。值可以是以下图标之一:

  • - 用于可用于查询集合的索引。

  • X - 用于不能用于查询集合的索引。

Size

索引的大小。

Documents

在集群的主节点或搜索节点上构建索引期间和之后,索引文档占集合中文档总数的近似数量和百分比。

Actions

您可以对索引执行的操作:

  • 单击 Query ,进入 Search Tester ,用于查询集合。

  • 单击 并选择以下操作之一来处理索引:

您可以按各个列对索引进行排序。默认情况下,Atlas 首先按数据库名称,然后按集合名称对索引进行排序。要按多个列排序,请按 Shift 键,然后按要对索引列表进行多重排序的顺序单击列名称。离开页面时,Atlas 不会保存您的首选排序顺序。

对于每个索引,Status 列显示集群主节点上索引的当前状态。下表描述了索引状态。

状态
说明

待处理

Atlas 尚未开始构建索引。

构建

Atlas 正在构建索引或在编辑后重新构建索引。当该索引处于以下状态时:

  • 对于新索引,Atlas Search 在索引构建完成之前不会提供服务查询。

  • 对于现有索引,可继续使用旧索引进行现有查询和新查询,直到索引重建完成。

Ready(就绪)

索引已准备就绪。

Stale

由于以下任一原因,索引已过时:

  • 由于磁盘利用率较高,复制已停止。

    暂停复制阈值为 90% 磁盘利用率,恢复复制的阈值为 85%。

  • 如果复制长时间停止,Atlas Search mongot 进程将从 oplog 中消失。

    若当前复制点在 mongod oplog 上不再可用,通常会出现此状态。如果 oplog 的 mongot 进程消失,Atlas 会重建索引。

  • 索引已达到 20 亿文档限制。

  • 复制因错误而失败。

您仍然可以查询现有索引。但是,针对过时索引的查询结果可能包含过时数据。您可以扩容搜索节点以获得更多磁盘空间,并删除现有索引以释放磁盘空间。或者,使用 View status details(查看状态详情)模态窗口中的错误来排查问题。要了解更多信息,请参阅修复 Atlas Search 问题

已失败

Atlas 无法构建索引。使用 View status details(查看状态详细信息)模式窗口中的错误来解决问题。要了解更多信息,请参阅修复 Atlas Search 问题。

删除

Atlas 正在从集群节点中删除索引。

Does not Exist(不存在)

索引无效,因为索引的集合不再存在。您无法对此索引运行查询,因为该索引没有相应的集合。Atlas 最终会删除该索引。

Status(状态)列显示集群主节点上索引的当前状态。您可以通过以下方式之一查看详细状态:

  • 单击 Status 列中状态下方的 View status details 链接。

  • 单击索引的名称,然后从索引的左侧菜单中选择 Status Details

在此页面,您可以查看有关索引的以下详细信息:

此部分显示提供服务查询的索引。如果该集合只有一个版本的索引使用该名称,则该部分包含一个指向 Index Overview 的链接,在该链接中可以查看正在使用的索引的索引定义。

如果集合中有其他同名索引,则该部分将显示最新的索引定义,还会显示您可以在不同标签页中复制的索引的先前有效版本。

更新索引时,不同节点以不同速度应用更新。因此,某些节点可能会使用最新版本,而某些节点可能会使用先前版本。Atlas 会显示索引的两个版本,直到更改应用于所有节点。

如果尝试使用无效的索引定义更新索引,则索引构建将失败,Atlas 只能显示之前有效的索引定义。

此部分显示集群每个节点上的索引状态。您可以查看每个节点上索引的以下详细信息:

Shard

分片名称。

Node

节点信息。

Status

分片节点点上的索引状态。

Queryable

指示索引能否提供服务查询提供服务的图标。

Message

索引状态的原因。对于处于 StaleFailed 状态的索引,显示索引过时或索引构建失败的原因。

Node Type

节点类型。值可以是以下类型之一:

  • 主节点

  • 从节点

  • 搜索节点

Region

Node 区域。

Size

节点上索引的大小。

Documents

已建立索引的文档数量和百分比。

Actions

您可以对节点中的索引采取的操作。您可以点击并选择下列选项之一:

警告

如果您对已具有 Atlas Search 索引的集合进行分片,当该集合开始出现在分片上时,您可能会遇到短暂的查询停机时间。此外,如果您为包含 Atlas Search 索引的已分片集合添加分片,针对该集合的搜索查询将失败,直到添加的分片完成初始同步过程为止。要了解更多信息,请参阅初始同步进程。

此部分显示 Atlas Search 和 Atlas Vector Search 索引正在迁移到搜索节点的状态(如果适用)。仅当您当前正在迁移到搜索节点时才可见。

单击 Index Status by Search Node 部分下信息横幅中的 View Migration Progress 链接,了解有关将索引迁移到 Search Nodes 的进度的详细信息(基于每个节点)。

Atlas 用户界面显示集群上每个索引的以下列。

说明

Database

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

Collection

索引集合的名称。

Index Name

索引名称。

Index Fields

索引字段。如果启用了动态映射,则值为dynamic 。对于静态映射,该列显示所有索引字段。

Type

索引的类型。值可以是以下类型的索引之一:

Status

Size

索引的大小。

Documents

迁移期间和之后,集群搜索节点上已建立索引的文档在集合中文档总数中所占的比例。

状态列显示搜索节点上索引的状态。状态可以是以下之一:

状态
说明

构建

当前正在搜索节点上构建索引。在构建新索引时,您的查询可以继续使用现有索引。

On Deck

索引已在搜索节点上成功构建,但迁移尚未完成。请注意,只有当 Atlas 在 Search 节点上成功构建所有索引时,迁移才算完成。

Ready(就绪)

搜索节点上的索引已准备好用于查询。仅当 Atlas 成功完成到搜索节点的迁移时才会显示。

已失败

索引构建失败,迁移到单独的搜索节点时停止。您的查询可以继续使用现有索引。Atlas 将尝试重建索引,但会在 7 天后超时。

要成功迁移,请在“按节点列出的状态详细信息” Message列或索引的Status Details 页面中查看索引的错误消息,并解决导致在搜索节点上构建索引失败的问题。

当新索引在搜索节点上构建时,您的查询可以继续使用现有索引。仅当 Atlas 成功在搜索节点上构建所有索引时,才能完成向搜索节点的迁移。然后,您的查询将自动路由到使用搜索节点上的索引。

后退

恢复或删除草稿