==========================================
MongoDB\\Collection::createSearchIndexes()
==========================================

.. versionadded:: 1.17

.. default-domain:: mongodb

.. contents:: On this page
   :local:
   :backlinks: none
   :depth: 1
   :class: singlecol

Definition
----------

.. phpmethod:: MongoDB\Collection::createSearchIndexes()

   Create one or more Atlas Search indexes for the collection.

   .. code-block:: php

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

   .. include:: /includes/extracts/note-atlas-search-requirement.rst

Parameters
----------

``$indexes`` : array
  Array of documents describing the indexes to create.

  A required ``definition`` document field describes the index to create. For
  details on definition syntax, see
  :manual:`Search Index Definition Syntax </reference/command/createSearchIndexes/#search-index-definition-syntax>`.

  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 index is named "default".

``$options`` : array
  An array specifying the desired options.

  .. list-table::
     :header-rows: 1
     :widths: 20 20 80

     * - Name
       - Type
       - Description

     * - comment
       - mixed
       - .. include:: /includes/extracts/common-option-comment.rst

Return Values
-------------

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

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-unsupportedexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Behavior
--------

.. include:: /includes/extracts/note-atlas-search-async.rst

Examples
--------

Create an Index with Dynamic Mappings
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following example creates an Atlas Search index using
`dynamic mappings <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#dynamic-mappings>`__
to index all document fields containing
`supported data types <https://www.mongodb.com/docs/atlas/atlas-search/define-field-mappings/#std-label-bson-data-chart>`__.

.. code-block:: php

   <?php

   $collection = (new MongoDB\Client)->selectCollection('test', 'articles');

   $indexNames = $collection->createSearchIndexes(
       [
           [
               'name' => 'test-search-index',
               'definition' => ['mappings' => ['dynamic' => true]],
           ],
       ]
   );

   var_dump($indexNames);

The output would then resemble:

.. code-block:: none

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

See Also
--------

- :phpmethod:`MongoDB\Collection::createSearchIndex()`
- :phpmethod:`MongoDB\Collection::dropSearchIndex()`
- :phpmethod:`MongoDB\Collection::listSearchIndexes()`
- :phpmethod:`MongoDB\Collection::updateSearchIndex()`
- :manual:`createSearchIndexes </reference/command/createSearchIndexes>` command
  reference in the MongoDB manual
- `Atlas Search <https://www.mongodb.com/docs/atlas/atlas-search/>`__ documentation in the MongoDB Manual
