shokme / laravel-scout-meilisearch
Laravel Scout custom engine for MeiliSearch
Requires
- php: ^7.4
- laravel/scout: ^8.0
- meilisearch/meilisearch-php: ^0.9.0
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/testbench: ^5.0
- phpunit/phpunit: ^9.1
This package is auto-updated.
Last update: 2020-04-18 18:18:54 UTC
README
The Laravel scout package for MeiliSearch.
MeiliSearch provides an ultra relevant and instant full-text search. Our solution is open-source and you can check out our repository here.
Here is the MeiliSearch documentation 📖
Table of Contents
Installation
Composer
$ composer require meilisearch/meilisearch-laravel-scout
Export configuration
$ php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider" $ php artisan vendor:publish --provider="Meilisearch\Scout\MeilisearchServiceProvider" --tag="config"
Update .env
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://127.0.0.1:7700
MEILISEARCH_KEY=masterKey
Run MeiliSearch
There are many easy ways to download and run a MeiliSearch instance.
For example, if you use Docker:
$ docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey
NB: you can also download MeiliSearch from Homebrew or APT.
Getting started
Indexes
Create an index
// Create an index $ php artisan scout:index books // Create an index and give the primary-key $ php artisan scout:index books --key book_id
Add document
<?php use Laravel\Scout\Searchable; class Book extends Model { use Searchable; }
<?php class BookController extends Controller { public function store() { $book = new Book(); $book->title = 'Pride and Prejudice'; ... $book->save(); } }
You can also import all your table to meilisearch by using the artisan command:
$ php artisan scout:import "App\Book"
Search in index
class BookController extends Controller { public function search() { // MeiliSearch is typo-tolerant: Book::search('harry pottre')->get(); // Or if you want to get the result from meilisearch: Book::search('harry pottre')->raw(); } }
Delete documents
class BookController extends Controller { public function destroy($id) { // Delete one document Book::find($id)->delete(); // Delete several documents Book::destroy([1, 42]); // Delete all documents /!\ Book::query()->delete(); } }
or you can use the artisan command to delete all documents from an index:
$ php artisan scout:flush "App\Book"
Delete an index
$ php artisan scout:index -d books
Search
Custom search
All the supported options are described in this documentation section.
class BookController extends Controller { public function customSearch() { Book::search('prince', function (Index $meilisearch, $query, $options) { $options['filters'] = 'author="Antoine de Saint-Exupéry"'; return $meilisearch->search($query, $options); })->limit(3)->get(); } }
Pagination
class BookController extends Controller { public function search() { Book::search('mustang')->paginate(); // with a limit of items per page: Book::search('mustang')->paginate(5); // using meilisearch response: Book::search('mustang')->paginateRaw(); } }
Compatibility with MeiliSearch
This package works for MeiliSearch >=v0.10
.
Additional notes
You can use more advance function by reading the documentation of MeiliSearch PHP Client
This package is a custom engine of Laravel Scout