@@ -55,154 +55,154 @@ application to support Atlas Search queries.
55
55
:style: connected
56
56
57
57
.. step:: Install the 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
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
65
66
66
.. step:: Add the Searchable trait to your model
67
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
- }
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
89
90
90
.. step:: Configure Scout in your application
91
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 the value of the ``SCOUT_DRIVER`` environment variable as
117
- the default search driver, or ``mongodb`` if the environment
118
- variable is not set
119
-
120
- - Specifies ``scout_`` as the prefix for the collection name of the
121
- searchable collection
122
-
123
- Set the following environment variable in your application's
124
- ``.env`` file to select ``mongodb`` as the default search driver:
125
-
126
- .. code-block:: none
127
- :caption: .env
128
-
129
- SCOUT_DRIVER=mongodb
130
-
131
- .. tip:: Queueing
132
-
133
- When using Scout, consider configuring a queue driver to reduce
134
- response times for your application's web interface. To learn more,
135
- see the `Queuing section
136
- <https://laravel.com/docs/{+laravel-docs-version+}/scout#queueing>`__
137
- of the Laravel Scout documentation and the :ref:`laravel-queues` guide.
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 the value of the ``SCOUT_DRIVER`` environment variable as
117
+ the default search driver, or ``mongodb`` if the environment
118
+ variable is not set
119
+
120
+ - Specifies ``scout_`` as the prefix for the collection name of the
121
+ searchable collection
122
+
123
+ Set the following environment variable in your application's
124
+ ``.env`` file to select ``mongodb`` as the default search driver:
125
+
126
+ .. code-block:: none
127
+ :caption: .env
128
+
129
+ SCOUT_DRIVER=mongodb
130
+
131
+ .. tip:: Queueing
132
+
133
+ When using Scout, consider configuring a queue driver to reduce
134
+ response times for your application's web interface. To learn more,
135
+ see the `Queuing section
136
+ <https://laravel.com/docs/{+laravel-docs-version+}/scout#queueing>`__
137
+ of the Laravel Scout documentation and the :ref:`laravel-queues` guide.
138
138
139
139
.. step:: Create the Atlas Search index
140
140
141
- After you configure Scout and set your default search driver, you can
142
- create your searchable collection and search index by running the
143
- following command from your application's root directory:
144
-
145
- .. code-block:: bash
146
-
147
- php artisan scout:index 'App\Models\Movie'
148
-
149
- Because you set MongoDB as the default search driver, the preceding
150
- command creates the search collection with an Atlas Search index in your
151
- MongoDB database. The collection is named ``scout_movies``, based on the prefix
152
- set in the preceding step. The Atlas Search index is named ``scout``
153
- and has the following configuration:
154
-
155
- .. code-block:: json
156
-
157
- {
158
- "mappings": {
159
- "dynamic": true
160
- }
161
- }
162
-
163
- .. note::
164
-
165
- MongoDB can take up to a minute to create and finalize
166
- an Atlas Search index, so the ``scout:index`` command might not
167
- return a success message immediately.
141
+ After you configure Scout and set your default search driver, you can
142
+ create your searchable collection and search index by running the
143
+ following command from your application's root directory:
144
+
145
+ .. code-block:: bash
146
+
147
+ php artisan scout:index 'App\Models\Movie'
148
+
149
+ Because you set MongoDB as the default search driver, the preceding
150
+ command creates the search collection with an Atlas Search index in your
151
+ MongoDB database. The collection is named ``scout_movies``, based on the prefix
152
+ set in the preceding step. The Atlas Search index is named ``scout``
153
+ and has the following configuration:
154
+
155
+ .. code-block:: json
156
+
157
+ {
158
+ "mappings": {
159
+ "dynamic": true
160
+ }
161
+ }
162
+
163
+ .. note::
164
+
165
+ MongoDB can take up to a minute to create and finalize
166
+ an Atlas Search index, so the ``scout:index`` command might not
167
+ return a success message immediately.
168
168
169
169
.. step:: Import data into the searchable collection
170
170
171
- You can use Scout to replicate data from a source collection into a
172
- searchable collection. The following command replicates and indexes data
173
- from the ``movies`` collection into the ``scout_movies`` collection
174
- created in the preceding step:
175
-
176
- .. code-block:: bash
177
-
178
- php artisan scout:import 'App\Models\Movie'
179
-
180
- The documents are automatically indexed for Atlas Search queries.
181
-
182
- .. tip:: Select Fields to Import
183
-
184
- You might not need all the fields from your source documents in your
185
- searchable collection. Limiting the fields you replicate can improve
186
- your application's speed and performance.
187
-
188
- You can select specific fields to import by defining the
189
- ``toSearchableArray()`` method in your Eloquent model class. The
190
- following code demonstrates how to define ``toSearchableArray()`` to
191
- select only the ``plot`` and ``title`` fields for replication:
192
-
193
- .. code-block:: php
194
-
195
- class Movie extends Model
196
- {
197
- ....
198
- public function toSearchableArray(): array
199
- {
200
- return [
201
- 'plot' => $this->plot,
202
- 'title' => $this->title,
203
- ];
204
- }
205
- }
171
+ You can use Scout to replicate data from a source collection into a
172
+ searchable collection. The following command replicates and indexes data
173
+ from the ``movies`` collection into the ``scout_movies`` collection
174
+ created in the preceding step:
175
+
176
+ .. code-block:: bash
177
+
178
+ php artisan scout:import 'App\Models\Movie'
179
+
180
+ The documents are automatically indexed for Atlas Search queries.
181
+
182
+ .. tip:: Select Fields to Import
183
+
184
+ You might not need all the fields from your source documents in your
185
+ searchable collection. Limiting the fields you replicate can improve
186
+ your application's speed and performance.
187
+
188
+ You can select specific fields to import by defining the
189
+ ``toSearchableArray()`` method in your Eloquent model class. The
190
+ following code demonstrates how to define ``toSearchableArray()`` to
191
+ select only the ``plot`` and ``title`` fields for replication:
192
+
193
+ .. code-block:: php
194
+
195
+ class Movie extends Model
196
+ {
197
+ ....
198
+ public function toSearchableArray(): array
199
+ {
200
+ return [
201
+ 'plot' => $this->plot,
202
+ 'title' => $this->title,
203
+ ];
204
+ }
205
+ }
206
206
207
207
After completing these steps, you can perform Atlas Search queries on the
208
208
``scout_movies`` collection in your {+odm-long+} application.
0 commit comments