MongoDB聚合:$listSearchIndexes

$listSearchIndexes返回指定集合现有Atlas Search索引的信息。

**重要:**该命令只能在托管的MongoDB Allas,并且要求群集层级至少为M10。

语法

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

参数说明:

  • id,字符串,可选参数,要查询索引的id
  • name,字符串,可选参数,要查询索引的名称

不能同时指定idname,必须要有listSearchIndexes的权限:

访问控制

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

内置读角色提供listSearchIndexes权限。下面的示例授予了qa数据库的read角色:

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

输出字段

$listSearchIndexes返回一个文档数组,数组中的每个文件都包含以下字段:

字段 类型 描述
id 字符串 索引的唯一标识
name 字符串 索引名称
status 字符串 索引状态
queryable 布尔 索引是否已经准备好被查询
latestDefinitionVersion 文档 索引版本
latestDefinitionVersion.version 整数 索引定义的版本,当更新索引定义时,版本号会自动增加
latestDefinitionVersion.createdAt 日期 索引定义创建的时间
latestDefinition 文档 最新的索引定义
statusDetail 文档数组 包含了每个搜索主机(mongot)上的索引状态
statusDetail.[n].hostname 字符串 相应的mongot的主机名
statusDetail.[n].status 字符串 相应的mongot的索引状态
statusDetail.[n].queryable 布尔 相应的mongot的索引是否可被查询
statusDetail.[n].mainIndex 文档 相应的mongot的活动索引的状态
statusDetail.[n].stagedIndex 文档 包含了相应mongot上,在后台构建的索引的状态信息。该字段只会出现在构建一个索引去更新已存在索引的时候
synonymMappingStatus 字符串 索引的同义词映射状态,该字段只会出现在索引有同义词定义时。可以是BUILDINGFAILEDREADY这个三个值之一。返回的状态是每个独立mongot上同义词映射的概括
synonymMappingStatusDetail 文档数组 该字段(和其子字段)只有当索引有同义词定义时才会出现。包含了每个搜索主机(mongot)的索引的同义词映射状态
synonymMappingStatusDetail.[n].status 字符串 所有mongot进程中相应同义词映射的状态
synonymMappingStatusDetail.[n].queryable 布尔 表示相应的同义词映射是否支持跨所有mongot进程的查询
message 字符串 描述同义词映射的错误(如果适用)。只有当该同义词映射的状态为FAILED时才会出现

索引状态详情

下表描述了statusDetail.[mongot].mainIndexstatusDetail.[mongot].stagedIndex文档的内嵌字段:

这些字段描述了特定mongot的索引状态。

|字段|类型|描述|
|-|-|
|status|字符串|相应mongot上索引的生成状态|
|queryable|布尔|相应mongot上索引是否可被查询|
|synonymMappingStatus|字符串|索引生成的同义词映射在相应mongot上的状态。只有当索引定义了同义词时才会出现|
|synonymMappingStatusDetails|文档|包含索引的同义词映射在相应mongot上的状态。该字段(及其子字段)只有在索引定义了同义词时才会出现|
|definitionVersion|文档|描述生成索引时使用的索引定义版本。
|definitionVersion.version|整数|索引生成在相应mongot上使用的版本号。更新索引定义时,更新后的索引的版本号将递增|
|definitionVersion.createdAt|日期|创建索引定义的时间|
|definition|文档|建立该索引时使用的定义|

同义词映射详细信息

下表描述了以下对象的嵌入字段:

  • statusDetail.mainIndex.synonymMappingStatusDetails.
  • statusDetail.stagedIndex.synonymMappingStatusDetails

|字段|类型|描述|
|-|-|
|status|字符串|跨mongot进程的同义词映射的状态。|
|queryable|布尔|指示同义词映射是否可以支持对相应mongot进程的查询。|
|message|字符串|描述同义词映射的错误(如果适用)。只有当该同义词映射的状态为FAILED时才会出现|

Atlas Search索引状态

$listSearchIndexesstatus字段可以是下面的值:

状态 描述
BUILDING 当编辑后,Atlas构建或重建索引时或当Atlas Search无法跟上采集索引的变化时(会在后台重建索引),状态为BUILDING;当索引处于BUILDDING时,对于新建索引,Atlas Search不能使用索引查询直到索引构建完成;对于新建索引,Atlas Search会先试用旧索引定义,直到新索引建好后
FAILED 索引构建失败,索引可能因索引定义无效而进入FAILED状态
PENDING Atlas 尚未开始构建索引
READY 索引就绪可以用于查询
STALE 索引可查询,但已停止从索引集合复制数据。对该索引的搜索可能会返回过期数据。由于复制错误,索引可能会进入STALE状态

错误

7.0.3 版中的更改:此命令在非Atlas上执行时会出错:

db.names.aggregate( [
   { $listSearchIndexes: { } }
] )
MongoServerError: PlanExecutor error during aggregation :: caused by :: Search index commands are only supported with Atlas.

在以前的版本中,如果在非Atlas上执行该命令,则返回空结果。

举例

返回所有查询索引

下面的示例将返回电影收藏中的所有Atlas Search索引:

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

输出:

[
  {
    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的索引:

db.movies.aggregate(
   [
      {
         $listSearchIndexes:
            {
               name: "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
              }
            }
          ]
        }
      }
    ]
  }
]

按id返回单个搜索索引

下面的示例以提供的id返回搜索索引:

db.movies.aggregate(
   [
      {
         $listSearchIndexes:
            {
               id: "6524096020da840844a4c4a7"
            }
      }
   ]
)

结果:

[
  {
    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: {} } }
        }
      }
    ]
  }
]

你可能感兴趣的:(mongodb,mongodb,数据库)