Searchable indexing with MySQL full text for Laravel
provision/searchable is a Laravel package for searchable indexing with mysql full text for laravel.
It currently has 22 GitHub stars and 8.425 downloads on Packagist (latest version 1.0.9).
Install it with composer require provision/searchable.
Discover more Laravel packages by provision
or browse all Laravel packages to compare alternatives.
Last updated
This package creates a MySQL fulltext index for models and enables you to search through those.
In config/database.php set
'mysql' => [
...
'strict' => false,
...
]
composer require provision/searchable.php artisan vendor:publish --tag=searchablephp artisan migrateThe package uses a model observer to update the index when models change. If you want to run a full index you can use the console commands.
Add the SearchableTrait trait to the model you want to have indexed and define the columns you'd like to index as title and content.
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',
];
}
}
You can use a dot notation to query relationships for the model, like contacts.value.
On related model for indexing use SearchableRelationTrait and method getSearchableRelationName to return relation name.
Listen for changes on relation and update parent model
class Contact extends Model
{
use \ProVision\Searchable\Traits\SearchableRelationTrait;
/**
* @return MorphTo
*/
public function contactable()
{
return $this->morphTo();
}
/**
* @inheritDoc
*/
static function getSearchableRelationName(): string
{
return 'contactable';
}
}
You can search using the search method.
$clientsCollection = Clients::search('John Doe')->paginate();
use ProVision\Searchable\SearchableModes;
---
$clientsCollection = Clients::search('John Doe', SearchableModes::Boolean)->paginate();
Available modes
NaturalLanguage - IN NATURAL LANGUAGE MODENaturalLanguageWithQueryExpression - IN NATURAL LANGUAGE MODE WITH QUERY EXPANSIONBoolean - IN BOOLEAN MODEQueryExpression - WITH QUERY EXPANSIONMySQL fulltext search documentation: https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html
$clientsCollection = Clients::search('John Doe')->where('active', 1)->with(['contacts'])->paginate();
$clientsCollection = Clients::search('John Doe')->searchableOrder('asc')->paginate();
Available options:
ASCDESCIndex all models for a certain class
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"
php artisan searchable:index "\App\Models\Client" 1
UnIndex all models for a certain class
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"
php artisan searchable:unindex "\App\Models\Client" 1
db_connectionChoose the database connection to use, defaults to the default database connection. When you are NOT using the default database connection, this MUST be set before running the migration to work correctly.
table_nameTable name of index
command_prefixPrefix of commands
weight.title, weight.contentResults on title or content are weighted in the results. Search result score is multiplied by the weight in this config
cleanersClean searching keywords for prevent breaking the MySQL query.
$ composer test