|
| 1 | +.. _laravel-scout: |
| 2 | + |
| 3 | +============================ |
| 4 | +Integrated Search with Scout |
| 5 | +============================ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: php framework, odm, code example, text search, atlas |
| 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 use the Laravel Scout feature in |
| 24 | +your {+odm-long+} application. Scout allows you to implement full-text |
| 25 | +search on your Eloquent models. To learn more, see `Laravel Scout |
| 26 | +<https://laravel.com/docs/{+laravel-docs-version+}/scout>`__ in the |
| 27 | +Laravel documentation. |
| 28 | + |
| 29 | +The Scout Integration for {+odm-long+} provides the following |
| 30 | +functionality: |
| 31 | + |
| 32 | +- Provides an abstraction to create search indexes on documents in |
| 33 | + MongoDB collections and other search engines. In MongoDB, this feature |
| 34 | + allows you to create :atlas:`Atlas Search indexes |
| 35 | + </atlas-search/manage-indexes/>`. |
| 36 | + |
| 37 | +- Allows you to automatically replicate data from MongoDB into a |
| 38 | + search engine such as `Meilisearch <https://www.meilisearch.com/>`__ |
| 39 | + or `Algolia <https://www.algolia.com/>`__. You can use a MongoDB Eloquent |
| 40 | + model as the source to import and index. |
| 41 | + |
| 42 | +.. note:: Deployment Compatibility |
| 43 | + |
| 44 | + You can use Laravel Scout only when you connect to MongoDB Atlas |
| 45 | + clusters. This feature is not available for self-managed or |
| 46 | + serverless deployments. |
| 47 | + |
| 48 | +Install Scout Package |
| 49 | +--------------------- |
| 50 | + |
| 51 | +Before you can use Scout in your application, you must run the following |
| 52 | +command to install Laravel Scout: |
| 53 | + |
| 54 | +.. code-block:: bash |
| 55 | + |
| 56 | + composer require laravel/scout |
| 57 | + |
| 58 | +Add the Searchable Trait to Your Model |
| 59 | +-------------------------------------- |
| 60 | + |
| 61 | +Add the ``Laravel\Scout\Searchable`` trait to an Eloquent model to make |
| 62 | +it searchable. This example uses the ``Movie`` model, which represents |
| 63 | +documents in the ``sample_mflix.movies`` collection. |
| 64 | + |
| 65 | +.. code-block:: php |
| 66 | + :emphasize-lines: 6, 10 |
| 67 | + |
| 68 | + <?php |
| 69 | + |
| 70 | + namespace App\Models; |
| 71 | + |
| 72 | + use MongoDB\Laravel\Eloquent\Model; |
| 73 | + use Laravel\Scout\Searchable; |
| 74 | + |
| 75 | + class Movie extends Model |
| 76 | + { |
| 77 | + use Searchable; |
| 78 | + |
| 79 | + protected $connection = 'mongodb'; |
| 80 | + } |
| 81 | + |
| 82 | +Configure Scout Integration |
| 83 | +--------------------------- |
| 84 | + |
| 85 | +Ensure that your application is configured to use MongoDB as its |
| 86 | +database connection. To learn how to configure MongoDB, see the |
| 87 | +:ref:`laravel-quick-start-connect-to-mongodb` section of the Quick Start |
| 88 | +guide. |
| 89 | + |
| 90 | +To configure Scout in your application, create a file named |
| 91 | +``scout.php`` in your application's ``config`` directory. Paste the |
| 92 | +following code into the file to configure Scout: |
| 93 | + |
| 94 | +.. code-block:: php |
| 95 | + :caption: config/scout.php |
| 96 | + |
| 97 | + <?php |
| 98 | + |
| 99 | + return [ |
| 100 | + 'driver' => env('SCOUT_DRIVER', 'mongodb'), |
| 101 | + |
| 102 | + 'mongodb' => [ |
| 103 | + 'connection' => env('SCOUT_MONGODB_CONNECTION', 'mongodb'), |
| 104 | + ], |
| 105 | + |
| 106 | + 'prefix' => env('SCOUT_PREFIX', 'scout_'), |
| 107 | + ]; |
| 108 | + |
| 109 | +The preceding code specifies the following configuration: |
| 110 | + |
| 111 | +- Uses MongoDB as the default search driver |
| 112 | + |
| 113 | +- Specifies ``scout_`` as the prefix for the collection name of the |
| 114 | + searchable collection used by Scout |
| 115 | + |
| 116 | +.. tip:: Queueing |
| 117 | + |
| 118 | + When using Scout, consider configuring a queue driver to reduce |
| 119 | + response times for your application's web interface. To learn more, |
| 120 | + see the `Queuing section |
| 121 | + <https://laravel.com/docs/{+laravel-docs-version+}/scout#queueing>`__ |
| 122 | + of the Laravel Scout documentation and the :ref:`laravel-queues` guide. |
| 123 | + |
| 124 | +Create the Search Index |
| 125 | +----------------------- |
| 126 | + |
| 127 | +After you configure Scout and set your default search driver, you can |
| 128 | +create your searchable collection and search index by running the |
| 129 | +following command: |
| 130 | + |
| 131 | +.. code-block:: bash |
| 132 | + |
| 133 | + php artisan scout:index 'App\Models\Movie' |
| 134 | + |
| 135 | +Because you set MongoDB as the default search driver, the preceding |
| 136 | +command creates the search collection with an Atlas Search index in your |
| 137 | +MongoDB database. The collection is named ``scout_movies``, based on the prefix |
| 138 | +set in the preceding section. The Atlas Search index is named ``scout`` |
| 139 | +and has the following configuration: |
| 140 | + |
| 141 | +.. code-block:: json |
| 142 | + |
| 143 | + { |
| 144 | + "mappings": { |
| 145 | + "dynamic": true |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +.. note:: |
| 150 | + |
| 151 | + It generally takes up to a minute for MongoDB to create and finalize |
| 152 | + an Atlas Search index, so the ``scout:index`` command might not |
| 153 | + return a success message immediately. |
| 154 | + |
| 155 | +Import Your Data into the Searchable Collection |
| 156 | +----------------------------------------------- |
| 157 | + |
| 158 | +You can use Scout to replicate data from a source collection into a |
| 159 | +searchable collection. The following command replicates and indexes data |
| 160 | +from the ``movies`` collection into the ``scout_movies`` collection |
| 161 | +created in the preceding section: |
| 162 | + |
| 163 | +.. code-block:: bash |
| 164 | + |
| 165 | + php artisan scout:import 'App\Models\Movie' |
| 166 | + |
| 167 | +The documents are automatically indexed for Atlas Search queries. |
| 168 | + |
| 169 | +Select Fields to Import |
| 170 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 171 | + |
| 172 | +You might not need all the fields from your source documents in your |
| 173 | +searchable collection. Limiting the fields you replicate can improve |
| 174 | +your application's speed and performance. |
| 175 | + |
| 176 | +You can select specific fields to import by defining the |
| 177 | +``toSearchableArray()`` method in your Eloquent model class. The |
| 178 | +following code demonstrates how to define ``toSearchableArray()`` to |
| 179 | +select only the ``plot`` and ``title`` fields for replication: |
| 180 | + |
| 181 | +.. code-block:: php |
| 182 | + |
| 183 | + class Movie extends Model |
| 184 | + { |
| 185 | + .... |
| 186 | + public function toSearchableArray(): array |
| 187 | + { |
| 188 | + return [ |
| 189 | + 'plot' => $this->plot, |
| 190 | + 'title' => $this->title, |
| 191 | + ]; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | +Use an External Search Engine |
| 196 | +----------------------------- |
| 197 | + |
| 198 | +.. TODO https://jira.mongodb.org/browse/DOCSP-45125 |
0 commit comments