-
Notifications
You must be signed in to change notification settings - Fork 33
DOCSP-45770: atlas search queries #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rustagir
merged 20 commits into
mongodb:v1.21
from
rustagir:DOCSP-45770-search-stage-args
Feb 11, 2025
Merged
Changes from 13 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c7c3a2f
DOCSP-45770: atlas search queries
rustagir 4610cf2
fix indentation
rustagir ecbc949
fix indentation
rustagir 02abea1
fix indentation
rustagir 7b21d0e
fix indentation
rustagir 712754e
snooty landing page
rustagir 8945778
snooty landing page
rustagir 4b97a4f
indentation fix
rustagir bc81545
netlify trigger
rustagir a967927
whats new
rustagir 3a15fbd
RM PR fixes 1
rustagir df43141
comments
rustagir d912397
small fix
rustagir 9d4aae2
JT tech review 1
rustagir 491f26a
indentation fix
rustagir b8fc265
fix
rustagir a6d8d98
small fixes
rustagir dab011f
small fixes
rustagir 88b50a8
remov Pipeline type
rustagir 55bddf2
Merge branch 'v1.21' into DOCSP-45770-search-stage-args
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
.. _php-atlas-search: | ||
|
||
============ | ||
Atlas Search | ||
============ | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: code example, semantic, text | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to perform searches on your documents | ||
by using the Atlas Search feature. The {+library-short+} allows you to | ||
perform Atlas Search queries by using the :ref:`Aggregation Builder API | ||
<TODO DOCSP-43919>`. | ||
|
||
.. 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:`Overview | ||
</atlas-search/atlas-search-overview/>` in the | ||
Atlas documentation. The Atlas Search implementation for the | ||
{+library-short+} internally uses the ``$search`` aggregation operator | ||
to perform queries. To learn more about this operator, see the | ||
:atlas:`$search </atlas-search/aggregation-stages/search/>` reference in | ||
the Atlas documentation. | ||
|
||
.. note:: | ||
|
||
You might not be able to use the methods described in this guide for | ||
every type of Atlas Search query. For more complex use cases, you can | ||
create standard aggregation pipelines. To learn more, see the | ||
:ref:`php-aggregation` guide. | ||
|
||
To perform searches on vector embeddings in MongoDB, you can use the | ||
Atlas Vector Search API. To learn about this feature, see | ||
the :ref:`php-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 :ref:`php-atlas-search-index` guide. | ||
|
||
Search Aggregation Stage | ||
------------------------ | ||
|
||
Import the following classes into your application to perform Atlas | ||
Search queries by using the Aggregation Builder: | ||
|
||
.. code-block:: php | ||
|
||
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`` class instance to create the pipeline | ||
|
||
#. Within the ``Pipeline`` instance, call the ``Stage::search()`` method | ||
to create the Atlas Search stage | ||
|
||
#. 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: | ||
|
||
.. code-block:: php | ||
|
||
$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 the ``name`` field for the string | ||
``"kitchen"`` | ||
- Includes a ``should`` clause to highly rank documents in which the | ||
Check failure on line 117 in source/aggregation/atlas-search.txt
|
||
``cuisine`` field includes ``"american"`` | ||
- Includes a ``filter`` field to include only documents in which the | ||
``borough`` value is ``"Queens"`` in the results | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: /includes/aggregation/atlas-search.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-compound-search-query | ||
:end-before: end-compound-search-query | ||
|
||
.. output:: | ||
:language: json | ||
:visible: false | ||
|
||
{"_id": ...,"borough":"Queens","cuisine":"American","name":"Suite Kitchen",...} | ||
{"_id": ...,"borough":"Queens","cuisine":"American","name":"Silver Kitchen",...} | ||
// Results truncated | ||
|
||
Autocomplete Query | ||
~~~~~~~~~~~~~~~~~~ | ||
|
||
The {+library-short+} 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 | ||
:atlas:`autocomplete </atlas-search/autocomplete/>` reference in the | ||
Atlas documentation. | ||
|
||
.. note:: | ||
|
||
Your Atlas Search index must be configured for autocomplete queries. | ||
To learn more, see :atlas:`How to Index Fields for Autocompletion | ||
</atlas-search/field-types/autocomplete-type/>` in the Atlas | ||
documentation. | ||
|
||
The following code performs an Atlas Search autocomplete query for the | ||
string ``"Lucy"`` on the ``name`` field: | ||
|
||
.. io-code-block:: | ||
:copyable: true | ||
|
||
.. input:: /includes/aggregation/atlas-search.php | ||
:language: php | ||
:dedent: | ||
:start-after: start-autocomplete-search-query | ||
:end-before: end-autocomplete-search-query | ||
|
||
.. output:: | ||
:language: json | ||
:visible: false | ||
|
||
{"name":"Lucy'S Asian Kitchen"} | ||
{"name":"Lucy'S Cantina Royale"} | ||
{"name":"Lucy'S Vietnamese Kitchen"} | ||
// Results Truncated | ||
|
||
You can also pass the following optional parameters to the ``autocomplete()`` | ||
method to customize the query: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
|
||
* - 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 :atlas:`Options | ||
</atlas-search/autocomplete/#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()``: | ||
|
||
.. list-table:: | ||
:header-rows: 1 | ||
|
||
* - 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 :atlas:`Fields | ||
</atlas-search/aggregation-stages/search/#fields>` section of the | ||
``$search`` operator reference in the Atlas documentation. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.