配置/可搜索

使用MySQL全文索引的Laravel可搜索索引

1.0.9 2021-04-20 13:23 UTC

README

Build Status

Laravel MySQL全文搜索

此包为模型创建MySQL全文索引,并允许您在这些索引中进行搜索。

要求

  • Laravel >= 5.7
  • MySQL >= 5.6 / MariaDB >= 10.0.15

重要!

config/database.php 中设置

'mysql' => [
...
    'strict' => false,
...
]

安装

  1. 使用composer安装 composer require provision/searchable
  2. 发布迁移和配置 php artisan vendor:publish --tag=searchable
  3. 迁移数据库 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();

可用选项

  • ASC
  • DESC

命令

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

在结果中对 titlecontent 进行加权。搜索结果得分乘以此配置中的权重

cleaners

清理搜索关键字以防止破坏MySQL查询。

测试

$ composer test