|
| 1 | +.. _php-atlas-search: |
| 2 | + |
| 3 | +============ |
| 4 | +Atlas Search |
| 5 | +============ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, semantic, text |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to perform searches on your documents |
| 24 | +by using the Atlas Search feature. The {+library-short+} allows you to |
| 25 | +perform Atlas Search queries by using the :ref:`Aggregation Builder API |
| 26 | +<TODO DOCSP-43919>`. |
| 27 | + |
| 28 | +.. note:: Deployment Compatibility |
| 29 | + |
| 30 | + You can use the Atlas Search feature only when |
| 31 | + you connect to MongoDB Atlas clusters. This feature is not |
| 32 | + available for self-managed deployments. |
| 33 | + |
| 34 | +To learn more about Atlas Search, see the :atlas:`Atlas Search Overview |
| 35 | +</atlas-search/atlas-search-overview/>`. The Atlas Search implementation |
| 36 | +for the {+library-short+} internally uses the ``$search`` aggregation operator |
| 37 | +to perform queries. To learn more about this operator, see the |
| 38 | +:atlas:`$search </atlas-search/aggregation-stages/search/>` reference in |
| 39 | +the Atlas documentation. |
| 40 | + |
| 41 | +.. note:: Atlas Vector Search |
| 42 | + |
| 43 | + To perform searches on vector embeddings in MongoDB, you can use the |
| 44 | + Atlas Vector Search API. To learn about this feature, see |
| 45 | + the :ref:`php-vector-search` guide. |
| 46 | + |
| 47 | +Atlas Search Index |
| 48 | +~~~~~~~~~~~~~~~~~~ |
| 49 | + |
| 50 | +Before you can perform Atlas Search queries, you must create an Atlas |
| 51 | +Search index on your collection. To learn more about creating this index |
| 52 | +type, see the :ref:`php-atlas-search-index` guide. |
| 53 | + |
| 54 | +Search Aggregation Stage |
| 55 | +------------------------ |
| 56 | + |
| 57 | +Import the following classes into your application to perform Atlas |
| 58 | +Search queries by using the Aggregation Builder: |
| 59 | + |
| 60 | +.. literalinclude:: /includes/aggregation/atlas-search.php |
| 61 | + :language: php |
| 62 | + :dedent: |
| 63 | + :start-after: start-imports |
| 64 | + :end-before: end-imports |
| 65 | + |
| 66 | +To create a ``$search`` stage in your aggregation pipeline, perform the |
| 67 | +following actions: |
| 68 | + |
| 69 | +1. Create an array to store the pipeline stages |
| 70 | + |
| 71 | +#. Call the ``Stage::search()`` method to create the Atlas Search stage |
| 72 | + |
| 73 | +#. Within the body of the ``search()`` method, use methods from the |
| 74 | + ``Search`` builder class to construct your Search query criteria |
| 75 | + |
| 76 | +The following code demonstrates the template for constructing basic Atlas Search |
| 77 | +queries: |
| 78 | + |
| 79 | +.. code-block:: php |
| 80 | + |
| 81 | + $pipeline = [ |
| 82 | + Stage::search( |
| 83 | + /* Atlas Search query specifications |
| 84 | + Search::compound(...) */ |
| 85 | + ), |
| 86 | + ]; |
| 87 | + |
| 88 | +Atlas Search Query Examples |
| 89 | +--------------------------- |
| 90 | + |
| 91 | +In this section, you can learn how to perform different types of Atlas |
| 92 | +Search queries by using the Aggregation Builder. The examples in this |
| 93 | +section use sample data from the ``sample_restaurants.restaurants`` |
| 94 | +collection. |
| 95 | + |
| 96 | +Compound Query with Filter |
| 97 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 98 | + |
| 99 | +Use the ``Search::compound()`` method to combine two or more operators |
| 100 | +into a single query. This method takes named arguments for your clauses, |
| 101 | +such as ``must`` and ``filter``. In each clause, use the |
| 102 | +``Search::text()`` method to specify the strings to look for when |
| 103 | +performing the full-text search. |
| 104 | + |
| 105 | +This example performs an Atlas Search query that has the following |
| 106 | +specifications: |
| 107 | + |
| 108 | +- Includes a ``must`` clause to search the ``name`` field for the string |
| 109 | + ``"kitchen"`` |
| 110 | +- Includes a ``should`` clause to highly rank documents in which the |
| 111 | + ``cuisine`` field includes ``"american"`` |
| 112 | +- Includes a ``filter`` field to include only documents in which the |
| 113 | + ``borough`` value is ``"Queens"`` in the results |
| 114 | + |
| 115 | +.. io-code-block:: |
| 116 | + :copyable: true |
| 117 | + |
| 118 | + .. input:: /includes/aggregation/atlas-search.php |
| 119 | + :language: php |
| 120 | + :dedent: |
| 121 | + :start-after: start-compound-search-query |
| 122 | + :end-before: end-compound-search-query |
| 123 | + |
| 124 | + .. output:: |
| 125 | + :language: json |
| 126 | + :visible: false |
| 127 | + |
| 128 | + {"_id":...,"borough":"Queens","cuisine":"American","name":"Kitchen Door"} |
| 129 | + {"_id":...,"borough":"Queens","cuisine":"American","name":"Cc Kitchen"} |
| 130 | + {"_id":...,"borough":"Queens","cuisine":"American","name":"Suite Kitchen"} |
| 131 | + // Results truncated |
| 132 | + |
| 133 | +Autocomplete Query |
| 134 | +~~~~~~~~~~~~~~~~~~ |
| 135 | + |
| 136 | +The {+library-short+} provides the ``Search::autocomplete()`` method to run |
| 137 | +autocomplete searches on documents in your collections. |
| 138 | + |
| 139 | +To learn more about this type of Atlas Search query, see the |
| 140 | +:atlas:`autocomplete </atlas-search/autocomplete/>` reference in the |
| 141 | +Atlas documentation. |
| 142 | + |
| 143 | +.. note:: |
| 144 | + |
| 145 | + Your Atlas Search index must be configured for autocomplete queries. |
| 146 | + To learn more, see :atlas:`How to Index Fields for Autocompletion |
| 147 | + </atlas-search/field-types/autocomplete-type/>` in the Atlas |
| 148 | + documentation. |
| 149 | + |
| 150 | +The following code performs an Atlas Search autocomplete query for the |
| 151 | +string ``"Lucy"`` on the ``name`` field: |
| 152 | + |
| 153 | +.. io-code-block:: |
| 154 | + :copyable: true |
| 155 | + |
| 156 | + .. input:: /includes/aggregation/atlas-search.php |
| 157 | + :language: php |
| 158 | + :dedent: |
| 159 | + :start-after: start-autocomplete-search-query |
| 160 | + :end-before: end-autocomplete-search-query |
| 161 | + |
| 162 | + .. output:: |
| 163 | + :language: json |
| 164 | + :visible: false |
| 165 | + |
| 166 | + {"name":"Juicy Lucy"} |
| 167 | + {"name":"Lucy'S Vietnamese Kitchen"} |
| 168 | + {"name":"Lucy'S Cantina Royale"} |
| 169 | + // Results Truncated |
| 170 | + |
| 171 | +You can also pass the following optional parameters to the ``autocomplete()`` |
| 172 | +method to customize the query: |
| 173 | + |
| 174 | +.. list-table:: |
| 175 | + :header-rows: 1 |
| 176 | + |
| 177 | + * - Optional Parameter |
| 178 | + - Description |
| 179 | + - Default Value |
| 180 | + |
| 181 | + * - ``fuzzy`` |
| 182 | + - Enables fuzzy search and fuzzy search options |
| 183 | + - ``false`` |
| 184 | + |
| 185 | + * - ``tokenOrder`` |
| 186 | + - Specifies order in which to search for tokens |
| 187 | + - ``'any'`` |
| 188 | + |
| 189 | +To learn more about these parameters, see the :atlas:`Options |
| 190 | +</atlas-search/autocomplete/#options>` section of the |
| 191 | +``autocomplete`` operator reference in the Atlas documentation. |
| 192 | + |
| 193 | +Search Options |
| 194 | +-------------- |
| 195 | + |
| 196 | +You can use the ``search()`` method to perform many types of Atlas |
| 197 | +Search queries. Depending on your desired query, you can pass the |
| 198 | +following optional parameters to ``search()``: |
| 199 | + |
| 200 | +.. list-table:: |
| 201 | + :header-rows: 1 |
| 202 | + |
| 203 | + * - Optional Parameter |
| 204 | + - Type |
| 205 | + - Description |
| 206 | + |
| 207 | + * - ``index`` |
| 208 | + - ``string`` |
| 209 | + - Provides the name of the Atlas Search index to use |
| 210 | + |
| 211 | + * - ``highlight`` |
| 212 | + - ``array`` |
| 213 | + - Specifies highlighting options for displaying search terms in their |
| 214 | + original context |
| 215 | + |
| 216 | + * - ``concurrent`` |
| 217 | + - ``bool`` |
| 218 | + - Parallelizes search query across segments on dedicated search nodes |
| 219 | + |
| 220 | + * - ``count`` |
| 221 | + - ``string`` |
| 222 | + - Specifies the count options for retrieving a count of the results |
| 223 | + |
| 224 | + * - ``searchAfter`` |
| 225 | + - ``string`` |
| 226 | + - Specifies a reference point for returning documents starting |
| 227 | + immediately following that point |
| 228 | + |
| 229 | + * - ``searchBefore`` |
| 230 | + - ``string`` |
| 231 | + - Specifies a reference point for returning documents starting |
| 232 | + immediately preceding that point |
| 233 | + |
| 234 | + * - ``scoreDetails`` |
| 235 | + - ``bool`` |
| 236 | + - Specifies whether to retrieve a detailed breakdown of the score |
| 237 | + for results |
| 238 | + |
| 239 | + * - ``sort`` |
| 240 | + - ``array`` |
| 241 | + - Specifies the fields on which to sort the results |
| 242 | + |
| 243 | + * - ``returnStoredSource`` |
| 244 | + - ``bool`` |
| 245 | + - Specifies whether to perform a full document lookup on the |
| 246 | + backend database or return only stored source fields directly |
| 247 | + from Atlas Search |
| 248 | + |
| 249 | + * - ``tracking`` |
| 250 | + - ``array`` |
| 251 | + - Specifies the tracking option to retrieve analytics information |
| 252 | + on the search terms |
| 253 | + |
| 254 | +To learn more about these parameters, see the :atlas:`Fields |
| 255 | +</atlas-search/aggregation-stages/search/#fields>` section of the |
| 256 | +``$search`` operator reference in the Atlas documentation. |
0 commit comments