配置 / 可搜索
使用MySQL全文索引的Laravel可搜索索引
Requires
- php: >=7.1.3
- illuminate/database: 5.7.*|5.8.*|^6.0|^7.0|^8.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ~6.0|^7.0|^8.0|^9.0
This package is auto-updated.
Last update: 2024-09-18 08:08:44 UTC
README
Laravel MySQL全文搜索
此包为模型创建MySQL全文索引,并允许您在这些索引中进行搜索。
要求
- Laravel >= 5.7
- MySQL >= 5.6 / MariaDB >= 10.0.15
重要!
在 config/database.php 中设置
'mysql' => [ ... 'strict' => false, ... ]
安装
- 使用composer安装
composer require provision/searchable。 - 发布迁移和配置
php artisan vendor:publish --tag=searchable - 迁移数据库
php artisan migrate
用法
该包使用模型观察器在模型更改时更新索引。如果需要运行完整索引,可以使用控制台命令。
模型
将 SearchableTrait 特性添加到您想索引的模型中,并定义您想要索引为标题和内容的列。
示例
class Clients extends Model
{
use \ProVision\Searchable\Traits\SearchableTrait;
/**
* @inheritDoc
*/
protected function getSearchableTitleColumns(): array
{
return [
'name'
];
}
/**
* @inheritDoc
*/
protected function getSearchableContentColumns(): array
{
return [
'description',
'address',
'vat_number',
'contacts.value',
'contactPersons.first_name',
'contactPersons.last_name',
'contactPersons.contacts.value',
];
}
}
您可以使用点表示法查询模型的关联,例如 contacts.value。
关联模型索引
在关联模型索引时使用 SearchableRelationTrait 和方法 getSearchableRelationName 返回关系名称。
监听关联的变化并更新父模型
示例
class Contact extends Model
{
use \ProVision\Searchable\Traits\SearchableRelationTrait;
/**
* @return MorphTo
*/
public function contactable()
{
return $this->morphTo();
}
/**
* @inheritDoc
*/
static function getSearchableRelationName(): string
{
return 'contactable';
}
}
搜索
您可以使用 search 方法进行搜索。
$clientsCollection = Clients::search('John Doe')->paginate();
使用特定的全文搜索模式进行搜索
use ProVision\Searchable\SearchableModes;
---
$clientsCollection = Clients::search('John Doe', SearchableModes::Boolean)->paginate();
可用模式
NaturalLanguage- 在自然语言模式下NaturalLanguageWithQueryExpression- 在自然语言模式下带有查询扩展Boolean- 在布尔模式下QueryExpression- 带有查询扩展
MySQL全文搜索文档:https://dev.mysqlserver.cn/doc/refman/8.0/en/fulltext-search.html
使用关联和额外的WHERE子句进行搜索
$clientsCollection = Clients::search('John Doe')->where('active', 1)->with(['contacts'])->paginate();
按可搜索得分排序
$clientsCollection = Clients::search('John Doe')->searchableOrder('asc')->paginate();
可用选项
ASCDESC
命令
searchable:index
为特定类索引所有模型
php artisan searchable:index
Usage:
searchable:index <model_class> {id?}
Arguments:
model_class Classname of the model to index
id Model id to index (optional)
示例
- 索引所有客户
php artisan searchable:index "\App\Models\Client"
- 按ID索引特定客户
php artisan searchable:index "\App\Models\Client" 1
searchable:unindex
取消索引特定类的所有模型
php artisan searchable:unindex
Usage:
searchable:unindex <model_class> {id?}
Arguments:
model_class Classname of the model to index
id Model id to unindex (optional)
示例
- 取消索引所有客户
php artisan searchable:unindex "\App\Models\Client"
- 按ID取消索引特定客户
php artisan searchable:unindex "\App\Models\Client" 1
配置选项
db_connection
选择要使用的数据库连接,默认为默认数据库连接。当您不使用默认数据库连接时,在运行迁移之前必须设置此选项才能正确工作。
table_name
索引表的名称
command_prefix
命令前缀
weight.title, weight.content
在结果中对 title 或 content 进行加权。搜索结果得分乘以此配置中的权重
cleaners
清理搜索关键字以防止破坏MySQL查询。
测试
$ composer test