Definition定义
updateSearchIndex
New in version 7.0.在版本7.0中新增。 (Also available starting in 6.0.7)
Updates an existing MongoDB Search index.更新现有的MongoDB搜索索引。
The mongosh method db.collection.updateSearchIndex() provides a wrapper around the updateSearchIndex database command.mongosh方法db.collection.updateSearchIndex()为updateSearchIndex数据库命令提供了一个包装器。
Important
This command can only be run on a deployment hosted on MongoDB Atlas.此命令只能在MongoDB Atlas上托管的部署上运行。
Syntax语法
Command syntax:命令语法:
db.runCommand(
{
updateSearchIndex: "<collection name>",
id: "<index Id>",
name: "<index name>",
type: "search | vectorSearch",
definition: {
/* search index definition fields */
}
}
)Command Fields命令字段
The updateSearchIndex command takes the following fields:updateSearchIndex命令包含以下字段:
updateSearchIndex | |||
id |
| ||
name |
| ||
type |
| ||
definition |
|
Search Index Definition Syntax搜索索引定义语法
The search index definition takes the following fields:搜索索引定义包含以下字段:
{
analyzer: "<analyzer-for-index>",
searchAnalyzer: "<analyzer-for-query>",
mappings: {
dynamic: <boolean>,
fields: { <field-definition> }
},
analyzers: [ <custom-analyzer> ],
storedSource: <boolean> | {
<stored-source-definition>
},
synonyms: [ {
name: "<synonym-mapping-name>",
source: {
collection: "<source-collection-name>"
},
analyzer: "<synonym-mapping-analyzer>"
} ]
}
analyzer |
| ||
searchAnalyzer |
| ||
mappings | object | ||
mappings.dynamic |
| ||
mappings.fields |
| ||
analyzers | |||
storedSource |
| ||
synonyms |
|
Behavior行为
The updateSearchIndex command triggers an index build with the new index definition. There may be a delay between when you receive a response from the command and when the updated index is ready.updateSearchIndex命令使用新的索引定义触发索引构建。从收到命令的响应到更新的索引准备就绪之间可能会有延迟。
The old index definition can still support queries while the new index is being built. Once the new index finishes building, the old index is no longer usable. To see the status of your search indexes, use the 在构建新索引时,旧的索引定义仍然可以支持查询。一旦新索引构建完成,旧索引就不再可用。要查看搜索索引的状态,请使用$listSearchIndexes aggregation stage.$listSearchIndexes聚合阶段。
Access Control访问控制
If your deployment enforces access control, the user running the 如果部署强制执行访问控制,则运行updateSearchIndex command must have the updateSearchIndex privilege action on the database or collection:updateSearchIndex命令的用户必须对数据库或集合具有updateSearchIndex权限操作:
{
resource: {
db : <database>,
collection: <collection>
},
actions: [ "updateSearchIndex" ]
}
The built-in 内置的readWrite and restore roles provide the updateSearchIndex privilege. The following example grants the readWrite role on the qa database:readWrite和restore角色提供updateSearchIndex权限。以下示例授予qa数据库上的readWrite角色:
db.grantRolesToUser(
"<user>",
[ { role: "readWrite", db: "qa" } ]
)Output输出
A successful 成功的updateSearchIndex command returns the following:updateSearchIndex命令返回以下内容:
{
ok: 1
}
Important
The response field 响应字段ok: 1 indicates that the command was successful. However, there may be a delay between when you receive the response and when the updated index is ready and replaces the original index.ok: 1表示命令成功。但是,从您收到响应到更新的索引准备就绪并替换原始索引之间可能会有延迟。
To see the status of your search indexes, use the 要查看搜索索引的状态,请使用$listSearchIndexes aggregation stage.$listSearchIndexes聚合阶段。
Example示例
The following example updates a search index named 以下示例更新searchIndex01 on the contacts collection:contacts集合上名为searchIndex01的搜索索引:
db.runCommand( {
updateSearchIndex: "contacts",
name: "searchIndex01",
definition:
{
mappings: { dynamic: true },
storedSource: {
exclude: [ "directors", "imdb.rating" ]
}
}
} )