========================================
MongoDB\\Collection::createSearchIndex()
========================================

.. versionadded:: 1.17

.. default-domain:: mongodb

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

Definition
----------

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

   Create an Atlas Search index for the collection.

   .. code-block:: php

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

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

Parameters
----------

``$definition`` : array|object
  Document describing the index to create. For details on definition syntax, see
  :manual:`Search Index Definition Syntax </reference/command/createSearchIndexes/#search-index-definition-syntax>`.

``$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

     * - 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
-----------------

.. 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');

   $indexName = $collection->createSearchIndex(
      ['mappings' => ['dynamic' => true]],
      ['name' => 'test-search-index']
   );

   var_dump($indexName);

The output would then resemble:

.. code-block:: none

   string(17) "test-search-index"

See Also
--------

- :phpmethod:`MongoDB\Collection::createSearchIndexes()`
- :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
