Laravel Scout
Introduction
Laravel Scout provides a simple, driver based solution for adding full-text search to your Eloquent models. Using model observers, Scout will automatically keep your search indexes in sync with your Eloquent records.
Currently, Scout ships with Algolia, Meilisearch, Typesense, and MySQL / PostgreSQL (database
) drivers. In addition, Scout includes a "collection" driver that is designed for local development usage and does not require any external dependencies or third-party services. Furthermore, writing custom drivers is simple and you are free to extend Scout with your own search implementations.
Installation
First, install Scout via the Composer package manager:
composer require laravel/scout
After installing Scout, you should publish the Scout configuration file using the vendor:publish
Artisan command. This command will publish the scout.php
configuration file to your application's config
directory:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Finally, add the Laravel\Scout\Searchable
trait to the model you would like to make searchable. This trait will register a model observer that will automatically keep the model in sync with your search driver:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
}
Queueing
While not strictly required to use Scout, you should strongly consider configuring a queue driver before using the library. Running a queue worker will allow Scout to queue all operations that sync your model information to your search indexes, providing much better response times for your application's web interface.
Once you have configured a queue driver, set the value of the queue
option in your config/scout.php
configuration file to true
:
'queue' => true,
Even when the queue
option is set to false
, it's important to remember that some Scout drivers like Algolia and Meilisearch always index records asynchronously. Meaning, even though the index operation has completed within your Laravel application, the search engine itself may not reflect the new and updated records immediately.
To specify the connection and queue that your Scout jobs utilize, you may define the queue
configuration option as an array:
'queue' => [
'connection' => 'redis',
'queue' => 'scout'
],
Of course, if you customize the connection and queue that Scout jobs utilize, you should run a queue worker to process jobs on that connection and queue:
php artisan queue:work redis --queue=scout
Driver Prerequisites
Algolia
When using the Algolia driver, you should configure your Algolia id
and secret
credentials in your config/scout.php
configuration file. Once your credentials have been configured, you will also need to install the Algolia PHP SDK via the Composer package manager:
composer require algolia/algoliasearch-client-php
Meilisearch
Meilisearch is a blazingly fast and open source search engine. If you aren't sure how to install Meilisearch on your local machine, you may use Laravel Sail, Laravel's officially supported Docker development environment.
When using the Meilisearch driver you will need to install the Meilisearch PHP SDK via the Composer package manager:
composer require meilisearch/meilisearch-php http-interop/http-factory-guzzle
Then, set the SCOUT_DRIVER
environment variable as well as your Meilisearch host
and key
credentials within your application's .env
file:
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://127.0.0.1:7700
MEILISEARCH_KEY=masterKey
For more information regarding Meilisearch, please consult the Meilisearch documentation.
In addition, you should ensure that you install a version of meilisearch/meilisearch-php
that is compatible with your Meilisearch binary version by reviewing Meilisearch's documentation regarding binary compatibility.
When upgrading Scout on an application that utilizes Meilisearch, you should always review any additional breaking changes to the Meilisearch service itself.
Typesense
Typesense is a lightning-fast, open source search engine and supports keyword search, semantic search, geo search, and vector search.
You can self-host Typesense or use Typesense Cloud.
To get started using Typesense with Scout, install the Typesense PHP SDK via the Composer package manager:
composer require typesense/typesense-php
Then, set the SCOUT_DRIVER
environment variable as well as your Typesense host and API key credentials within your application's .env file:
SCOUT_DRIVER=typesense
TYPESENSE_API_KEY=masterKey
TYPESENSE_HOST=localhost
If needed, you may also specify your installation's port, path, and protocol:
TYPESENSE_PORT=8108
TYPESENSE_PATH=
TYPESENSE_PROTOCOL=http
Additional settings and schema definitions for your Typesense collections can be found within your application's config/scout.php
configuration file. For more information regarding Typesense, please consult the Typesense documentation.
Preparing Data for Storage in Typesense
When utilizing Typesense, your searchable model's must define a toSearchableArray
method that casts your model's primary key to a string and creation date to a UNIX timestamp:
/**
* Get the indexable data array for the model.
*
* @return array<string, mixed>
*/
public function toSearchableArray()
{
return array_merge($this->toArray(),[
'id' => (string) $this->id,
'created_at' => $this->created_at->timestamp,
]);
}
You should also define your Typesense collection schemas in your application's config/scout.php
file. A collection schema describes the data types of each field that is searchable via Typesense. For more information on all available schema options, please consult the Typesense documentation.
If you need to change your Typesense collection's schema after it has been defined, you may either run scout:flush
and scout:import
, which will delete all existing indexed data and recreate the schema. Or, you may use Typesense's API to modify the collection's schema without removing any indexed data.
If your searchable model is soft deletable, you should define a __soft_deleted
field in the model's corresponding Typesense schema within your application's config/scout.php
configuration file:
User::class => [
'collection-schema' => [
'fields' => [
// ...
[
'name' => '__soft_deleted',
'type' => 'int32',
'optional' => true,
],
],
],
],
Dynamic Search Parameters
Typesense allows you to modify your search parameters dynamically when performing a search operation via the options
method:
use App\Models\Todo;
Todo::search('Groceries')->options([
'query_by' => 'title, description'
])->get();