needbrainz / scout-typesense-aggregator
Laravel Scout Aggregator for Typesense
Fund package maintenance!
NeedBrainz
Requires
- php: ^8.3
- illuminate/contracts: ^10.0||^11.0||^12.0
- laravel/scout: ^10.
- spatie/laravel-package-tools: ^1.16
- typesense/typesense-php: ^4.9||^5.0
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^10.0.0||^9.0.0||^8.22.0
- pestphp/pest: ^1.21.3||^2.34||^3.0
- pestphp/pest-plugin-arch: ^2.0||^3.0
- pestphp/pest-plugin-laravel: ^1.2|^2.3|^3.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- spatie/laravel-ray: ^1.35
README
Laravel Scout Aggregator for Typesense extends Laravel Scout with support for aggregated search across multiple models using Typesense. Based on Algolia's Scout Extended package, it adapts and extends the aggregator functionality for the open-source Typesense engine.
Installation
You can install the package via composer:
composer require needbrainz/scout-typesense-aggregator
Usage
To create a new aggregator, you can use the make:aggregator
command:
php artisan make:typesense-aggregator MyAggregator
<?php namespace App\Search; use NeedBrainz\TypesenseAggregator\TypesenseAggregator; class MyAggregator extends TypesenseAggregator { /** * The names of the models that should be aggregated. * * @var string[] */ protected $models = [ App\Models\MyModel::class, App\Models\MyOtherModel::class, ]; }
Then configure the aggregator settings in your config/scout.php
file:
'typesense' => [ 'model-settings' => [ MyAggregator::class => [ 'collection-schema' => [ 'fields' => [ [ 'name' => 'id', 'type' => 'string', ], // Your aggregated fields [ 'name' => 'name', 'type' => 'string', ], [ 'name' => 'description', 'type' => 'string', ], [ 'name' => 'created_at', 'type' => 'int64', ], ], 'default_sorting_field' => 'created_at', ], 'search-parameters' => [ 'query_by' => 'name,description', ] ], ]
Register your Aggregator in an appropriate service provider, such as App\Providers\AppServiceProvider
:
use App\Search\MyAggregator; public function boot(): void { MyAggregator::bootSearchable(); }
By default the Aggregator will merge the models toSearchableArrray and set the custom id coming from the Aggregator getScoutKey($model)
which will create an id in the format ModelClassName::ModelScoutKey
. This format can then be reverted with the extractScoutKey($key)
for models retrieve. So if you change the syntax, don't forget to override the getScoutKey
and extractScoutKey
methods in your Aggregator class.
If the structure of the models is different, you can override the toSearchableArray
method in your Aggregator class to customize the merging of the models.
public function toSearchableArray(): array { $array = [ 'id' => (string )$this->getScoutKey(), 'created_at' => $this->model->created_at->timestamp, // default empty values 'name' => '', 'description' => '', ]; // Customize the merging of the models here if ($this->model instanceof MyModel) { $array['name'] = (string) $this->model->name; $array['description'] = (string) $this->model->description; } elseif ($this->model instanceof MyOtherModel) { $array['name'] = (string) $this->model->title; $array['description'] = (string) $this->model->summary; } return $array; }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.