Installation:
composer require iverberk/larasearch
Add the service provider to config/app.php:
'providers' => [
Iverberk\Larasearch\LarasearchServiceProvider::class,
],
Configure Elasticsearch: Publish the config file:
php artisan vendor:publish --provider="Iverberk\Larasearch\LarasearchServiceProvider"
Update config/larasearch.php with your Elasticsearch connection details (e.g., host, port, index name).
Enable a Model:
Use the Searchable trait in your Eloquent model:
use Iverberk\Larasearch\Searchable;
class Product extends Model
{
use Searchable;
protected $searchable = [
'name', // Fields to index
'description',
'price',
];
}
First Use Case: Trigger an initial index:
php artisan larasearch:index
Now search via Eloquent:
$results = Product::search('wireless headphones')->get();
Searching: Chain Elasticsearch queries onto Eloquent:
$products = Product::search('wireless')
->where('price', '<', 100)
->orderBy('price', 'desc')
->paginate(10);
Aggregations: Group results by category:
$results = Product::search('headphones')
->aggregate('category', 'terms')
->get();
Autocomplete/Suggestions:
$suggestions = Product::suggest('headpho')->get();
Highlighting:
$results = Product::search('wireless', ['highlight' => ['fields' => ['name']]])->get();
protected $searchable = ['*'] to index all non-guarded fields.$searchable:
protected $searchable = [
'name',
'category.name', // Index related field
];
searchableAs():
public function searchableAs()
{
return 'products_v2';
}
Index Naming Conflicts:
{model}_larasearch. Override with searchableAs() to avoid collisions.products_v1, products_v2).Relation Indexing Lag:
category.name) are indexed on model updates but may not reflect immediately.php artisan larasearch:reindex {Model}.Case Sensitivity:
analyzer: 'standard' in $searchable for case-insensitive searches:
protected $searchable = [
'name' => ['analyzer' => 'standard'],
];
Pagination Issues:
from/size) differs from SQL. Use paginate() instead of offset():
// ❌ Avoid
Product::search('term')->offset(10)->limit(10);
// ✅ Use
Product::search('term')->paginate(10);
Check Index Status:
php artisan larasearch:status
Log Queries:
Enable debug mode in config/larasearch.php:
'debug' => env('APP_DEBUG', false),
Queries will log to storage/logs/larasearch.log.
Reindexing: Reindex a single model or all models:
php artisan larasearch:reindex Product
php artisan larasearch:reindex --all
Custom Analyzers:
Extend Iverberk\Larasearch\Analyzers\Analyzer to add custom text processing.
Pre/Post-Index Hooks:
Override searchableBeforeIndex() or searchableAfterIndex() in your model:
public function searchableBeforeIndex(array &$data)
{
$data['slug'] = Str::slug($data['name']);
}
Bulk Operations:
Use searchableBulkIndex() for large datasets:
Product::query()->chunk(100, function ($products) {
Product::searchableBulkIndex($products);
});
should for Fuzzy Search:
Product::search('wireles', ['should' => ['fuzziness' => 2]])->get();
remember():
$results = Product::search('term')->remember(60)->get();
How can I help you explore Laravel packages today?