fab2s/searchable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fab2s/searchable

Composer 安装命令:

composer require fab2s/searchable

包简介

Laravel searchable models based on FullText indexes

README 文档

README

Searchable models for Laravel (The Awesome) based on Mysql FullText indexes.

This package does not try to be smart, just KISS. It will allow you to make any filed of your model searchable by concatenating them into a new column indexed with a mysql FullText index.

It requires mysql / mariadb and Laravel 9.x

Installation

Searchable can be installed using composer:

composer require "fab2s/searchable"

Usage

To start using Searchable on a specific model, just use the Searchable trait and setup $searchables:

class MyModel extends Model
{
    use Searchable;
    
     /**
     * @var string[]
     */
    protected $searchables = [
        'field1',
        'field2',
        // ...
        'fieldN',
    ];
}

By default, field1 to fieldN will be concatenated and stored into the default SearchQuery::SEARCHABLE_FIELD field added to the model by the Enable command.

By default, this searchable field will be of type VARCHAR(255), but you can customize it at will with any type and length supporting a FullText index by just overriding the Searchable trait method in your model:

    /**
     * @return string
     */
    public function getSearchableField(): string
    {
        return SearchQuery::SEARCHABLE_FIELD; // searchable
    }

    /**
     * @return string any migration method such as string, text etc ...
     */
    public function getSearchableFieldDbType(): string
    {
        return 'string';
    }

    /**
     * @return int
     */
    public function getSearchableFieldDbSize(): int
    {
        return 255;
    }

You can customise concatenation as well overriding:

    /**
     * @param string $additional for case where this method is overridden in users
     *
     * @return string
     */
    public function getSearchableContent(string $additional = ''): string
    {
        return TermParser::prepareSearchable(array_map(function ($field) {
            return $this->$field;
        }, $this->getSearchables()), $additional);
    }

The $additional parameter can be used to preprocess model data if needed, can be handy to encrypt/decrypt or anonymize for example:

    /**
     * @return string
     */
    public function getSearchableContent(): string
    {
        $additional = [
            $this->decrypt('additional_field1'),
            0 . substr((string) $this->decrypt('phone'), 3, 6), // will allow for partial matches
        ];

        return $this->getSearchableContentTrait(implode(' ', $additional));
    }

Once you have configured your model(s), you can use the Enable command to add the searchable field to your models and / or index them:

$ php artisan searchable:enable --help
Description:
  Enable searchable for your models

Usage:
  searchable:enable [options]

Options:
      --root[=ROOT]     The place where to start looking for models, defaults to Laravel's app/Models
      --index           To also index / re index
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Stopwords

Searchable comes with an english and french stop words files which you can use to reduce FullText indexing by ignoring words listed in these files.

The StopWords command can be used to populate a stopwords table with these words:

php artisan searchable:stopwords

The db server configuration must be configured as demonstrated in innodb_full_text.cnf for these words to effectively be excluded from indexing.

Contributing

Contributions are welcome, do not hesitate to open issues and submit pull requests.

License

Searchable is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 51.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-05