Tuesday, November 28, 2017

Laravel 5.* - Elasticsearch setup guide

Hi,

Lets discuss about how to setup Elasticsearch in Laravel 5.* framework.

Step 1:
Add elasticsearch and elasticsearch development packages to composer jason and composer update.

"elasticsearch/elasticsearch": "~2.0",
"elasticquent/elasticquent": "dev-master",
 

Currently i'm using elastic search 1.5. So that ii have to download elasticsearch 2.0. you can download latest version.
Then register elastic in laravel. Go to config/app.php add

Elasticquent\ElasticquentServiceProvider::class, to providers 
'Es' => Elasticquent\ElasticquentElasticsearchFacade::class, to aliases 

Create elasticquent.php file in config, add followings to it. You can 
define settings in here. (Name of your elastic indexes)
Download file
 
Step 2
Then create elastic search model for create, update and reindex data 
to indexes. 
Download file
 
Step 3
For every model need to add $table, $primaryKey, $searchFields and 
constructor.
Download file


Step 4
need to create index when saving new data to database. 
Sample code:
if(config('app.ElasticsearchSync')) {    // Save to ES Index    self::where('comp_id', $company->comp_id)->first()->addToIndex();
    User::where('id', $emp_id)->first()->addToIndex($company->comp_id);}
 

Need to update index when update database.
Sample code: 
if(config('app.ElasticsearchSync')) {    // Update ES Index    self::getCompanyByCompId($company->comp_id)->updateIndex();}
 
Step 5:

Get data from elastic search index
Download File



 
 
 


Share/Bookmark

0 comments:

Post a Comment