@@ -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,151 @@ 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- '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- }
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;
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 step. 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 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
185+ {
186+ ....
187+ public function toSearchableArray(): array
188+ {
189+ return [
190+ 'plot' => $this->plot,
191+ 'title' => $this->title,
192+ ];
193+ }
194+ }
195+
196+ After completing these steps, you can perform Atlas Search queries on the
197+ ``scout_movies`` collection in your {+odm-long+} application.
198+
199+ .. TODO add link to Atlas Search guide
195200
196201.. TODO Use an External Search Engine
197202.. -----------------------------
0 commit comments