Definition定义
db.collection.createSearchIndex()
New in version 7.0.在版本7.0中新增。 (Also available starting in 6.0.7从6.0.7开始也可用)
Creates an MongoDB Search index or Vector Search index on a specified collection or view.在指定的集合或视图上创建MongoDB搜索索引或矢量搜索索引。
Important
mongosh Method方法
This page documents a 本页记录了一种mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.mongosh方法。这不是数据库命令或特定语言驱动程序(如Node.js)的文档。
For the database command, see the 关于数据库命令,请参阅createSearchIndexes command.createSearchIndexes命令。
For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档。
Compatibility兼容性
This method is available in deployments hosted in the following environments:此方法在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
Important
This command is not supported in serverless instances. For more information, see Unsupported Commands.无服务器实例不支持此命令。有关详细信息,请参阅不支持的命令。
Syntax语法
Command syntax:命令语法:
db.<collection>.createSearchIndex(
<name>,
<type>,
{
<definition>
}
)Command Fields命令字段
createSearchIndex() takes these fields:接受这些字段:
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 |
|
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行为
createSearchIndex() triggers an index build. 触发索引构建。There may be a delay between when you receive a response from the command and when the index is ready.从收到命令的响应到索引准备就绪之间可能会有延迟。
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 如果部署强制执行访问控制,则运行createSearchIndex() must have the createSearchIndexes privilege action on the database or collection:createSearchIndex()的用户必须对数据库或集合具有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" } ]
)Examples示例
Create a Search Index on All Fields在所有字段上创建搜索索引
The following example creates a search index named 以下示例在searchIndex01 on the movies collection:movies集合上创建了一个名为searchIndex01的搜索索引:
db.movies.createSearchIndex(
"searchIndex01",
{ 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.cars.createSearchIndex(
"frenchIndex01",
{
mappings: {
fields: {
subject: {
fields: {
fr: {
analyzer: "lucene.french",
type: "string"
}
},
type: "document"
}
}
}
}
)
To learn more about language analyzers, see Language Analyzers.要了解有关语言分析器的更多信息,请参阅语言分析器。
Create a Search Index with the Default Name使用默认名称创建搜索索引
The following 以下createSearchIndex() method only specifies the index definition and omits the index name. The command creates a search index with the name default on the food collection:createSearchIndex()方法仅指定索引定义,省略了索引名称。该命令在food集合上创建了一个default名称的搜索索引:
db.food.createSearchIndex(
{
mappings: {
fields: {
title: {
type: "string"
}
}
}
}
)Create a Vector Search Index创建矢量搜索索引
The following example creates a vector search index named 以下示例在vectorSearchIndex01 on the movies collection:movies集合上创建了一个名为vectorSearchIndex01的向量搜索索引:
db.movies.createSearchIndex(
"vectorSearchIndex01",
"vectorSearch",
{
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矢量搜索更改日志