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 an Algolia driver; however, 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:

  1. composer require laravel/scout

After installing Scout, you should publish the Scout configuration using the vendor:publish Artisan command. This command will publish the scout.php configuration file to your config directory:

  1. 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 to keep the model in sync with your search driver:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Laravel\Scout\Searchable;
  5. class Post extends Model
  6. {
  7. use Searchable;
  8. }

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:

  1. 'queue' => true,

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:

  1. composer require algolia/algoliasearch-client-php

Configuration

Configuring Model Indexes

Each Eloquent model is synced with a given search “index”, which contains all of the searchable records for that model. In other words, you can think of each index like a MySQL table. By default, each model will be persisted to an index matching the model’s typical “table” name. Typically, this is the plural form of the model name; however, you are free to customize the model’s index by overriding the searchableAs method on the model:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Laravel\Scout\Searchable;
  5. class Post extends Model
  6. {
  7. use Searchable;
  8. /**
  9. * Get the index name for the model.
  10. *
  11. * @return string
  12. */
  13. public function searchableAs()
  14. {
  15. return 'posts_index';
  16. }
  17. }

Configuring Searchable Data

By default, the entire toArray form of a given model will be persisted to its search index. If you would like to customize the data that is synchronized to the search index, you may override the toSearchableArray method on the model:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Laravel\Scout\Searchable;
  5. class Post extends Model
  6. {
  7. use Searchable;
  8. /**
  9. * Get the indexable data array for the model.
  10. *
  11. * @return array
  12. */
  13. public function toSearchableArray()
  14. {
  15. $array = $this->toArray();
  16. // Customize array...
  17. return $array;
  18. }
  19. }

Configuring The Model ID

By default, Scout will use the primary key of the model as the unique ID stored in the search index. If you need to customize this behavior, you may override the getScoutKey and the getScoutKeyName methods on the model:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Laravel\Scout\Searchable;
  5. class User extends Model
  6. {
  7. use Searchable;
  8. /**
  9. * Get the value used to index the model.
  10. *
  11. * @return mixed
  12. */
  13. public function getScoutKey()
  14. {
  15. return $this->email;
  16. }
  17. /**
  18. * Get the key name used to index the model.
  19. *
  20. * @return mixed
  21. */
  22. public function getScoutKeyName()
  23. {
  24. return 'email';
  25. }
  26. }

Identifying Users

Scout also allows you to auto identify users when using Algolia. Associating the authenticated user with search operations may be helpful when viewing your search analytics within Algolia’s dashboard. You can enable user identification by setting SCOUT_IDENTIFY to true in your .env file:

  1. SCOUT_IDENTIFY=true

Enabling this feature this will also pass the request’s IP address and your authenticated user’s primary identifier to Algolia so this data is associated with any search request that is made by the user.

Indexing

Batch Import

If you are installing Scout into an existing project, you may already have database records you need to import into your search driver. Scout provides an import Artisan command that you may use to import all of your existing records into your search indexes:

  1. php artisan scout:import "App\Models\Post"

The flush command may be used to remove all of a model’s records from your search indexes:

  1. php artisan scout:flush "App\Models\Post"

Adding Records

Once you have added the Laravel\Scout\Searchable trait to a model, all you need to do is save a model instance and it will automatically be added to your search index. If you have configured Scout to use queues this operation will be performed in the background by your queue worker:

  1. $order = new App\Models\Order;
  2. // ...
  3. $order->save();

Adding Via Query

If you would like to add a collection of models to your search index via an Eloquent query, you may chain the searchable method onto an Eloquent query. The searchable method will chunk the results of the query and add the records to your search index. Again, if you have configured Scout to use queues, all of the chunks will be added in the background by your queue workers:

  1. // Adding via Eloquent query...
  2. App\Models\Order::where('price', '>', 100)->searchable();
  3. // You may also add records via relationships...
  4. $user->orders()->searchable();
  5. // You may also add records via collections...
  6. $orders->searchable();

The searchable method can be considered an “upsert” operation. In other words, if the model record is already in your index, it will be updated. If it does not exist in the search index, it will be added to the index.

Updating Records

