Docs Menu
Docs Home
/ / /
PHP Library Manual
/

Atlas Search

On this page

  • Overview
  • Atlas Search Index
  • Search Aggregation Stage
  • Atlas Search Query Examples
  • Compound Query with Filter
  • Autocomplete Query
  • Search Options

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.

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.

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:

  1. Create a Pipeline instance to store the pipeline stages.

  2. Call the Stage::search() method to create the Atlas Search stage.

  3. Within the body of the search() method, use methods from the Search 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(...) */
),
);

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.

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 the name field for the string "kitchen"

  • Includes a should clause to highly rank documents in which the cuisine field includes "american"

  • Includes a filter field to include only documents in which the borough 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

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

fuzzy

Enables fuzzy search and fuzzy search options

false

tokenOrder

Specifies order in which to search for tokens

'any'

To learn more about these parameters, see the Options section of the autocomplete operator reference in the Atlas documentation.

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

index

string

Provides the name of the Atlas Search index to use

highlight

array

Specifies highlighting options for displaying search terms in their original context

concurrent

bool

Parallelizes search query across segments on dedicated search nodes

count

string

Specifies the count options for retrieving a count of the results

searchAfter

string

Specifies a reference point for returning documents starting immediately following that point

searchBefore

string

Specifies a reference point for returning documents starting immediately preceding that point

scoreDetails

bool

Specifies whether to retrieve a detailed breakdown of the score for results

sort

array

Specifies the fields on which to sort the results

returnStoredSource

bool

Specifies whether to perform a full document lookup on the backend database or return only stored source fields directly from Atlas Search

tracking

array

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.

Back

Data Aggregation