Docs Menu
Docs Home
/ /

MongoDB Search Indexes

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

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

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

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

  • MongoDB\Collection::createSearchIndex()

  • MongoDB\Collection::createSearchIndexes()

  • MongoDB\Collection::listSearchIndexes()

  • MongoDB\Collection::updateSearchIndex()

  • MongoDB\Collection::dropSearchIndex()

Note

MongoDB Search and MongoDB Vector Search Index Management is Asynchronous

The MongoDB PHP Library manages MongoDB Search and MongoDB 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 MongoDB Search or MongoDB 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 MongoDB Search index:

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

The following code example shows how to create a single MongoDB 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 MongoDB 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 MongoDB Search or MongoDB Vector Search indexes, you can perform the corresponding query types on your documents. To learn more, see the following guides:

  • Run a MongoDB Search Query guide

  • Run a MongoDB Vector Search Query guide

You can use the listSearchIndexes() method to return an array of the MongoDB Search and MongoDB 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 a MongoDB Search or MongoDB 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 MongoDB Search or MongoDB Vector Search index from a collection.

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

$collection->dropSearchIndex('mySearchIdx');

To view runnable examples that demonstrate how to manage indexes, see Indexes for Query Optimization.

To view tutorials that explain how to use the MongoDB Search feature, see Get Started with MongoDB 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

On this page