1.17版本新增。
定义
MongoDB\Collection::createSearchIndex()为集合创建MongoDB Search 或MongoDB Vector Search索引。
function createSearchIndex( array|object $definition, array $options = [] ): string This command can only be run on a deployment hosted on MongoDB Atlas and requires an Atlas cluster tier of at least M10. A Local Atlas Deployment can also be used for development.
参数
$definition: array|object- Document describing the index to create. For details on definition syntax, see Search Index Definition Syntax.
$options: array指定所需选项的数组。
Return Values
以string形式表示的已创建MongoDB Search 或 Vector Search索引的名称。
错误/异常
MongoDB\Exception\UnsupportedException,如果所选服务器使用了选项但不支持选项(例如collation、readConcern、writeConcern)。
MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。
MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。
行为
MongoDB Search 和 Vector Search 搜索索引是异步托管的。创建或更新索引后,您可以定期执行 MongoDB\Collection::listSearchIndexes() 并检查 queryable 输出字段,以确定是否可以使用。
示例
使用动态映射创建索引
The following example creates a MongoDB Search index using dynamic mappings to index all document fields containing supported data types.
$collection = (new MongoDB\Client)->selectCollection('test', 'articles'); $indexName = $collection->createSearchIndex( ['mappings' => ['dynamic' => true]], ['name' => 'test-search-index'] ); var_dump($indexName);
而输出将类似如下所示:
string(17) "test-search-index"
另请参阅
MongoDB 搜索索引 指南
createSearchIndexes command reference in the MongoDB manual
MongoDB Search documentation in the MongoDB Manual