@@ -5,17 +5,17 @@ Full-Text Search with Scout
55===========================
66
77.. facet::
8- :name: genre
9- :values: reference
8+ :name: genre
9+ :values: reference
1010
1111.. meta::
12- :keywords: php framework, odm, code example, text search, atlas
12+ :keywords: php framework, odm, code example, text search, atlas
1313
1414.. contents:: On this page
15- :local:
16- :backlinks: none
17- :depth: 2
18- :class: singlecol
15+ :local:
16+ :backlinks: none
17+ :depth: 2
18+ :class: singlecol
1919
2020Overview
2121--------
@@ -41,9 +41,9 @@ functionality:
4141
4242.. note:: Deployment Compatibility
4343
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.
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.
4747
4848Scout for Atlas Search Tutorial
4949-------------------------------
@@ -52,146 +52,157 @@ 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 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
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;
55+ :style: connected
56+
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
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;
7986
80- use MongoDB\Laravel\Eloquent\Model;
81- use Laravel\Scout\Searchable;
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 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+
139+ .. step:: Create the Atlas Search index
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.
168+
169+ .. step:: Import data into the searchable collection
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.
82187
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
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:
105192
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 step. The Atlas Search index is named ``scout``
142- and has the following configuration:
143-
144- .. code-block:: json
145-
193+ .. code-block:: php
194+
195+ class Movie extends Model
146196 {
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 step:
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
197+ ....
198+ public function toSearchableArray(): array
185199 {
186- ....
187- public function toSearchableArray(): array
188- {
189- return [
190- 'plot' => $this->plot,
191- 'title' => $this->title,
192- ];
193- }
200+ return [
201+ 'plot' => $this->plot,
202+ 'title' => $this->title,
203+ ];
194204 }
205+ }
195206
196207After completing these steps, you can perform Atlas Search queries on the
197208``scout_movies`` collection in your {+odm-long+} application.
0 commit comments