Docs Menu

Atlas Search Indexes

In this guide, you can learn how to programmatically manage your Atlas Search and Atlas Vector Search indexes by using the PHP library.

The Atlas Search feature enables you to perform full-text searches on collections hosted on MongoDB Atlas. To learn more about Atlas Search, see the Atlas Search Overview.

Atlas Vector Search enables you to perform semantic searches on vector embeddings stored in MongoDB Atlas. To learn more about Atlas Vector Search, see the Atlas Vector Search Overview.

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

  • MongoDB\Collection::createSearchIndex()

  • MongoDB\Collection::createSearchIndexes()

  • MongoDB\Collection::listSearchIndexes()

  • MongoDB\Collection::updateSearchIndex()

  • MongoDB\Collection::dropSearchIndex()

Note

Atlas Search and Vector Search Index Management is Asynchronous

The MongoDB PHP Library manages Atlas Search and Vector 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 of the preceding methods.

You can use the createSearchIndex() method to create a single Atlas Search or Vector 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:

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

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

$vectorSearchIndexName = $collection->createSearchIndex(
[
'fields' => [[
'type' => 'vector',
'path' => 'plot_embedding',
'numDimensions' => 1536,
'similarity' => 'dotProduct'
]]
],
['name' => 'myVSidx', 'type' => 'vectorSearch']
);

The following code example shows how to create Atlas Search and Vector Search indexes in one call:

$indexNames = $collection->createSearchIndexes(
[
[
'name' => 'SearchIdx',
'definition' => ['mappings' => ['dynamic' => true]],
],
[
'name' => 'VSidx',
'type' => 'vectorSearch',
'definition' => [
'fields' => [[
'type' => 'vector',
'path' => 'plot_embedding',
'numDimensions' => 1536,
'similarity' => 'dotProduct'
]]
],
],
]
);

After you create Atlas Search or Atlas Vector Search indexes, you can perform the corresponding query types on your documents.

You can use the listSearchIndexes() method to return an array of the Atlas Search and Vector 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 or Vector Search index. You can use this method to change the name or configuration of an existing 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 or Vector 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: