- Notifications
You must be signed in to change notification settings - Fork6
Elasticsearch driver for Laravel Scout
License
thomasjsn/laravel-scout-elastic
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This package provides anElasticsearch driver for Laravel Scout.
You can install the package via composer:
composer require thomasjsn/laravel-scout-elastic
You must add the Scout service provider and the package service provider in your app.php config:
// config/app.php'providers' => [ ...Laravel\Scout\ScoutServiceProvider::class, ...ScoutEngines\Elasticsearch\ElasticsearchProvider::class,],
Then you should publish the Elasticsearch configuration using thevendor:publish Artisan command.
php artisan vendor:publish --provider="ScoutEngines\Elasticsearch\ElasticsearchProvider"You must have a Elasticsearch server up and running, indices can be created with an Artisan command; see below.
After you've published the Laravel Scout package configuration:
// config/scout.php// Set your driver to elasticsearch'driver' =>env('SCOUT_DRIVER','elasticsearch'),
You may define custom mappings for Elasticsearch fields in the config. See examples in theconfig file.If you prefer storing mappings in models, you may create a static public methodmapping() in each particular model:
class Articleextends Model{// ...publicstaticfunctionmapping() {return ['location' => ['type' =>'geo_point' ], ]; }// ...}
And then use it in the config file:
'indices' => ['realestate' => ['settings' => ["number_of_shards" =>1,"number_of_replicas" =>0, ],'mappings' => ['articles' => \App\Article::mapping(), ], ], ]
The document type, in this examplearticles must matchsearchableAs() for the respective model.
Elasticsearch can set default types to model fields on the first insert if you do not explicitly define them.However; sometimes the defaults are not what you're looking for, or you need to define additional mapping properties.
In that case, we strongly recommend creating indices with proper mappings before inserting any data.For that purpose, there is an Artisan command, calledelastic:make-indices {index} which creates an index based onthe settings in your configuration file.
To create all indices from your config just ignore the{index} parameter and run:
php artisan elastic:make-indicesIf the index exists you will be asked if you want to delete and recreate it, or you can use the--force flag.
To get information about your existing Elasticsearch indices you may want to use the following command:
php artisan elastic:indicesYou may follow instructions from theofficial Laravel Scout documentationto index your data.
The package supports everything that is provided by Laravel Scout.
The Scoutsearch method used the default query method defined in the config file.
Sorting withorderBy() method:
$articles = Article::search($keywords) ->orderBy('id','desc') ->get();
However, to use the extra Elasticsearch features included in this package, use traitElasticSearchableby adding it to your model instead ofSearchable:
class Articleextends Model{// use Searchable;use ElasticSearchable;// ...}
The package features:
- The
elasticSearchmethod,elasticSearch($method, $query, array $params = null):
$articles = Article::elasticSearch('multi_match',$q, ['fields' => ['title','content','tags'],'fuzziness' =>'auto','prefix_length' =>2,'operator' =>'AND'])->get();
Parameters are taken from the configuration, for the specific query method, if not supplied. But you may override them.
- A separate Elasticsearch index for each model.
If you have defined several indices in yourconfig file,you may choose which index a model belongs to by overridingsearchableWithin() method in your model:
publicfunctionsearchableWithin(){return'foobar';}
If you do not overridesearchableWithin() in your model, the first index from the config will be used.
The MIT License (MIT).
About
Elasticsearch driver for Laravel Scout
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Languages
- PHP100.0%