open-southeners/laravel-scout-advanced-meilisearch

高级 Meilisearch 与 Laravel Scout 集成

4.1.0 2024-05-24 11:26 UTC

This package is auto-updated.

Last update: 2024-09-24 12:10:16 UTC


README

高级 Meilisearch 与 Laravel Scout 集成。

入门指南

使用 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

在此处了解更多关于 Meilisearch 转储的信息.

任务

通过命令行列出所有任务,只需运行以下命令

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 官方文档所述(请参阅下面的链接)。

在此处了解更多关于 Meilisearch 任务的信息.

合作伙伴

skore logo

许可

本软件包是开源软件,许可协议为 MIT 协议