To update a searchable model, you only need to update the model instance’s properties and save the model to your database. Scout will automatically persist the changes to your search index:

  1. $order = App\Models\Order::find(1);
  2. // Update the order...
  3. $order->save();

You may also use the searchable method on an Eloquent query to update a collection of models. If the models do not exist in your search index, they will be created:

  1. // Updating via Eloquent query...
  2. App\Models\Order::where('price', '>', 100)->searchable();
  3. // You may also update via relationships...
  4. $user->orders()->searchable();
  5. // You may also update via collections...
  6. $orders->searchable();

Removing Records

To remove a record from your index, delete the model from the database. This form of removal is even compatible with soft deleted models:

  1. $order = App\Models\Order::find(1);
  2. $order->delete();

If you do not want to retrieve the model before deleting the record, you may use the unsearchable method on an Eloquent query instance or collection:

  1. // Removing via Eloquent query...
  2. App\Models\Order::where('price', '>', 100)->unsearchable();
  3. // You may also remove via relationships...
  4. $user->orders()->unsearchable();
  5. // You may also remove via collections...
  6. $orders->unsearchable();

Pausing Indexing

Sometimes you may need to perform a batch of Eloquent operations on a model without syncing the model data to your search index. You may do this using the withoutSyncingToSearch method. This method accepts a single callback which will be immediately executed. Any model operations that occur within the callback will not be synced to the model’s index:

  1. App\Models\Order::withoutSyncingToSearch(function () {
  2. // Perform model actions...
  3. });

Conditionally Searchable Model Instances

Sometimes you may need to only make a model searchable under certain conditions. For example, imagine you have App\Models\Post model that may be in one of two states: “draft” and “published”. You may only want to allow “published” posts to be searchable. To accomplish this, you may define a shouldBeSearchable method on your model:

  1. public function shouldBeSearchable()
  2. {
  3. return $this->isPublished();
  4. }

The shouldBeSearchable method is only applied when manipulating models through the save method, queries, or relationships. Directly making models or collections searchable using the searchable method will override the result of the shouldBeSearchable method:

  1. // Will respect "shouldBeSearchable"...
  2. App\Models\Order::where('price', '>', 100)->searchable();
  3. $user->orders()->searchable();
  4. $order->save();
  5. // Will override "shouldBeSearchable"...
  6. $orders->searchable();
  7. $order->searchable();

Searching

You may begin searching a model using the search method. The search method accepts a single string that will be used to search your models. You should then chain the get method onto the search query to retrieve the Eloquent models that match the given search query:

  1. $orders = App\Models\Order::search('Star Trek')->get();

Since Scout searches return a collection of Eloquent models, you may even return the results directly from a route or controller and they will automatically be converted to JSON:

  1. use Illuminate\Http\Request;
  2. Route::get('/search', function (Request $request) {
  3. return App\Models\Order::search($request->search)->get();
  4. });

If you would like to get the raw results before they are converted to Eloquent models, you should use the raw method:

  1. $orders = App\Models\Order::search('Star Trek')->raw();

Search queries will typically be performed on the index specified by the model’s searchableAs method. However, you may use the within method to specify a custom index that should be searched instead:

  1. $orders = App\Models\Order::search('Star Trek')
  2. ->within('tv_shows_popularity_desc')
  3. ->get();

Where Clauses

Scout allows you to add simple “where” clauses to your search queries. Currently, these clauses only support basic numeric equality checks, and are primarily useful for scoping search queries by a tenant ID. Since a search index is not a relational database, more advanced “where” clauses are not currently supported:

  1. $orders = App\Models\Order::search('Star Trek')->where('user_id', 1)->get();

Pagination

In addition to retrieving a collection of models, you may paginate your search results using the paginate method. This method will return a Paginator instance just as if you had paginated a traditional Eloquent query:

  1. $orders = App\Models\Order::search('Star Trek')->paginate();

You may specify how many models to retrieve per page by passing the amount as the first argument to the paginate method:

  1. $orders = App\Models\Order::search('Star Trek')->paginate(15);

