Docs Menu

MongoDB\Collection::createSearchIndexes()

バージョン 1.17 の新機能

MongoDB\Collection::createSearchIndexes()

Create one or more Atlas Search or Vector Search indexes for the collection.

function createSearchIndexes(
array $indexes,
array $options = []
): string

このコマンドは、 MongoDB Atlasでホストされている配置でのみ実行でき、少なくとも M 10の Atlas クラスター階層が必要です。 Atlas のローカル配置は、開発にも使用できます。

$indexes : 配列

作成するインデックスを説明するドキュメントの配列。

必須のdefinitionドキュメント フィールドには、作成するインデックスについて説明します。 定義構文の詳細については、「検索インデックスの定義構文 」を参照してください。

An optional name string field specifies the 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 is default.

An optional type string field specifies the type of search index to create. Accepted values are 'search' and 'vectorSearch'. If you do not specify a type, the method creates an Atlas Search index.

$options : 配列

必要なオプションを指定する配列。

The names of the created Atlas Search and Vector Search indexes as an array of strings.

MongoDB\Exception\UnsupportedExceptionオプションが使用され、選択したサーバーでサポートされていない場合(例: collationreadConcernwriteConcern )。

パラメータまたはオプションの解析に関連するエラーは、 MongoDB\Exception\InvalidArgumentException

MongoDB\Driver\Exception\RuntimeException 拡張レベルのその他のエラー(例:)。

Atlas Search and Vector Search indexes are managed asynchronously. After creating or updating an index, you can periodically execute MongoDB\Collection::listSearchIndexes() and check the queryable output field to determine whether it is ready to be used.

次の例では、動的マッピングを使用して Atlas Search インデックスを作成し、サポートされているデータ型を含むすべてのドキュメント フィールドにインデックスを作成します。

<?php
$collection = (new MongoDB\Client)->selectCollection('test', 'articles');
$indexNames = $collection->createSearchIndexes(
[
[
'name' => 'test-search-index',
'definition' => ['mappings' => ['dynamic' => true]],
],
]
);
var_dump($indexNames);

出力は次のようになります。

array(1) {
[0]=>
string(17) "test-search-index"
}