clydescobidal/larasearch
最新稳定版本:1.0.6
Composer 安装命令:
composer require clydescobidal/larasearch
包简介
A Laravel package that provides FULLTEXT index search functionality
README 文档
README
Larasearch
The goal of this Laravel package is to offer fast FULLTEXT indexed searches. This is only relevant if you wish to include a basic search feature in your project. However, if your project has a large amount of data that needs to be searched and is frequently used, search engines like Typesense, ElasticSearch, Algolia, and similar ones are more appropriate.
Search queries are executed on the searchable table to save your main table from the heavy search workload.
Features
- FULLTEXT index search
- Cached results
Installation
You can install the package via composer:
composer require clydescobidal/larasearch
Publish the configuration file:
php artisan vendor:publish --provider="Clydescobidal\Larasearch\LarasearchServiceProvider"
Run migration:
php artisan migrate
Usage
Add the Clydescobidal\Larasearch\Searchable trait to the model you would like to make searchable. Models that are using this trait will be indexed in the searchable table whenever changes are made on the model.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Clydescobidal\Larasearch\Searchable; class Post extends Model { use Searchable; }
Search for posts and chain query builder methods as normal:
<?php $post= Post::search('my post title')->get(); $posts = Post::search('search posts')->paginate();
Make model searchable or unsearchable:
<?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
Commands
You can run the command below if you want to make all your models searchable. Note that this will only work on models with Clydescobidal\Larasearch\Searchable trait. This is applicable when you first install the package and you want your existing models to be searchable, or when you want to do a batch reindex of a model.
In this example, we will make all instances of App\Models\Post searchable.
php artisan make:searchable "App\Models\Post"
We can also do a batch unsearchable on a model.
php artisan make:unsearchable "App\Models\Post"
Config
| Property | Type | Default | Description |
|---|---|---|---|
| table | string | searchable | The table where the searchable indices are stored |
| cache | boolean | true | Enable caching of results |
| queue | boolean | true | Run searchable syncs in queue (recommended) |
Cache
By default, search query results are cached. You can turn this off by setting the cache property in the configuration. In any case you want to clear the cached results, you can run the artisan command:
php artisan cache:clear --tags="App\Models\Post"
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 246
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-28
