Create an aggregator
To create a new aggregator, use thescout:make-aggregator Artisan command.
This command creates a new aggregator class in the app/Search directory:
Command line
$models property of the class to identify the models to aggregate:
PHP
bootSearchable method on the aggregator you want to register.
For this, you should use the boot method of one of your service providers.
The following example registers the aggregator in the App\AppServiceProvider:
PHP
Aggregator::bootSearchables method, passing your aggregator classes as argument:
PHP
Conditionally sync an aggregator
ForSearchable models, you can use the shouldBeSearchable method to conditionally change which results you want to index.
For aggregators, you can define a shouldBeSearchable method that calls the shouldBeSearchable method of the aggregated model.
PHP
shouldBeSearchable method with any condition that doesn’t depend on its grouped models.
Search an aggregator
An aggregator is a standardSearchable class.
You can search models on the aggregator using the search method:
PHP
Be careful, the
$models array may contain different types of models instances.raw method.
Be aware that each result may contain a different structure when using this method:
PHP
JSON
toSearchableArray on the
each Searchable classes or directly on the aggregator class.
Eager loading relationships
Sometimes you may want to eager load a relationship of an aggregated model. To eager load relationships, use the property$relations of your aggregator class.
For example:
PHP
Turning off syncing per model
By default, the aggregator creates one aggregated index (in this case, “news”). But, Laravel Scout also still indexes theArticle and Event models in their own “articles” and “events” indices.
To stop this, turn off indexing for these models globally.
In your AppServiceProvider, add the following:
PHP
shouldBeSearchable
method in this case because the method doesn’t know about the destination index.
The
scout:import command doesn’t take calls to disableSearchSyncing into account. Disabling search syncing only applies to updates to models made by the application, and not to bulk import operations through the scout:import command.