Atlas Search
On this page
Overview
In this guide, you can learn how to perform searches on your documents by using the Atlas Search feature. The PHP library allows you to perform Atlas Search queries by using the Aggregation Builder.
Note
Deployment Compatibility
You can use the Atlas Search feature only when you connect to MongoDB Atlas clusters. This feature is not available for self-managed deployments.
To learn more about Atlas Search, see the Atlas Search Overview. The Atlas Search implementation
for the PHP library internally uses the $search
aggregation operator
to perform queries. To learn more about this operator, see the
$search reference in
the Atlas documentation.
Note
Atlas Vector Search
To perform searches on vector embeddings in MongoDB, you can use the Atlas Vector Search API. To learn about this feature, see the Atlas Vector Search guide.
Atlas Search Index
Before you can perform Atlas Search queries, you must create an Atlas Search index on your collection. To learn more about creating this index type, see the Atlas Search Indexes guide.
Search Aggregation Stage
Import the following classes into your application to perform Atlas Search queries by using the Aggregation Builder:
use MongoDB\Builder\Pipeline; use MongoDB\Builder\Search; use MongoDB\Builder\Stage;
To create a $search
stage in your aggregation pipeline, perform the
following actions:
Create a
Pipeline
instance to store the pipeline stages.Call the
Stage::search()
method to create the Atlas Search stage.Within the body of the
search()
method, use methods from theSearch
builder class to construct your Search query criteria.
The following code demonstrates the template for constructing basic Atlas Search queries:
$pipeline = new Pipeline( Stage::search( /* Atlas Search query specifications Search::compound(...) */ ), );
Atlas Search Query Examples
In this section, you can learn how to perform different types of Atlas
Search queries by using the Aggregation Builder. The examples in this
section use sample data from the sample_restaurants.restaurants
collection.
Compound Query with Filter
Use the Search::compound()
method to combine two or more operators
into a single query. This method takes named arguments for your clauses,
such as must
and filter
. In each clause, use the
Search::text()
method to specify the strings to look for when
performing the full-text search.
This example performs an Atlas Search query that has the following specifications:
Includes a
must
clause to search thename
field for the string"kitchen"
Includes a
should
clause to highly rank documents in which thecuisine
field includes"american"
Includes a
filter
field to include only documents in which theborough
value is"Queens"
in the results
$pipeline = new Pipeline( Stage::search( Search::compound( must: [ Search::text( query: 'kitchen', path: 'name', ), ], should: [ Search::text( query: 'american', path: 'cuisine', ), ], filter: [ Search::text( query: 'Queens', path: 'borough', ), ], ), ), Stage::project( borough: 1, cuisine: 1, name: 1 ), Stage::limit(3) ); $cursor = $collection->aggregate($pipeline); foreach ($cursor as $doc) { echo json_encode($doc), PHP_EOL; }
{"_id":...,"borough":"Queens","cuisine":"American","name":"Kitchen Door"} {"_id":...,"borough":"Queens","cuisine":"American","name":"Cc Kitchen"} {"_id":...,"borough":"Queens","cuisine":"American","name":"Suite Kitchen"} // Results truncated
Autocomplete Query
The PHP library provides the Search::autocomplete()
method to run
autocomplete searches on documents in your collections.
To learn more about this type of Atlas Search query, see the autocomplete reference in the Atlas documentation.
Note
Your Atlas Search index must be configured for autocomplete queries. To learn more, see How to Index Fields for Autocompletion in the Atlas documentation.
The following code performs an Atlas Search autocomplete query for the
string "Lucy"
on the name
field:
$pipeline = new Pipeline( Stage::search( Search::autocomplete( query: 'Lucy', path: 'name', ), ), Stage::limit(3), Stage::project(_id: 0, name: 1), ); $cursor = $collection->aggregate($pipeline); foreach ($cursor as $doc) { echo json_encode($doc), PHP_EOL; }
{"name":"Juicy Lucy"} {"name":"Lucy'S Vietnamese Kitchen"} {"name":"Lucy'S Cantina Royale"} // Results Truncated
You can also pass the following optional parameters to the autocomplete()
method to customize the query:
Optional Parameter | Description | Default Value |
---|---|---|
| Enables fuzzy search and fuzzy search options |
|
| Specifies order in which to search for tokens |
|
To learn more about these parameters, see the Options section of the
autocomplete
operator reference in the Atlas documentation.
Search Options
You can use the search()
method to perform many types of Atlas
Search queries. Depending on your desired query, you can pass the
following optional parameters to search()
:
Optional Parameter | Type | Description |
---|---|---|
|
| Provides the name of the Atlas Search index to use |
|
| Specifies highlighting options for displaying search terms in their original context |
|
| Parallelizes search query across segments on dedicated search nodes |
|
| Specifies the count options for retrieving a count of the results |
|
| Specifies a reference point for returning documents starting immediately following that point |
|
| Specifies a reference point for returning documents starting immediately preceding that point |
|
| Specifies whether to retrieve a detailed breakdown of the score for results |
|
| Specifies the fields on which to sort the results |
|
| Specifies whether to perform a full document lookup on the backend database or return only stored source fields directly from Atlas Search |
|
| Specifies the tracking option to retrieve analytics information on the search terms |
To learn more about these parameters, see the Fields section of the
$search
operator reference in the Atlas documentation.