clydescobidal/larasearch

一个提供全文索引搜索功能的 Laravel 扩展包

1.0.6 2023-12-07 14:57 UTC

This package is auto-updated.

Last update: 2024-09-07 17:09:20 UTC


README

Larasearch

Latest Stable Version Total Downloads License GitHub Actions

此 Laravel 扩展包的目标是提供快速的全文索引搜索。这仅在您希望在项目中包含基本搜索功能时才有意义。然而,如果您的项目包含大量需要搜索且经常使用的数据,则像 Typesense、ElasticSearch、Algolia 等搜索引擎更为合适。

搜索查询在可搜索表中执行,以节省主表从繁重的搜索工作负载中解脱出来。

特性

  • 全文索引搜索
  • 缓存结果

安装

您可以通过 composer 安装此包

composer require clydescobidal/larasearch

发布配置文件

php artisan vendor:publish --provider="Clydescobidal\Larasearch\LarasearchServiceProvider"

运行迁移

php artisan migrate

用法

Clydescobidal\Larasearch\Searchable 特性添加到您希望可搜索的模型中。使用此特性的模型在模型发生变化时将自动在可搜索表中索引。

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use Clydescobidal\Larasearch\Searchable;
 
class Post extends Model
{
    use Searchable;
}

按常规搜索帖子并链式调用查询构建器方法

<?php

$post= Post::search('my post title')->get();
$posts = Post::search('search posts')->paginate();

使模型可搜索或不可搜索

<?php

$post= Post::find(1);
$post->searchable(); // Adds this model to the search index

$post= Post::find(2);
$post->unsearchable(); // Removes this model from the search index

命令

如果您想使所有模型可搜索,可以运行以下命令。请注意,这仅适用于具有 Clydescobidal\Larasearch\Searchable 特性的模型。这适用于您首次安装包时希望现有模型可搜索,或者当您想要批量重新索引模型时。

在这个例子中,我们将使所有 App\Models\Post 实例可搜索。

php artisan make:searchable "App\Models\Post"

我们还可以对模型进行批量不可搜索操作。

php artisan make:unsearchable "App\Models\Post"

配置

缓存

默认情况下,搜索查询结果会被缓存。您可以通过在配置中设置 cache 属性来关闭此功能。在任何您想要清除缓存结果的情况下,您都可以运行 artisan 命令

php artisan cache:clear --tags="App\Models\Post"

贡献

请参阅 CONTRIBUTING 获取详细信息。

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件