Docs HomeMongoDB Manual

$listSearchIndexes (aggregation)

Definition定义

$listSearchIndexes

Returns information about existing Atlas Search indexes on a specified collection.返回有关指定集合上现有Atlas Search索引的信息。

Important

This command can only be run on a deployment hosted on MongoDB Atlas, and requires an Atlas cluster tier of at least M10.此命令只能在MongoDB Atlas上托管的部署上运行,并且需要至少M10的Atlas集群层。

Syntax语法

Command syntax:命令语法:

db.<collection>.aggregate(
[
{
$listSearchIndexes:
{
id: <indexId>,
name: <indexName>
}
}
]
)

Command Fields命令字段

$listSearchIndexes takes either of the following fields:采用以下任一字段:

Field字段Type类型Necessity必要性Description描述
idstringOptional可选的The id of the index to return information about.要返回有关信息的索引的id。
namestringOptional可选的The name of the index to return information about.要返回有关信息的索引的名称。

You cannot specify both id and name. 不能同时指定idnameIf you omit both the id and name fields, $listSearchIndexes returns information about all Atlas Search indexes on the collection.如果同时省略idname字段,$listSearchIndexes将返回集合上所有Atlas Search索引的信息。

Access Control访问控制

If your deployment enforces access control, the user running $listSearchIndexes must have the listSearchIndexes privilege action on the database or collection:如果部署强制执行访问控制,则运行$listSearchIndexes的用户必须对数据库或集合具有listSearchIndexes权限操作:

{
resource: {
db : <database>,
collection: <collection>
},
actions: [ "listSearchIndexes" ]
}

The built-in read role provides the the listSearchIndexes privilege. The following example grants the read role on the qa database:内置的read角色提供listSearchIndexes权限。以下示例授予qa数据库上的read角色:

db.grantRolesToUser(
"<user>",
[ { role: "read", db: "qa" } ]
)

Output输出

$listSearchIndexes returns an array of documents. Each document in the array contains the following fields:返回一个文档数组。数组中的每个文档都包含以下字段:

Field字段Type类型Description描述
idstringUnique identifier for the Atlas Search index.Atlas Search索引的唯一标识符。
namestringName of the Atlas Search index.Atlas Search索引的名称。
statusstringStatus of the Atlas Search index. Atlas Search索引的状态。For more information, see Atlas Search Index Statuses.有关更多信息,请参阅Atlas Search Index Statues
queryablebooleanIndicates whether the index is ready to be queried.指示索引是否已准备好进行查询。
latestDefinitiondocumentThe most recent index definition set for this index. 为此索引设置的最新索引定义。For more information, see Search Index Definition Syntax.有关详细信息,请参阅搜索索引定义语法

Atlas Search Index StatusesAtlas搜索索引状态

The status field in the $listSearchIndexes output can be one of the following:$listSearchIndexes输出中的status字段可以是以下字段之一:

StatusDescription描述
BUILDINGThe following scenarios can cause an index to be in the BUILDING state: 以下情况可能导致索引处于BUILDING状态:
  • Atlas is building the index or re-building the index after an edit.Atlas正在构建索引或在编辑后重新构建索引。
  • Atlas Search cannot keep up with indexing changes to the collection. Atlas Search无法跟上对集合的索引更改。In this case, Atlas rebuilds the index in the background.在这种情况下,Atlas会在后台重建索引。
When the index is in the BUILDING state: 当索引处于BUILDING状态时:
  • For a new index, Atlas Search cannot use the index for queries until the index build is complete.对于新索引,在索引构建完成之前,Atlas Search无法使用该索引进行查询。
  • For an existing index, Atlas Search uses the old index definition for queries until the index rebuild is complete.对于现有索引,Atlas Search将使用旧的索引定义进行查询,直到索引重建完成。
FAILEDThe index build failed. 索引生成失败。Indexes can enter the FAILED state due to an invalid index definition.由于索引定义无效,索引可能会进入FAILED状态。
PENDINGAtlas has not yet started building the index.Atlas尚未开始建立索引。
READYThe index is ready and can support queries.索引已准备就绪,可以支持查询。
STALEThe index is queryable but has stopped replicating data from the indexed collection. 该索引是可查询的,但已停止从索引集合中复制数据。Searches on the index may return out-of-date data.对索引的搜索可能会返回过期的数据。
Indexes can enter the STALE state due to replication errors. 由于复制错误,索引可能会进入STALE状态。

Examples实例

These examples demonstrate how to:这些示例演示了如何:

Return All Search Indexes返回所有搜索索引

The following example returns all Atlas Search indexes on the movies collection:以下示例返回movies集合上的所有Atlas Search索引:

db.movies.aggregate(
[
{
$listSearchIndexes: { }
}
]
)

Sample output:样本输出:

[
{
id: '648b5397d8261c7d7d6f720e',
name: 'searchIndex01',
status: 'READY',
queryable: true,
latestDefinition: { mappings: { dynamic: true } }
},
{
id: '648b6110912df5513228465f',
name: 'frenchIndex01',
status: 'PENDING',
queryable: false,
latestDefinition: {
mappings: {
fields: {
subject: {
fields: { fr: { analyzer: 'lucene.french', type: 'string' } },
type: 'document'
}
}
}
}
}
]

The movies collection contains two indexes:movies集合包含两个索引:

  • searchIndex01 is in the READY state.处于READY状态。
  • frenchIndex01 is in the PENDING state.处于PENDING状态。

Return a Single Search Index by Name按名称返回单个搜索索引

The following example returns the searchIndex01 index on the movies collection:以下示例返回movies集合的searchIndex01索引:

db.movies.aggregate(
[
{
$listSearchIndexes:
{
name: "searchIndex01"
}
}
]
)

Sample output:样本输出:

[
{
id: '648cb60e06f6780ba87a9913',
name: 'searchIndex01',
status: 'READY',
queryable: true,
latestDefinition: { mappings: { dynamic: true } }
}
]

Return a Single Search Index by id按id返回单个搜索索引

The following example returns the frenchIndex01 index based on the index id:以下示例根据索引id返回frenchIndex01索引:

db.movies.aggregate(
[
{
$listSearchIndexes:
{
id: "648b6110912df5513228465f"
}
}
]
)

Sample output:样本输出:

[
{
id: '648b6110912df5513228465f',
name: 'frenchIndex01',
status: 'PENDING',
queryable: true,
latestDefinition: {
mappings: {
fields: {
subject: {
fields: { fr: { analyzer: 'lucene.french', type: 'string' } },
type: 'document'
}
}
}
}
}
]

Learn More了解更多信息

To use a mongosh method to view Atlas Search indexes, see db.collection.getSearchIndexes().要使用mongosh方法查看Atlas搜索索引,请参阅db.collection.getSearchIndexes()

To create Atlas Search indexes, see:要创建Atlas Search索引,请参阅: