Definition定义
createSearchIndexes
New in version 7.0.在版本7.0中新增。 (Also available starting in 6.0.7从6.0.7开始也可用)
Creates one or more MongoDB Search indexes or Vector Search indexes on a specified collection or view.在指定的集合或视图上创建一个或多个MongoDB搜索索引或向量搜索索引。
The mongosh method db.collection.createSearchIndex() provides a wrapper around the createSearchIndexes database command.mongosh方法db.collection.createSearchIndex()为createSearchIndexes数据库命令提供了一个包装器。
Important
This command can only be run on a deployment hosted on MongoDB Atlas.此命令只能在MongoDB Atlas上托管的部署上运行。
Syntax语法
Command syntax:命令语法:
db.runCommand(
{
createSearchIndexes: "<collection name>",
indexes: [
{
name: "<index name>",
type: "<search index type>",
definition: {
/* search index definition fields */
}
},
...
]
}
)Command Fields命令字段
The createSearchIndexes command takes the following fields:createSearchIndexes命令包含以下字段:
createSearchIndexes | |||
indexes | |||
indexes.name |
| ||
indexes.type |
| ||
indexes.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 |
|
Vector Search Index Definition Syntax矢量搜索索引定义语法
The vector search index definition takes the following fields:向量搜索索引定义包含以下字段:
{
"fields": [
{
"type": "vector" | "filter",
"path": "<field-to-index>",
"numDimensions": <number-of-dimensions>,
"similarity": "euclidean" | "cosine" | "dotProduct"
}
]
}
For explanations of vector search index definition fields, see How to Index Fields for Vector Search.有关矢量搜索索引定义字段的说明,请参阅如何为矢量搜索的字段建立索引。
Behavior行为
The createSearchIndexes command triggers an index build. There may be a delay between when you receive a response from the command and when the index is ready.createSearchIndexes命令触发索引构建。从收到命令的响应到索引准备就绪之间可能会有延迟。
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 如果部署强制执行访问控制,则运行createSearchIndexes command must have the createSearchIndexes privilege action on the database or collection:createSearchIndexes命令的用户必须对数据库或集合具有createSearchIndexes权限操作:
{
resource: {
db : <database>,
collection: <collection>
},
actions: [ "createSearchIndexes" ]
}
The built-in 内置的readWrite role provides the createSearchIndexes privilege. The following example grants accountUser01 the readWrite role on the products database:readWrite角色提供createSearchIndexes权限。以下示例授予accountUser01在products数据库上的readWrite角色:
db.grantRolesToUser(
"accountUser01",
[ { role: "readWrite", db: "products" } ]
)Output输出
The createSearchIndexes command output resembles the following:createSearchIndexes命令输出类似于以下内容:
{
ok: 1,
indexesCreated: [
{
id: "<index Id>",
name: "<index name>"
}
]
}
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 created indexes are ready for use.ok: 1表示命令成功。但是,在收到响应和创建的索引准备就绪之间可能会有延迟。
To see the status of your search indexes, use the 要查看搜索索引的状态,请使用$listSearchIndexes aggregation stage.$listSearchIndexes聚合阶段。
Examples示例
Create a Search Index on All Fields在所有字段上创建搜索索引
The following example creates a search index named 以下示例在searchIndex01 on the contacts collection:contacts集合上创建了一个名为searchIndex01的搜索索引:
db.runCommand( {
createSearchIndexes: "contacts",
indexes: [
{
name: "searchIndex01",
definition: { mappings: { dynamic: true } }
}
]
} )
The index definition specifies 索引定义指定了mappings: { dynamic: true }, which means that the index contains all fields in the collection that have supported data types.mappings: { dynamic: true },这意味着索引包含集合中支持数据类型的所有字段。
Create a Search Index with a Language Analyzer使用语言分析器创建搜索索引
A language analyzer introduces stop-words, which are words that are not significant enough to be indexed.语言分析器引入停用词,这些词的重要性不足以被索引。
The following example creates a search index named 以下示例在frenchIndex01 on the cars collection, and specifies the lucene.french analyzer on the fr field:car集合上创建了一个名为frenchIndex01的搜索索引,并在fr字段上指定了lucene.french分析器:
db.runCommand( {
createSearchIndexes: "cars",
indexes: [
{
name: "frenchIndex01",
definition: {
mappings: {
fields: {
subject: {
fields: {
fr: {
analyzer: "lucene.french",
type: "string"
}
},
type: "document"
}
}
}
}
}
]
} )
To learn more about language analyzers, see Language Analyzers.要了解有关语言分析器的更多信息,请参阅语言分析器。
Create Multiple Search Indexes创建多个搜索索引
The following command creates two search indexes on the 以下命令在products collection, searchIndex02 and searchIndex03:products集合上创建两个搜索索引,searchIndex02和searchIndex03:
db.runCommand( {
createSearchIndexes: "products",
indexes: [
{
name: "searchIndex02",
definition: {
mappings: {
fields: {
title: {
type: "string",
analyzer: "lucene.simple"
}
}
}
}
},
{
name: "searchIndex03",
definition:
{
mappings: { dynamic: true }
}
}
]
} )
searchIndex02 uses a simple analyzer on the 在标题字段上使用一个简单的分析器。简单分析器根据非字母字符(如空格、标点符号或数字)将文本划分为可搜索的术语。title field. The simple analyzer divides text into searchable terms based on non-letter characters, such as whitespace, punctuation, or digits.
searchIndex03 uses a dynamic field mapping, meaning the index contains all fields in the collection that have supported data types.使用动态字段映射,这意味着索引包含集合中支持数据类型的所有字段。
Create a Vector Search Index创建矢量搜索索引
The following example creates a vector search index named 以下示例在vectorSearchIndex01 on the movies collection:movies集合上创建了一个名为vectorSearchIndex01的向量搜索索引:
db.runCommand( {
createSearchIndexes: "movies",
indexes: [
{
name: "vectorSearchIndex01",
type: "vectorSearch",
definition: {
fields: [
{
type: "vector",
numDimensions: 1,
path: "genre",
similarity: "cosine"
}
]
}
}
]
} )
The vector search index contains one dimension and indexes the 向量搜索索引包含一个维度,并对genre field.genre字段进行索引。
Learn More了解更多
$vectorSearchaggregation stage聚合阶段Tutorial: Semantic Search教程:语义搜索Vector Search Changelog矢量搜索更改日志