db.collection.getSearchIndexes()
定义
7.0 版本中的新增功能:(也可从 6.0.7 开始使用)
返回指定集合上现有 Atlas Search 索引的信息。
重要
此命令只能在MongoDB Atlas托管的部署上运行,并且需要至少 M 10的 Atlas 集群层。
重要
mongosh 方法
本页介绍了mongosh
方法。这不是特定于语言的驱动程序(例如 Node.js)的文档。
对于 MongoDB API 驱动程序,请参阅特定语言的MongoDB 驱动程序文档。
语法
命令语法:
db.<collection>.getSearchIndexes(<indexName>)
命令字段
getSearchIndexes()
采用此字段:
字段 | 类型 | 必要性 | 说明 |
---|---|---|---|
indexName | 字符串 | Optional | 要返回信息的索引名称。 如果省略 indexName 字段, getSearchIndexes() 将返回有关collection上所有 Atlas Search 搜索索引的信息。 |
访问控制
如果您的部署强制执行访问控制,则运行getSearchIndexes()
的用户必须对数据库或集合具有listSearchIndexes
特权操作:
{ resource: { db : <database>, collection: <collection> }, actions: [ "listSearchIndexes" ] }
内置read
角色提供listSearchIndexes
特权。以下示例授予针对qa
数据库的read
角色:
db.grantRolesToUser( "<user>", [ { role: "read", db: "qa" } ] )
输出
getSearchIndexes()
返回一个文档数组。数组中的每个文档都包含以下字段:
字段 | 类型 | 说明 |
---|---|---|
id | 字符串 | 索引的唯一标识符。 |
name | 字符串 | 索引的名称。 |
status | 字符串 | 索引的状态。有关更多信息,请参阅Atlas Search 索引状态。 |
queryable | 布尔 | 指示索引是否已准备好接受查询。 |
latestDefinitionVersion | 文档 | 描述索引的版本。 |
latestDefinitionVersion.version | 整型 | 与索引定义关联的版本号。更新索引定义时,版本号会自动递增。 |
latestDefinitionVersion.createdAt | 日期 | 当前索引定义的创建时间。 |
latestDefinition | 文档 | 索引的最新定义。有关详细信息,请参阅搜索索引定义语法。 |
statusDetail | 文档数组 | 包含每个搜索主机 ( mongot ) 上的索引状态。 |
statusDetail.[n].hostname | 字符串 | 相应 mongot 的主机名。 |
statusDetail.[n].status | 字符串 | 相应 mongot 上的索引状态。 |
statusDetail.[n].queryable | 布尔 | 指示索引是否准备好在相应的 mongot 上进行查询。 |
statusDetail.[n].mainIndex | 文档 | 包含相应 有关详细信息,请参阅索引状态详细信息。 |
statusDetail.[n].stagedIndex | 文档 | 包含有关在相应 有关详细信息,请参阅索引状态详细信息。 |
synonymMappingStatus | 字符串 | 索引同义词映射的状态。仅当索引定义了同义词时才会显示此字段。可以是以下值之一:
返回的状态是每个 |
synonymMappingStatusDetail | 文档数组 | 包含每个搜索主机 ( mongot ) 上索引同义词映射的状态。仅当索引定义了同义词时,才会显示此字段(及其子字段)。 |
synonymMappingStatusDetail.[n].status | 字符串 | 所有 mongot 进程的相应同义词映射状态。 |
synonymMappingStatusDetail.[n].queryable | 布尔 | 指示相应的同义词映射是否可以支持跨所有 mongot 进程的查询。 |
message | 字符串 | 描述同义词映射错误(如果适用)。仅当此同义词映射的 status 为FAILED 时才会出现。 |
索引状态详细信息
下表描述了以下文档的嵌入式字段: - statusDetail.[mongot].mainIndex
- statusDetail.[mongot].stagedIndex
这些字段描述特定mongot
上的索引状态。
字段 | 类型 | 说明 |
---|---|---|
status | 字符串 | 相应 mongot 上的索引生成状态。 |
queryable | 布尔 | 指示索引生成是否已准备好在相应的 mongot 上进行查询。 |
synonymMappingStatus | 字符串 | 相应 mongot 上索引生成的同义词映射的状态。仅当索引定义了同义词时才出现。 |
synonymMappingStatusDetails | 文档 | 包含相应 有关更多信息,请参阅同义词映射详情。 |
definitionVersion | 文档 | 描述用于构建此索引生成的索引定义版本。 |
definitionVersion.version | 整型 | 索引生成在相应 mongot 上使用的版本号。更新索引定义时,更新后的索引会使用递增的版本号进行构建。 |
definitionVersion.createdAt | 日期 | 索引定义的创建时间。 |
definition | 文档 | 构建此索引所使用的定义。 |
同义词映射详细信息
下表描述了以下对象的嵌入式字段:
statusDetail.mainIndex.synonymMappingStatusDetails.<synonymMapping>
statusDetail.stagedIndex.synonymMappingStatusDetails<synonymMapping>
字段 | 类型 | 说明 |
---|---|---|
status | 字符串 | 相应 mongot 进程上的同义词映射状态。 |
queryable | 布尔 | 指示同义词映射是否可以支持对相应 mongot 进程的查询。 |
message | 字符串 | 描述同义词映射错误(如果适用)。仅当此同义词映射的 status 为FAILED 时才会出现。 |
Atlas Search 索引状态
getSearchIndexes()
输出中的status
字段可以是以下字段之一:
状态 | 说明 |
---|---|
BUILDING | 以下情况可能导致索引处于
当索引处于
处于 |
DOES_NOT_EXIST | 索引不存在。 处于 |
DELETING | Atlas 正在删除索引。 处于 |
FAILED | 索引构建失败。由于索引定义无效,索引可能会进入 处于 |
PENDING | Atlas 尚未开始构建索引。 处于 |
READY | 索引已准备就绪,可以支持查询。 处于 |
STALE | 索引可查询,但已停止从索引集合复制数据。对索引的搜索可能会返回过时的数据。 由于复制错误,索引可能会进入 处于 |
举例
这些示例演示如何:
返回所有搜索索引
以下示例返回movies
集合上的所有 Atlas Search 索引。 movies
collection 包含两个搜索索引: default
和synonym_mappings
。
db.movies.getSearchIndexes()
输出示例:
[ { id: '6524096020da840844a4c4a7', name: 'default', status: 'BUILDING', queryable: true, latestDefinitionVersion: { version: 2, createdAt: ISODate("2023-10-09T14:51:57.355Z") }, latestDefinition: { mappings: { dynamic: true }, storedSource: { include: [ 'awards.text' ] } }, statusDetail: [ { hostname: 'atlas-n1cm1j-shard-00-02', status: 'BUILDING', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:08:32.000Z") }, definition: { mappings: { dynamic: true, fields: {} } } }, stagedIndex: { status: 'PENDING', queryable: false, definitionVersion: { version: 1, createdAt: ISODate("2023-10-09T14:51:29.000Z") }, definition: { mappings: { dynamic: true, fields: {} }, storedSource: true } } }, { hostname: 'atlas-n1cm1j-shard-00-01', status: 'BUILDING', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:08:32.000Z") }, definition: { mappings: { dynamic: true, fields: {} } } }, stagedIndex: { status: 'PENDING', queryable: false, definitionVersion: { version: 1, createdAt: ISODate("2023-10-09T14:51:29.000Z") }, definition: { mappings: { dynamic: true, fields: {} }, storedSource: true } } }, { hostname: 'atlas-n1cm1j-shard-00-00', status: 'BUILDING', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:08:32.000Z") }, definition: { mappings: { dynamic: true, fields: {} } } } } ] }, { id: '65240be420da840844a4d077', name: 'synonym_mappings', status: 'READY', queryable: true, latestDefinitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:19:16.305Z") }, latestDefinition: { mappings: { dynamic: true, fields: { fullplot: { type: 'string' } } }, synonyms: [ { name: 'synonym_mapping', analyzer: 'lucene.english', source: { collection: 'synonyms' } } ] }, synonymMappingStatus: 'READY', synonymMappingStatusDetail: [ { synonym_mapping: { status: 'READY', queryable: true } } ], statusDetail: [ { hostname: 'atlas-n1cm1j-shard-00-02', status: 'READY', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:19:16.000Z") }, definition: { mappings: { dynamic: true, fields: { fullplot: { type: 'string', indexOptions: 'offsets', store: true, norms: 'include' } } }, synonyms: [ { name: 'synonym_mapping', analyzer: 'lucene.english', source: { collection: 'synonyms' } } ] }, synonymMappingStatus: 'READY', synonymMappingStatusDetail: [ { synonym_mapping: { status: 'READY', queryable: true } } ] } }, { hostname: 'atlas-n1cm1j-shard-00-01', status: 'READY', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:19:16.000Z") }, definition: { mappings: { dynamic: true, fields: { fullplot: { type: 'string', indexOptions: 'offsets', store: true, norms: 'include' } } }, synonyms: [ { name: 'synonym_mapping', analyzer: 'lucene.english', source: { collection: 'synonyms' } } ] }, synonymMappingStatus: 'READY', synonymMappingStatusDetail: [ { synonym_mapping: { status: 'READY', queryable: true } } ] } }, { hostname: 'atlas-n1cm1j-shard-00-00', status: 'READY', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:19:16.000Z") }, definition: { mappings: { dynamic: true, fields: { fullplot: { type: 'string', indexOptions: 'offsets', store: true, norms: 'include' } } }, synonyms: [ { name: 'synonym_mapping', analyzer: 'lucene.english', source: { collection: 'synonyms' } } ] }, synonymMappingStatus: 'READY', synonymMappingStatusDetail: [ { synonym_mapping: { status: 'READY', queryable: true } } ] } } ] } ]
返回单个搜索索引
synonym_mappings
movies
以下示例返回collection上的 索引:
db.movies.getSearchIndexes("synonym_mappings")
输出示例:
[ { id: '65240be420da840844a4d077', name: 'synonym_mappings', status: 'READY', queryable: true, latestDefinitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:19:16.305Z") }, latestDefinition: { mappings: { dynamic: true, fields: { fullplot: { type: 'string' } } }, synonyms: [ { name: 'synonym_mapping', analyzer: 'lucene.english', source: { collection: 'synonyms' } } ] }, synonymMappingStatus: 'READY', synonymMappingStatusDetail: [ { synonym_mapping: { status: 'READY', queryable: true } } ], statusDetail: [ { hostname: 'atlas-n1cm1j-shard-00-02', status: 'READY', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:19:16.000Z") }, definition: { mappings: { dynamic: true, fields: { fullplot: { type: 'string', indexOptions: 'offsets', store: true, norms: 'include' } } }, synonyms: [ { name: 'synonym_mapping', analyzer: 'lucene.english', source: { collection: 'synonyms' } } ] }, synonymMappingStatus: 'READY', synonymMappingStatusDetail: [ { synonym_mapping: { status: 'READY', queryable: true } } ] } }, { hostname: 'atlas-n1cm1j-shard-00-01', status: 'READY', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:19:16.000Z") }, definition: { mappings: { dynamic: true, fields: { fullplot: { type: 'string', indexOptions: 'offsets', store: true, norms: 'include' } } }, synonyms: [ { name: 'synonym_mapping', analyzer: 'lucene.english', source: { collection: 'synonyms' } } ] }, synonymMappingStatus: 'READY', synonymMappingStatusDetail: [ { synonym_mapping: { status: 'READY', queryable: true } } ] } }, { hostname: 'atlas-n1cm1j-shard-00-00', status: 'READY', queryable: true, mainIndex: { status: 'READY', queryable: true, definitionVersion: { version: 0, createdAt: ISODate("2023-10-09T14:19:16.000Z") }, definition: { mappings: { dynamic: true, fields: { fullplot: { type: 'string', indexOptions: 'offsets', store: true, norms: 'include' } } }, synonyms: [ { name: 'synonym_mapping', analyzer: 'lucene.english', source: { collection: 'synonyms' } } ] }, synonymMappingStatus: 'READY', synonymMappingStatusDetail: [ { synonym_mapping: { status: 'READY', queryable: true } } ] } } ] } ]