naph/searchlight

Search query language for Laravel 5.4+ supporting Eloquent and Elasticsearch

Installs: 1 730

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 0

Open Issues: 0

Type:project

0.6.0 2017-08-24 01:30 UTC

This package is auto-updated.

Last update: 2024-10-29 05:04:53 UTC


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.