@@ -52,148 +52,146 @@ This section demonstrates how to use the Scout integration in your
5252application to support Atlas Search queries.
5353
5454.. procedure::
55- :style: connected
56-
57- .. step:: Install Scout package
58-
59- Before you can use Scout in your application, run the following
60- command from your application's root directory to install Laravel Scout:
61-
62- .. code-block:: bash
63-
64- composer require laravel/scout
65-
66- .. step:: Add the Searchable trait to your model
67-
68- Add the ``Laravel\Scout\Searchable`` trait to an Eloquent model to make
69- it searchable. The following example adds this trait to the ``Movie``
70- model, which represents documents in the ``sample_mflix.movies``
71- collection:
72-
73- .. code-block:: php
74- :emphasize-lines: 6, 10
75-
76- <?php
77-
78- namespace App\Models;
79-
80- use MongoDB\Laravel\Eloquent\Model;
81- use Laravel\Scout\Searchable;
82-
83- class Movie extends Model
84- {
85- use Searchable;
86-
87- protected $connection = 'mongodb';
88- }
89-
90- .. step:: Configure Scout in your application
91-
92- Ensure that your application is configured to use MongoDB as its
93- database connection. To learn how to configure MongoDB, see the
94- :ref:`laravel-quick-start-connect-to-mongodb` section of the Quick Start
95- guide.
96-
97- To configure Scout in your application, create a file named
98- ``scout.php`` in your application's ``config`` directory. Paste the
99- following code into the file to configure Scout:
100-
101- .. code-block:: php
102- :caption: config/scout.php
103-
104- <?php
105-
106- return [
107- 'driver' => env('SCOUT_DRIVER', 'mongodb'),
108-
109- 'mongodb' => [
110- 'connection' => env('SCOUT_MONGODB_CONNECTION', 'mongodb'),
111- ],
112-
113- 'prefix' => env('SCOUT_PREFIX', 'scout_'),
114- ];
115-
116- The preceding code specifies the following configuration:
117-
118- - Uses MongoDB as the default search driver
119- - Specifies ``scout_`` as the prefix for the collection name of the
120- searchable collection
121-
122- .. tip:: Queueing
123-
124- When using Scout, consider configuring a queue driver to reduce
125- response times for your application's web interface. To learn more,
126- see the `Queuing section
127- <https://laravel.com/docs/{+laravel-docs-version+}/scout#queueing>`__
128- of the Laravel Scout documentation and the :ref:`laravel-queues` guide.
129-
130- .. step:: Create the Atlas Search index
131-
132- After you configure Scout and set your default search driver, you can
133- create your searchable collection and search index by running the
134- following command from your application's root directory:
135-
136- .. code-block:: bash
137-
138- php artisan scout:index 'App\Models\Movie'
139-
140- Because you set MongoDB as the default search driver, the preceding
141- command creates the search collection with an Atlas Search index in your
142- MongoDB database. The collection is named ``scout_movies``, based on the prefix
143- set in the preceding section. The Atlas Search index is named ``scout``
144- and has the following configuration:
145-
146- .. code-block:: json
147-
148- {
149- "mappings": {
150- "dynamic": true
151- }
152- }
153-
154- .. note::
155-
156- MongoDB can take up to a minute to create and finalize
157- an Atlas Search index, so the ``scout:index`` command might not
158- return a success message immediately.
159-
160- .. step:: Import data into the searchable collection
161-
162- You can use Scout to replicate data from a source collection into a
163- searchable collection. The following command replicates and indexes data
164- from the ``movies`` collection into the ``scout_movies`` collection
165- created in the preceding section:
166-
167- .. code-block:: bash
168-
169- php artisan scout:import 'App\Models\Movie'
170-
171- The documents are automatically indexed for Atlas Search queries.
172-
173- .. tip:: Select Fields to Import
174-
175- You might not need all the fields from your source documents in your
176- searchable collection. Limiting the fields you replicate can improve
177- your application's speed and performance.
178-
179- You can select specific fields to import by defining the
180- ``toSearchableArray()`` method in your Eloquent model class. The
181- following code demonstrates how to define ``toSearchableArray()`` to
182- select only the ``plot`` and ``title`` fields for replication:
183-
184- .. code-block:: php
185-
186- class Movie extends Model
187- {
188- ....
189- public function toSearchableArray(): array
190- {
191- return [
192- 'plot' => $this->plot,
193- 'title' => $this->title,
194- ];
195- }
196- }
55+ :style: connected
56+
57+ .. step:: Install Scout package
58+
59+ Before you can use Scout in your application, run the following
60+ command from your application's root directory to install Laravel Scout:
61+
62+ .. code-block:: bash
63+
64+ composer require laravel/scout
65+
66+ .. step:: Add the Searchable trait to your model
67+
68+ Add the ``Laravel\Scout\Searchable`` trait to an Eloquent model to make
69+ it searchable. The following example adds this trait to the ``Movie``
70+ model, which represents documents in the ``sample_mflix.movies``
71+ collection:
72+
73+ .. code-block:: php
74+ :emphasize-lines: 6, 10
75+
76+ <?php
77+
78+ namespace App\Models;
79+
80+ use MongoDB\Laravel\Eloquent\Model;
81+ use Laravel\Scout\Searchable;
82+
83+ class Movie extends Model
84+ {
85+ use Searchable;
86+
87+ protected $connection = 'mongodb';
88+ }
89+
90+ .. step:: Configure Scout in your application
91+
92+ Ensure that your application is configured to use MongoDB as its
93+ database connection. To learn how to configure MongoDB, see the
94+ :ref:`laravel-quick-start-connect-to-mongodb` section of the Quick Start
95+ guide.
96+
97+ To configure Scout in your application, create a file named
98+ ``scout.php`` in your application's ``config`` directory. Paste the
99+ following code into the file to configure Scout:
100+
101+ .. code-block:: php
102+ :caption: config/scout.php
103+
104+ <?php
105+
106+ return [
107+ 'driver' => env('SCOUT_DRIVER', 'mongodb'),
108+ 'mongodb' => [
109+ 'connection' => env('SCOUT_MONGODB_CONNECTION', 'mongodb'),
110+ ],
111+ 'prefix' => env('SCOUT_PREFIX', 'scout_'),
112+ ];
113+
114+ The preceding code specifies the following configuration:
115+
116+ - Uses MongoDB as the default search driver
117+ - Specifies ``scout_`` as the prefix for the collection name of the
118+ searchable collection
119+
120+ .. tip:: Queueing
121+
122+ When using Scout, consider configuring a queue driver to reduce
123+ response times for your application's web interface. To learn more,
124+ see the `Queuing section
125+ <https://laravel.com/docs/{+laravel-docs-version+}/scout#queueing>`__
126+ of the Laravel Scout documentation and the :ref:`laravel-queues` guide.
127+
128+ .. step:: Create the Atlas Search index
129+
130+ After you configure Scout and set your default search driver, you can
131+ create your searchable collection and search index by running the
132+ following command from your application's root directory:
133+
134+ .. code-block:: bash
135+
136+ php artisan scout:index 'App\Models\Movie'
137+
138+ Because you set MongoDB as the default search driver, the preceding
139+ command creates the search collection with an Atlas Search index in your
140+ MongoDB database. The collection is named ``scout_movies``, based on the prefix
141+ set in the preceding section. The Atlas Search index is named ``scout``
142+ and has the following configuration:
143+
144+ .. code-block:: json
145+
146+ {
147+ "mappings": {
148+ "dynamic": true
149+ }
150+ }
151+
152+ .. note::
153+
154+ MongoDB can take up to a minute to create and finalize
155+ an Atlas Search index, so the ``scout:index`` command might not
156+ return a success message immediately.
157+
158+ .. step:: Import data into the searchable collection
159+
160+ You can use Scout to replicate data from a source collection into a
161+ searchable collection. The following command replicates and indexes data
162+ from the ``movies`` collection into the ``scout_movies`` collection
163+ created in the preceding section:
164+
165+ .. code-block:: bash
166+
167+ php artisan scout:import 'App\Models\Movie'
168+
169+ The documents are automatically indexed for Atlas Search queries.
170+
171+ .. tip:: Select Fields to Import
172+
173+ You might not need all the fields from your source documents in your
174+ searchable collection. Limiting the fields you replicate can improve
175+ your application's speed and performance.
176+
177+ You can select specific fields to import by defining the
178+ ``toSearchableArray()`` method in your Eloquent model class. The
179+ following code demonstrates how to define ``toSearchableArray()`` to
180+ select only the ``plot`` and ``title`` fields for replication:
181+
182+ .. code-block:: php
183+
184+ class Movie extends Model
185+ {
186+ ....
187+ public function toSearchableArray(): array
188+ {
189+ return [
190+ 'plot' => $this->plot,
191+ 'title' => $this->title,
192+ ];
193+ }
194+ }
197195
198196.. TODO Use an External Search Engine
199197.. -----------------------------
0 commit comments