MongoDB\Collection::createSearchIndex()
New in version 1.17.
Definition
MongoDB\Collection::createSearchIndex()
Create an Atlas Search index for the collection.
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.
Parameters
$definition
: array|object- Document describing the index to create. For details on definition syntax, see Search Index Definition Syntax.
$options
: arrayAn array specifying the desired options.
NameTypeDescriptioncomment
mixed
Enables users to specify an arbitrary comment to help trace the operation through the database profiler, currentOp output, and logs.
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 index is named "default".
Return Values
The name of the created Atlas Search index as a string.
Errors/Exceptions
MongoDB\Exception\UnsupportedException
if options are used and
not supported by the selected server (e.g. collation
, readConcern
,
writeConcern
).
MongoDB\Exception\InvalidArgumentException
for errors related to
the parsing of parameters or options.
MongoDB\Driver\Exception\RuntimeException for other errors at the extension level (e.g. connection errors).
Behavior
Atlas 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.
Examples
Create an Index with Dynamic Mappings
The following example creates an Atlas 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);
The output would then resemble:
string(17) "test-search-index"
See Also
createSearchIndexes command reference in the MongoDB manual
Atlas Search documentation in the MongoDB Manual