-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-46479: document Scout integration #3261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
bd5b0c2
DOCSP-46479: document Scout integration
rustagir 239b02a
NR PR fixes 1
rustagir ccaac91
fix spacing
rustagir 0ee85b1
fix spacing
rustagir 5edb13d
fix spacing
rustagir e7b0fe3
fix spacing
rustagir 45a7e79
NR PR fixes 2
rustagir 33a0275
JT tech comment
rustagir eac4c21
fix spacing
rustagir 619fe56
JT tech review 1
rustagir bf82dd7
Merge branch '5.x' of github.com:mongodb/laravel-mongodb into DOCSP-4…
rustagir e02220a
JT tech review 1
rustagir 7c988f0
custom index
rustagir fbf203e
link to atlas doc
rustagir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
.. _laravel-scout: | ||
|
||
=========================== | ||
Full-Text Search with Scout | ||
=========================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: php framework, odm, code example, text search, atlas | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use the Laravel Scout feature in | ||
your {+odm-long+} application. Scout enables you to implement full-text | ||
search on your Eloquent models. To learn more, see `Laravel Scout | ||
<https://laravel.com/docs/{+laravel-docs-version+}/scout>`__ in the | ||
Laravel documentation. | ||
|
||
The Scout integration for {+odm-long+} provides the following | ||
functionality: | ||
|
||
- Provides an abstraction to create :atlas:`Atlas Search indexes | ||
</atlas-search/manage-indexes/>` from any MongoDB or SQL model. | ||
|
||
.. important:: Use Schema Builder to Create Search Indexes | ||
|
||
If your documents are already in MongoDB, create Search indexes | ||
by using {+php-library+} or ``Schema`` builder methods to improve | ||
search query performance. To learn more about creating Search | ||
indexes, see the :ref:`laravel-as-index` section of the Atlas | ||
Search guide. | ||
|
||
- Enables you to automatically replicate data from MongoDB into a | ||
search engine such as `Meilisearch <https://www.meilisearch.com/>`__ | ||
or `Algolia <https://www.algolia.com/>`__. You can use a MongoDB Eloquent | ||
model as the source to import and index. To learn more about indexing | ||
to a search engine, see the `Indexing | ||
<https://laravel.com/docs/{+laravel-docs-version+}/scout#indexing>`__ | ||
section of the Laravel Scout documentation. | ||
|
||
.. important:: Deployment Compatibility | ||
|
||
You can use Laravel Scout only when you connect to MongoDB Atlas | ||
deployments. This feature is not available for self-managed | ||
deployments. | ||
|
||
Scout for Atlas Search Tutorial | ||
------------------------------- | ||
|
||
This tutorial demonstrates how to use Scout to compound and index | ||
documents for MongoDB Atlas Search from Eloquent models (MongoDB or SQL). | ||
|
||
.. procedure:: | ||
:style: connected | ||
|
||
.. step:: Install the Scout package | ||
|
||
Before you can use Scout in your application, run the following | ||
command from your application's root directory to install the | ||
``laravel/scout`` package: | ||
|
||
.. code-block:: bash | ||
|
||
composer require laravel/scout | ||
|
||
.. step:: Add the Searchable trait to your model | ||
|
||
Add the ``Laravel\Scout\Searchable`` trait to an Eloquent model to make | ||
it searchable. The following example adds this trait to the ``Movie`` | ||
model, which represents documents in the ``sample_mflix.movies`` | ||
collection: | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: php | ||
:emphasize-lines: 6, 10 | ||
|
||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use MongoDB\Laravel\Eloquent\Model; | ||
use Laravel\Scout\Searchable; | ||
|
||
class Movie extends Model | ||
{ | ||
use Searchable; | ||
|
||
protected $connection = 'mongodb'; | ||
} | ||
|
||
You can also use the ``Searchable`` trait to reformat documents, | ||
embed related documents, or transform document values. To learn | ||
more, see the `Configuring Searchable Data | ||
<https://laravel.com/docs/{+laravel-docs-version+}/scout#configuring-searchable-data>`__ | ||
section of the Laravel Scout documentation. | ||
|
||
.. step:: Configure Scout in your application | ||
|
||
Ensure that your application is configured to use MongoDB as its | ||
database connection. To learn how to configure MongoDB, see the | ||
:ref:`laravel-quick-start-connect-to-mongodb` section of the Quick Start | ||
guide. | ||
|
||
To configure Scout in your application, create a file named | ||
``scout.php`` in your application's ``config`` directory. Paste the | ||
following code into the file to configure Scout: | ||
|
||
.. code-block:: php | ||
:caption: config/scout.php | ||
|
||
<?php | ||
|
||
return [ | ||
'driver' => env('SCOUT_DRIVER', 'mongodb'), | ||
'mongodb' => [ | ||
'connection' => env('SCOUT_MONGODB_CONNECTION', 'mongodb'), | ||
], | ||
'prefix' => env('SCOUT_PREFIX', 'scout_'), | ||
]; | ||
|
||
The preceding code specifies the following configuration: | ||
|
||
- Uses the value of the ``SCOUT_DRIVER`` environment variable as | ||
the default search driver, or ``mongodb`` if the environment | ||
variable is not set | ||
|
||
- Specifies ``scout_`` as the prefix for the collection name of the | ||
searchable collection | ||
|
||
In the ``config/scout.php`` file, you can also specify a custom | ||
Atlas Search index definition. To learn more, see the :ref:`custom | ||
index definition example <laravel-scout-custom-index>` in the | ||
following step. | ||
|
||
Set the following environment variable in your application's | ||
``.env`` file to select ``mongodb`` as the default search driver: | ||
|
||
.. code-block:: none | ||
:caption: .env | ||
|
||
SCOUT_DRIVER=mongodb | ||
|
||
.. tip:: Queueing | ||
|
||
When using Scout, consider configuring a queue driver to reduce | ||
response times for your application's web interface. To learn more, | ||
see the `Queuing section | ||
<https://laravel.com/docs/{+laravel-docs-version+}/scout#queueing>`__ | ||
of the Laravel Scout documentation and the :ref:`laravel-queues` guide. | ||
|
||
.. step:: Create the Atlas Search index | ||
|
||
After you configure Scout and set your default search driver, you can | ||
create your searchable collection and search index by running the | ||
following command from your application's root directory: | ||
|
||
.. code-block:: bash | ||
|
||
php artisan scout:index 'App\Models\Movie' | ||
|
||
Because you set MongoDB as the default search driver, the preceding | ||
command creates the search collection with an Atlas Search index in your | ||
MongoDB database. The collection is named ``scout_movies``, based on the prefix | ||
set in the preceding step. The Atlas Search index is named ``scout`` | ||
and has the following configuration by default: | ||
|
||
.. code-block:: json | ||
|
||
{ | ||
"mappings": { | ||
"dynamic": true | ||
} | ||
} | ||
|
||
.. _laravel-scout-custom-index: | ||
|
||
To customize the index definition, add the ``index-definitions`` | ||
configuration to the ``mongodb`` entry in your | ||
``config/scout.php`` file. The following code demonstrates how to | ||
specify a custom index definition to create on the | ||
``scout_movies`` collection: | ||
|
||
.. code-block:: php | ||
|
||
'mongodb' => [ | ||
'connection' => env('SCOUT_MONGODB_CONNECTION', 'mongodb'), | ||
'index-definitions' => [ | ||
'scout_movies' => [ | ||
'mappings' => [ | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'dynamic' => false, | ||
'fields' => ['title' => ['type' => 'string']] | ||
] | ||
] | ||
] | ||
], ... | ||
|
||
To learn more about defining Atlas Search index definitions, see the | ||
:atlas:`Define Field Mappings | ||
</atlas-search/define-field-mappings/>` guide in the Atlas | ||
documentation. | ||
|
||
.. note:: | ||
|
||
MongoDB can take up to a minute to create and finalize | ||
an Atlas Search index, so the ``scout:index`` command might not | ||
return a success message immediately. | ||
|
||
.. step:: Import data into the searchable collection | ||
|
||
You can use Scout to replicate data from a source collection | ||
modeled by your Eloquent model into a searchable collection. The | ||
following command replicates and indexes data from the ``movies`` | ||
collection into the ``scout_movies`` collection indexed in the | ||
preceding step: | ||
|
||
.. code-block:: bash | ||
|
||
php artisan scout:import 'App\Models\Movie' | ||
|
||
The documents are automatically indexed for Atlas Search queries. | ||
|
||
.. tip:: Select Fields to Import | ||
|
||
You might not need all the fields from your source documents in your | ||
searchable collection. Limiting the amount of data you replicate can improve | ||
your application's speed and performance. | ||
|
||
You can select specific fields to import by defining the | ||
``toSearchableArray()`` method in your Eloquent model class. The | ||
following code demonstrates how to define ``toSearchableArray()`` to | ||
select only the ``plot`` and ``title`` fields for replication: | ||
|
||
.. code-block:: php | ||
|
||
class Movie extends Model | ||
{ | ||
.... | ||
public function toSearchableArray(): array | ||
{ | ||
return [ | ||
'plot' => $this->plot, | ||
'title' => $this->title, | ||
]; | ||
} | ||
} | ||
|
||
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
After completing these steps, you can perform Atlas Search queries on the | ||
``scout_movies`` collection in your {+odm-long+} application. To learn | ||
how to perform full-text searches, see the :ref:`laravel-atlas-search` | ||
guide. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.