open-southeners / laravel-scout-advaced-meilisearch
与 Laravel Scout 的高级 Meilisearch 集成
Requires
- php: ^8.0
- illuminate/console: ^9.0 || ^10.0 || ^11.0
- laravel/scout: ^10.0
- meilisearch/meilisearch-php: ^1.0
Requires (Dev)
- guzzlehttp/guzzle: ^7.1
- http-interop/http-factory-guzzle: ^1.0
- larastan/larastan: ^2.0
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^9.0 || ^10.0
README
与 Laravel Scout 的高级 Meilisearch 集成。
入门指南
使用 Composer 安装包
composer require open-southeners/laravel-scout-advanced-meilisearch
可筛选和可排序属性
为了将可筛选和可排序属性发送到您的 Meilisearch 服务器,请按如下方式配置您已经可搜索的模型
use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; class Tag extends Model { use Searchable; /** * Get the indexable data array for the model. * * @return array */ public function toSearchableArray() { return [ 'id' => $this->id, 'name' => $this->name, 'slug' => $this->slug, ]; } /** * Get the search sortable attributes array for the model. * * @return array<string> */ public function searchableFilters(): array { return ['name']; } /** * Get the search sortable attributes array for the model. * * @return array<string> */ public function searchableSorts(): array { return ['slug']; } }
使用 PHP 属性
如果您正在使用 PHP 8,您可以通过模型类上的属性或 toSearchableArray
方法来完成此操作
use Illuminate\Foundation\Auth\User as Authenticatable; use Laravel\Scout\Searchable; use OpenSoutheners\LaravelScoutAdvancedMeilisearch\Attributes\ScoutSearchableAttributes; #[ScoutSearchableAttributes(filterable: ['email'], sortable: ['name'])] class User extends Authenticatable { use Searchable; /** * Get the indexable data array for the model. * * @return array */ public function toSearchableArray() { return [ 'id' => $this->id, 'name' => $this->name, 'email' => $this->email, ]; } }
最后,运行以下 artisan 命令
php artisan scout:update "App\Models\User"
您也可以使用 --wait
选项运行此命令,这将告诉命令等待任务完成
php artisan scout:update "App\Models\User" --wait
请记住检查有关这些筛选和排序的官方文档。
转储
使用以下命令创建 Meilisearch 数据转储(将保存在您的 Meilisearch 服务器上的数据备份)
php artisan scout:dump
与 scout:update
命令一样,此命令也具有 --wait
选项
php artisan scout:dump --wait
任务
通过命令行列出所有任务,只需运行以下命令
php artisan scout:tasks
它们甚至可以被筛选!(运行 --help
查看更多选项)
php artisan scout:tasks --status=succeeded
取消队列中的任务
您也可以使用非常简单的命令取消任务,您可以取消一个特定的任务或多个
php artisan scout:tasks-cancel 1
前面的命令将取消 UID = 1 的任务。如果您想取消多个,可以将它们用逗号分隔或使用类似以下选项
php artisan scout:tasks-cancel --before-enqueued=1d
因此,这将取消所有在 1 天前入队(也可以发送 1m、1y... 在此背景中,这使用 Carbon::now()->add()
和 Carbon::now()->sub()
方法)的任务
修剪完成的任务
由于取消任务不会使其从任务历史中消失,您只需运行以下命令即可
php artisan scout:tasks-prune
出于调试目的的安全起见,此命令不会删除失败的任务,如果您想这样做,请像这样运行命令使用 --include-failed
php artisan scout:tasks-prune --include-failed
不用担心,这不会删除尚未完成入队任务,正如 Meilisearch 官方文档中所述(请参阅下面的链接)。
合作伙伴
许可证
此包是开源软件,根据MIT 许可证授权。