teamtnt/laravel-scout-tntsearch-driver
Installation:
composer require teamtnt/laravel-scout-tntsearch-driver
Publish the config file:
php artisan vendor:publish --provider="TeamTNT\ScoutTNTSearchDriver\ScoutTNTSearchDriverServiceProvider"
Configure config/scout.php:
'driver' => 'tntsearch',
Ensure config/scout-tntsearch.php is updated with your TNTSearch index path (default: storage/tntsearch).
First Use Case: Define a model using Scout:
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
public function toSearchableArray()
{
return [
'title' => $this->title,
'body' => $this->body,
];
}
}
Index a model:
$post = Post::find(1);
$post->searchable();
Indexing:
searchable() on model instances to index them.Post::all()->each->searchable().Post::chunk(100, function ($posts) {
$posts->each->searchable();
});
Searching:
$results = Post::search('laravel')->get();
$results = Post::search('title:"Laravel" AND body:scout')->get();
$results = Post::search('query')->paginate(10);
Customizing Search Behavior:
toSearchableArray() for model-specific fields.scout:import artisan command for initial bulk indexing:
php artisan scout:import "App\Models\Post"
'driver' => env('SCOUT_DRIVER', 'tntsearch'),
scout:flush to clear the index before reindexing:
php artisan scout:flush "App\Models\Post"
php artisan scout:import "App\Models\Post"
config/scout-tntsearch.php for field mappings or analyzers:
'fields' => [
'title' => ['boost' => 2],
'body' => ['analyzer' => 'snowball'],
],
Index Path Permissions:
storage/tntsearch is writable by the web server:
chmod -R 775 storage/tntsearch
storage/logs/laravel.log for permission errors.Case Sensitivity:
lowercase) for case-insensitive searches:
'fields' => [
'title' => ['analyzer' => 'lowercase'],
],
Large Datasets:
Post::chunk(500, function ($posts) {
$posts->each->searchable();
})->onQueue('indexing');
Special Characters:
+ or -) or use TNTSearch’s syntax:
$results = Post::search('title:"Laravel + Scout"')->get();
Query Inspection:
config/scout-tntsearch.php:
'debug' => true,
storage/logs/tntsearch.log.Index Validation:
php artisan tntsearch:dump
Custom Analyzers:
config/tntsearch.php:
'analyzers' => [
'custom_analyzer' => [
'tokenizer' => 'standard',
'filter' => ['lowercase', 'stemmer'],
],
],
Model Events:
searchable events for pre/post-processing:
Post::searchable(function ($model) {
$model->body = str_replace(['&', ';'], '', $model->body);
});
Fuzzy Search:
$results = Post::search('laravel~')->get(); // ~ allows 1 typo
How can I help you explore Laravel packages today?