fab2s/searchable

Laravel searchable models based on FullText indexes

dev-main 2023-01-03 10:30 UTC

This package is auto-updated.

Last update: 2024-04-30 00:47:46 UTC


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.