1.17 版本中的新增功能。
定义
MongoDB\Collection::createSearchIndex()为集合创建MongoDB Search 或MongoDB Vector Search索引。
function createSearchIndex( array|object $definition, array $options = [] ): string 此命令只能在托管在 MongoDB Atlas上的部署上运行,并且需要至少 M 10的 Atlas 集群层。 本地 Atlas 部署也可用于开发。
参数
$definition: array|object- 描述要创建的索引的文档。 有关定义语法的详细信息,请参阅搜索索引定义事务语法。
$options: array指定所需选项的数组。
名称类型说明comment
混合
名称
字符串
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 default index name isdefault.类型
字符串
要创建的索引类型。接受的值为
'search'和'vectorSearch'。如果省略此选项,默认值为'search',并且该方法会创建MongoDB Search索引。
Return Values
以字符串形式表示的已创建MongoDB Search 或 Vector Search索引的名称。
错误/异常
MongoDB\Exception\UnsupportedException,如果所选服务器使用了选项但不支持选项(例如collation、readConcern、writeConcern)。
MongoDB\Exception\InvalidArgumentException 用于与参数或选项解析相关的错误。
MongoDB\ 驱动程序\Exception\RuntimeException 用于扩展级别的其他错误(例如连接错误)。
行为
MongoDB Search 和 Vector Search 索引是异步托管的。创建或更新索引后,您可以定期执行MongoDB\Collection::listSearchIndexes() 并检查queryable 输出字段,以确定是否可以使用。
示例
使用动态映射创建索引
以下示例使用动态映射创建MongoDB搜索索引,以对包含受支持数据类型的所有文档字段索引。
$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"