Database Manual / Reference / Database Commands / MongoDB Search

createSearchIndexes (database command数据库命令)

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命令包含以下字段:

Field字段Type类型Necessity必要性Description描述
createSearchIndexesstring字符串Required必需Name of the collection on which to create the search index.要在其上创建搜索索引的集合的名称。
indexesarray数组Required必需Array of documents describing the indexes to create.描述要创建的索引的文档数组。
indexes.namestring字符串Optional可选

Name of the search index to create.要创建的搜索索引的名称。

You cannot create multiple indexes with the same name on a single collection.不能在单个集合上创建同名的多个索引。

If you do not specify a name, the index is named default.如果不指定name,则索引将命名为default

indexes.typestring字符串Optional可选

Type of search index to create. You can specify either:要创建的搜索索引类型。您可以指定:

  • search
  • vectorSearch

If you omit the type field, the index type is search.如果省略type字段,则索引类型为search

indexes.definitiondocument文档Required必需

Document describing the index to create. The definition syntax depends on whether you create a standard search index or a Vector Search index. For the definition syntax, see:描述要创建的索引的文档。definition语法取决于您创建的是标准搜索索引还是矢量搜索索引。有关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>"
} ]
}
Field字段Type类型Necessity必要性Description描述
analyzerstring字符串Optional可选

Specifies the analyzer to apply to string fields when indexing.指定索引时应用于字符串字段的分析器

If you omit this field, the index uses the standard analyzer.如果省略此字段,则索引将使用标准分析器

searchAnalyzerstring字符串Optional可选

Specifies the analyzer to apply to query text before the text is searched.指定在搜索文本之前应用于查询文本的分析器

If you omit this field, the index uses the same analyzer specified in the analyzer field.如果省略此字段,则索引将使用analyzer字段中指定的相同分析器。

If you omit both the searchAnalyzer and the analyzer fields, the index uses the standard analyzer.如果同时省略searchAnalyzeranalyzer字段,则索引将使用标准分析器

mappingsobjectRequired必需Specifies how to index fields on different paths for this index.指定如何为此索引的不同路径上的字段建立索引。
mappings.dynamicboolean布尔值Optional可选

Enables or disables dynamic field mapping for this index.启用或禁用此索引的动态字段映射。

If set to true, the index contains all fields containing supported data types.如果设置为true,则索引包含所有包含支持的数据类型的字段。

If set to false, you must specify individual fields to index using mappings.fields.如果设置为false,则必须使用mappings.fields指定要索引的单个字段。

If omitted, defaults to false.如果省略,则默认为false

mappings.fieldsdocument文档Conditional附条件

Required only if dynamic mapping is disabled.仅当禁用动态映射时才需要。

Specifies the fields to index. To learn more, see Define Field Mappings.指定要索引的字段。要了解更多信息,请参阅定义字段映射

analyzersarray数组Optional可选Specifies the Custom Analyzers to use in this index.指定要在此索引中使用的自定义分析器
storedSourceboolean or Stored Source Definition布尔值或存储源定义Optional可选

Specifies document fields to store for queries performed using the returnedStoredSource option.为使用returnedStoredSource选项执行的查询指定要存储的文档字段。

You can store fields of all MongoDB Search Field Types on MongoDB Search. The storedSource value can be one of these:您可以在MongoDB搜索上存储所有MongoDB搜索字段类型的字段。storedSource值可以是以下值之一:

  • true, to store all fields,存储所有字段
  • false, to not store any fields,不存储任何字段
  • An object that specifies the fields to include or exclude from storage指定要包含或从存储中排除的字段的对象

If omitted, defaults to false.如果省略,则默认为false

To learn more, see Define Stored Source Fields in Your MongoDB Search Index.要了解更多信息,请参阅在MongoDB搜索索引中定义存储的源字段

synonymsarray of Synonym Mapping Definitions同义词映射定义数组Optional可选

Specifies synonym mappings to use in your index. Configuring synonyms allows you to index and search for words that have the same or a similar meaning.指定要在索引中使用的同义词映射。配置同义词允许您索引和搜索具有相同或相似含义的单词。

To learn more, see Define Synonym Mappings in Your MongoDB Search Index.要了解更多信息,请参阅在MongoDB搜索索引中定义同义词映射

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权限。以下示例授予accountUser01products数据库上的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集合上创建两个搜索索引,searchIndex02searchIndex03

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了解更多