naph / searchlight
Search query language for Laravel 5.4+ supporting Eloquent and Elasticsearch
Installs: 1 748
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^7.0
- elasticsearch/elasticsearch: ^5.2
- illuminate/bus: ^5.4
- illuminate/database: ^5.4
- illuminate/http: ^5.4
- illuminate/queue: ^5.4
- illuminate/support: ^5.4
Requires (Dev)
- phpunit/phpunit: ^5.6
README
An Elasticsearch search query language for Laravel featuring multi-model search
public function search(Search $search, Comment $comments, Post $posts) { return $search->in($comments, $posts) ->match('Searchlight') ->get(); }
and built-in qualifier reducers.
$driver->qualifier('/#(\w+)/', function (Search $search, $fragment) { $search->filter(['tags' => $fragment]); });
Install
Using composer
composer require naph/searchlight
Register the service provider
Naph\Searchlight\SearchlightServiceProvider;
Publish vendor files containing driver and host configuration. Lumen users should copy the file instead.
php artisan vendor:publish --tag searchlight
Setup models by implementing the contract/trait pair and overriding getSearchableFields
. The trait binds events to saved and deleted so indices are kept in sync with your database.
use Illuminate\Database\Eloquent\Model; use Naph\Searchlight\Model\SearchlightContract; use Naph\Searchlight\Model\SearchlightTrait; class Topic extends Model implements SearchlightContract { use SearchlightTrait; public function getSearchableFields(): array { return [ 'title' => 1, 'description' => 0.5, 'content' => 0.1 ]; } }
Storing this model's fully qualified classname in searchlight.repositories
config ensures their indices are built when running:
php artisan searchlight:rebuild
This command destroys all existing indexed documents in the process.
Requirements
Currently only supports PHP7 and latest versions of Laravel and Lumen.
License
Searchlight is an open-sourced software licensed under the MIT license.