Docs Menu
Docs Home
/ / /
PHP Library Manual
/

Atlas Search Indexes

On this page

  • Overview
  • Create a Search Index
  • List Search Indexes
  • Update a Search Index
  • Delete a Search Index
  • Additional Information

The MongoDB Atlas Search feature enables you to perform full-text searches on collections hosted on Atlas. Before you can perform Atlas Search queries, you must create indexes that specify which fields to index and how they are indexed.

To learn more about Atlas Search, see the Atlas Search Overview.

You can use the following methods on a MongoDB\Collection instance to manage your Atlas Search indexes:

  • MongoDB\Collection::createSearchIndex()

  • MongoDB\Collection::createSearchIndexes()

  • MongoDB\Collection::listSearchIndexes()

  • MongoDB\Collection::updateSearchIndex()

  • MongoDB\Collection::dropSearchIndex()

Note

Atlas Search Index Management is Asynchronous

The MongoDB PHP Library manages Atlas Search indexes asynchronously. The library methods described in the following sections return the server response immediately, but the changes to your Search indexes take place in the background and might not complete until some time later.

The following sections provide code examples that demonstrate how to use each Atlas Search index management method.

You can use the createSearchIndex() method to create a single Atlas Search index on a collection, or the createSearchIndexes() method to create multiple indexes simultaneously.

The following code example shows how to create a single Atlas Search index:

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

The following code example shows how to create multiple Atlas Search indexes:

$indexNames = $collection->createSearchIndexes(
[
[
'name' => 'SearchIdx_dynamic',
'definition' => ['mappings' => ['dynamic' => true]],
],
[
'name' => 'SearchIdx_simple',
'definition' => [
'mappings' => [
'dynamic' => false,
'fields' => [
'title' => [
'type' => 'string',
'analyzer' => 'lucene.simple'
]
]
]
],
],
]
);

After you create a Search index, you can perform Atlas Search queries on your collection. To learn more, see Create and Run Atlas Search Queries in the Atlas documentation.

You can use the listSearchIndexes() method to return an array of the Atlas Search indexes on a collection:

foreach ($collection->listSearchIndexes() as $indexInfo) {
echo json_encode($indexInfo), PHP_EOL;
}

You can use the updateSearchIndex() method to update an Atlas Search index. You can use this method to change the name of a Search index or change the configuration of the index.

The following code shows how to update a search index to use a simple analyzer on the title field:

$collection->updateSearchIndex(
'mySearchIdx',
['mappings' => [
'dynamic' => false,
'fields' => [
'title' => [
'type' => 'string',
'analyzer' => 'lucene.simple'
]
]
]]
);

You can use the dropSearchIndex() method to remove an Atlas Search index from a collection.

The following code shows how to delete the Atlas Search index named mySearchIdx:

$collection->dropSearchIndex('mySearchIdx');

To view runnable examples that demonstrate how to manage indexes, see Optimize Queries by Using Indexes.

To view tutorials that explain how to use the Atlas Search feature, see Get Started with Atlas Search in the Atlas documentation.

To learn more about any of the methods discussed in this guide, see the following API documentation:

Back

Multikey Indexes