nqxcode/laravel-lucene-search
Laravel package that adds Lucene-powered full-text search to your app. Index Eloquent models and query them with fast, relevant results, plus helpers for managing indexes and integrating search into common Laravel workflows.
Installation
composer require nqxcode/laravel-lucene-search
php artisan vendor:publish --provider="Nqxcode\LuceneSearch\LuceneServiceProvider"
config/lucene-search.php file with your Lucene index path and other settings.Basic Setup
searchable() method in your Eloquent model to specify searchable fields:
use Nqxcode\LuceneSearch\Searchable;
class Post extends Model implements Searchable
{
public function searchable()
{
return [
'title' => 5, // Weight (higher = more important)
'body' => 3,
];
}
}
First Search Query
search() method on the model:
$results = Post::search('laravel')->get();
$results = Post::search('laravel')->paginate(10);
Basic Search
$posts = Post::search('backend development')->get();
Field-Specific Search
$posts = Post::search('laravel', ['title'])->get();
Boolean Queries
AND, OR, or NOT:
$posts = Post::search('laravel AND framework')->get();
$posts = Post::search('laravel NOT tutorial')->get();
Fuzzy Search
$posts = Post::search('laravel', [], ['fuzzy' => true])->get();
Pagination & Sorting
$posts = Post::search('laravel')->paginate(15);
$posts = Post::search('laravel')->orderBy('created_at', 'desc')->get();
Integration with Eloquent Scopes
class PostScope
{
public function scopePublished($query)
{
return $query->where('published', true);
}
}
Then use it with search:
$posts = Post::published()->search('laravel')->get();
Real-Time Indexing
$post = Post::find(1);
$post->index(); // Add to Lucene index
creating, updating, deleting) to auto-index:
protected static function boot()
{
parent::boot();
static::created(function ($model) {
$model->index();
});
static::updated(function ($model) {
$model->index();
});
static::deleted(function ($model) {
$model->unindex();
});
}
Advanced Query Options
$posts = Post::search('title:"Laravel" AND body:tutorial')->get();
$posts = Post::search('laravel^3 framework')->get();
Index Path Permissions
chmod -R 775 storage/lucene-index
Case Sensitivity
lowercase in config or normalize fields in searchable():
public function searchable()
{
return [
'title' => 5,
'body' => ['field' => 'lowercase(body)'], // Normalize case
];
}
Performance with Large Datasets
Post::chunk(100, function ($posts) {
foreach ($posts as $post) {
$post->index();
}
});
Field Weighting
title: 5) amplify relevance but may skew results. Test with real data.Special Characters in Queries
+, -, &&) in user input:
$query = addcslashes($userInput, '+-!()[]{}:~*"&\|');
$posts = Post::search($query)->get();
Lucene Version Compatibility
php-zendframework2-lucene or similar).Debugging Queries
'debug' => true,
Concurrent Indexing Issues
if (!$model->isIndexed()) {
$model->index();
}
Custom Analyzers
public function searchable()
{
return [
'body' => [
'field' => 'body',
'analyzer' => 'custom_analyzer',
],
];
}
config/lucene-search.php.Post-Processing Results
Nqxcode\LuceneSearch\SearchableTrait to add custom logic after retrieval:
public function getResults()
{
$results = parent::getResults();
// Add custom filtering or transformations
return $results->filter(fn ($item) => $item->isVisible());
}
Multi-Model Search
$posts = Post::search($query)->get();
$users = User::search($query)->get();
$results = $posts->merge($users);
Synonyms & Thesaurus
// In config/lucene-search.php
'synonyms' => [
'php' => ['laravel', 'framework'],
],
Integration with API Resources
return new PostResourceCollection(Post::search($request->query('q'))->paginate());
How can I help you explore Laravel packages today?