Docs Menu

MongoDB\Collection::createSearchIndex()

バージョン 1.17 の新機能

MongoDB\Collection::createSearchIndex()

Create an Atlas Search or Vector Search index for the collection.

function createSearchIndex(
array|object $definition,
array $options = []
): string

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

$definition : array|object
作成するインデックスを説明するドキュメント。 定義構文の詳細については、「検索インデックスの定義構文 」を参照してください。
$options : 配列

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

名前
タイプ
説明

comment

混合

name

string

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.

タイプ

string

Type of index to create. Accepted values are 'search' and 'vectorSearch'. If you omit this option, the default value is 'search' and the method creates an Atlas Search index.

The name of the created Atlas Search or Vector Search index as a string.

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

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

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

Atlas Search インデックスとベクトル検索インデックスは非同期で管理されます。インデックスを作成または更新した後、MongoDB\Collection::listSearchIndexes() を定期的に実行し、queryable 出力フィールド を確認して、使用可能かどうかを判断します。

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

<?php
$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"