PHP code example of fareselshinawy / elastic-search
1. Go to this page and download the library: Download fareselshinawy/elastic-search library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fareselshinawy / elastic-search example snippets
use Fareselshinawy\ElasticSearch\Traits\ElasticSearchable;
class User extends Model
{
use ElasticSearchable;
// Required: Define the data to be synchronized with Elasticsearch
public function getElasticSearchSyncAbleData()
{
return [
"name" => Str::lower($this->name),
"email" => $this->email,
"id" => $this->id
];
}
// Optional: Define custom index mappings
public function getIndexableProperties()
{
return [
"name" => ['type' => 'keyword'],
"email" => ['type' => 'keyword'],
"id" => ['type' => 'integer']
];
}
// Optional: Define relations to sync as example department:id,name..etc
public $elasticSyncAbleRelations = [];
// Optional: Define dependent indexes that should be updated ( relation => class ) users => User::class
public $dependentIndexesRelations = [];
// Optional: Custom index name
public function getElasticIndexKey()
{
return $this->indexPrefix() . 'users';
}
}