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';
    }
}

// Basic queries
User::elasticWhere('name', 'John')
    ->elasticOrWhere('email', '[email protected]')
    ->elasticGet();

// Wildcard searches
User::elasticWhereLike('name', 'jo')
    ->elasticWhereStartWith('email', 'john')
    ->elasticGet();

// Range queries
User::elasticRange('age', 18, 30)->elasticGet();

// Term queries
User::elasticTermMust('status', 'active')
    ->elasticTermMustNot('role', 'guest')
    ->elasticGet();

// Match queries
User::elasticWhereMatch('description', 'search text')
    ->elasticWhereMatchPhrase('title', 'exact phrase')
    ->elasticGet();

// Nested queries
User::elasticNested('orders', function($query) {
    $query->elasticWhere('status', 'completed');
})->elasticGet();

// Sorting
User::elasticSortBy('created_at', 'desc')->elasticGet();

// Pagination
User::elasticWhere('status', 'active')->elasticPaginate(15);

'enable_elastic_for_tenants' => true,
'tenants_indexable' => [
    'user' => new App\Models\User
],
'tenant_model' => 'App\Models\Tenant',
'tenancy_facade' => 'Stancl\Tenancy\Facades\Tenancy',
bash
php artisan vendor:publish --provider="Fareselshinawy\ElasticSearch\Providers\ElasticSearchServiceProvider"
bash
php artisan elastic:index-create        # Create index
php artisan elastic:index-delete        # Delete index
php artisan elastic:index-sync          # Sync data
php artisan elastic:index-update        # Update index
bash
php artisan elastic:tenants-index-create  # Create tenant index
php artisan elastic:tenants-index-delete  # Delete tenant index
php artisan elastic:tenants-index-sync    # Sync tenant data
php artisan elastic:tenants-index-update  # Update tenant index
bash
php artisan elastic:tenants-index-sync --tenantReference=tenant1