hnp / laravel-es
An easy way to use Elasticsearch in your Laravel applications.
Requires
- php: >=7.2.0
- elasticsearch/elasticsearch: ^7.0
- illuminate/config: 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0
- illuminate/container: 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0
- illuminate/filesystem: 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0
- illuminate/http: 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0
- illuminate/support: 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || ^6.0 || ^7.0 || ^8.0
This package is auto-updated.
Last update: 2024-04-05 10:30:50 UTC
README
An easy way to use Elasticsearch in your Laravel applications.
Installation and Configuration
Install via composer:
composer require hnp/laravel-es
The package's service provider will automatically register its service provider.
Publish the configuration file:
php artisan vendor:publish --provider="HNP\\LaravelES\LaravelESServiceProvider"
After you publish the configuration file as suggested above, you may configure
by adding the following to your application's .env
file (with appropriate values):
ELASTIC_HOST=localhost:9200
Preparing your model
The model must implement the following trait:
namespace App\Models; use Illuminate\Database\Eloquent\Model; use HNP\LaravelES\Traits\LaravelES; class YourModel extends Model { use LaravelES; }
You should add method getIndexDocumentData and es_search_fields property to your model.
protected $es_search_fields = ["field1", "field2"]; function getIndexDocumentData() { return array( 'id' => $this->id, 'field1' => $this->field1, 'field2' => $this->field2, 'field3' =>$this->field3 ); }
Create an index
Add es_settings and es_mappings property to your model.
protected $es_settings = []; protected $es_mappings = [];
Let's add a new index with some custom settings:
YourModel::createIndex();
Delete an index
YourModel::deleteIndex();
Index a document
$model->addToIndex();
Remove index a document
$model->removeFromIndex();
Usage
//Simple search $results = YourModel::search("keywords"); //Search with custom query $query = [ 'multi_match'=>[ 'query'=>"search keyword", 'fields'=>["field1", "field2"], 'type'=>'phrase', 'slop'=>150 ] ]; $results = YourModel::searchWithQuery($query); //Get query builder from result $query = $results->query();
License
The MIT License (MIT). Please see License File for more information.