Once you have retrieved the results, you may display the results and render the page links using Blade just as if you had paginated a traditional Eloquent query:

  1. <div class="container">
  2. @foreach ($orders as $order)
  3. {{ $order->price }}
  4. @endforeach
  5. </div>
  6. {{ $orders->links() }}

Soft Deleting

If your indexed models are soft deleting and you need to search your soft deleted models, set the soft_delete option of the config/scout.php configuration file to true:

  1. 'soft_delete' => true,

When this configuration option is true, Scout will not remove soft deleted models from the search index. Instead, it will set a hidden __soft_deleted attribute on the indexed record. Then, you may use the withTrashed or onlyTrashed methods to retrieve the soft deleted records when searching:

  1. // Include trashed records when retrieving results...
  2. $orders = App\Models\Order::search('Star Trek')->withTrashed()->get();
  3. // Only include trashed records when retrieving results...
  4. $orders = App\Models\Order::search('Star Trek')->onlyTrashed()->get();

{tip} When a soft deleted model is permanently deleted using forceDelete, Scout will remove it from the search index automatically.

Customizing Engine Searches

If you need to customize the search behavior of an engine you may pass a callback as the second argument to the search method. For example, you could use this callback to add geo-location data to your search options before the search query is passed to Algolia:

  1. use Algolia\AlgoliaSearch\SearchIndex;
  2. App\Models\Order::search('Star Trek', function (SearchIndex $algolia, string $query, array $options) {
  3. $options['body']['query']['bool']['filter']['geo_distance'] = [
  4. 'distance' => '1000km',
  5. 'location' => ['lat' => 36, 'lon' => 111],
  6. ];
  7. return $algolia->search($query, $options);
  8. })->get();

Custom Engines

Writing The Engine

If one of the built-in Scout search engines doesn’t fit your needs, you may write your own custom engine and register it with Scout. Your engine should extend the Laravel\Scout\Engines\Engine abstract class. This abstract class contains eight methods your custom engine must implement:

  1. use Laravel\Scout\Builder;
  2. abstract public function update($models);
  3. abstract public function delete($models);
  4. abstract public function search(Builder $builder);
  5. abstract public function paginate(Builder $builder, $perPage, $page);
  6. abstract public function mapIds($results);
  7. abstract public function map(Builder $builder, $results, $model);
  8. abstract public function getTotalCount($results);
  9. abstract public function flush($model);

You may find it helpful to review the implementations of these methods on the Laravel\Scout\Engines\AlgoliaEngine class. This class will provide you with a good starting point for learning how to implement each of these methods in your own engine.

Registering The Engine

Once you have written your custom engine, you may register it with Scout using the extend method of the Scout engine manager. You should call the extend method from the boot method of your AppServiceProvider or any other service provider used by your application. For example, if you have written a MySqlSearchEngine, you may register it like so:

  1. use Laravel\Scout\EngineManager;
  2. /**
  3. * Bootstrap any application services.
  4. *
  5. * @return void
  6. */
  7. public function boot()
  8. {
  9. resolve(EngineManager::class)->extend('mysql', function () {
  10. return new MySqlSearchEngine;
  11. });
  12. }

Once your engine has been registered, you may specify it as your default Scout driver in your config/scout.php configuration file:

  1. 'driver' => 'mysql',

Builder Macros

If you would like to define a custom builder method, you may use the macro method on the Laravel\Scout\Builder class. Typically, “macros” should be defined within a service provider’s boot method:

  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\Facades\Response;
  4. use Illuminate\Support\ServiceProvider;
  5. use Laravel\Scout\Builder;
  6. class ScoutMacroServiceProvider extends ServiceProvider
  7. {
  8. /**
  9. * Register the application's scout macros.
  10. *
  11. * @return void
  12. */
  13. public function boot()
  14. {
  15. Builder::macro('count', function () {
  16. return $this->engine->getTotalCount(
  17. $this->engine()->search($this)
  18. );
  19. });
  20. }
  21. }

The macro function accepts a name as its first argument, and a Closure as its second. The macro’s Closure will be executed when calling the macro name from a Laravel\Scout\Builder implementation:

  1. App\Models\Order::search('Star Trek')->count();