1
1
.. _laravel-scout:
2
2
3
- ============================
4
- Integrated Search with Scout
5
- ============================
3
+ ===========================
4
+ Full-Text Search with Scout
5
+ ===========================
6
6
7
7
.. facet::
8
- :name: genre
9
- :values: reference
8
+ :name: genre
9
+ :values: reference
10
10
11
11
.. meta::
12
- :keywords: php framework, odm, code example, text search, atlas
12
+ :keywords: php framework, odm, code example, text search, atlas
13
13
14
14
.. 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
19
19
20
20
Overview
21
21
--------
@@ -26,11 +26,11 @@ search on your Eloquent models. To learn more, see `Laravel Scout
26
26
<https://laravel.com/docs/{+laravel-docs-version+}/scout>`__ in the
27
27
Laravel documentation.
28
28
29
- The Scout Integration for {+odm-long+} provides the following
29
+ The Scout integration for {+odm-long+} provides the following
30
30
functionality:
31
31
32
32
- Provides an abstraction to create search indexes on documents in
33
- MongoDB collections and other search engines. In MongoDB, this feature
33
+ MongoDB collections and external search engines. In MongoDB, this feature
34
34
allows you to create :atlas:`Atlas Search indexes
35
35
</atlas-search/manage-indexes/>`.
36
36
@@ -41,158 +41,161 @@ functionality:
41
41
42
42
.. note:: Deployment Compatibility
43
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
- -----------------------------
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
+ Scout for Atlas Search Tutorial
49
+ -------------------------------
50
+
51
+ This section demonstrates how to use the Scout integration in your
52
+ application to support Atlas Search queries.
53
+
54
+ .. 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
+ }
197
+
198
+ .. TODO Use an External Search Engine
199
+ .. -----------------------------
197
200
198
201
.. TODO https://jira.mongodb.org/browse/DOCSP-45125
0 commit comments