PHP code example of synergy / scout-elasticsearch-driver

1. Go to this page and download the library: Download synergy/scout-elasticsearch-driver 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/ */

    

synergy / scout-elasticsearch-driver example snippets




namespace App;

use SynergyScoutElastic\IndexConfigurator;

class MyIndexConfigurator extends IndexConfigurator
{
    // It's not obligatory to determine name. By default it'll be a snaked class name without `IndexConfigurator` part.
    protected $name = 'my_index';  
    
    // You can specify any settings you want, for example, analyzers. 
    protected $settings = [
        'analysis' => [
            'analyzer' => [
                'es_std' => [
                    'type' => 'standard',
                    'stopwords' => '_spanish_'
                ]
            ]    
        ]
    ];

    // Common mapping for all types.
    protected $defaultMapping = [
        '_all' => [
            'enabled' => true
        ],
        'dynamic_templates' => [
            [
                'es' => [
                    'match' => '*_es',
                    'match_mapping_type' => 'string',
                    'mapping' => [
                        'type' => 'string',
                        'analyzer' => 'es_std'
                    ]
                ]    
            ]
        ]
    ];
}



namespace App;

use SynergyScoutElastic\Models\Searchable;
use Illuminate\Database\Eloquent\Model;
use SynergyScoutElastic\Models\SearchableInterface;

class MyModel extends Model implements SearchableInterface
{
    use Searchable;

    protected $indexConfigurator = MyIndexConfigurator::class;

    protected $searchRules = [
        //
    ];

    // Here you can specify a mapping for a model fields.
    protected $mapping = [
        'properties' => [
            'text' => [
                'type' => 'string',
                'fields' => [
                    'raw' => [
                        'type' => 'string',
                        'index' => 'not_analyzed',
                    ]
                ]
            ],
        ]
    ];
}

App\MyModel::search('*')
    ->where('id', 1)
    ->get();

App\MyModel::search('Brazil')
    ->rule(App\MySearchRule::class)
    ->get();

App\MyModel::search('*')
    ->whereRegexp('name.raw', 'A.+')
    ->where('age', '>=', 30)
    ->whereExists('unemployed')
    ->get();

App\MyModel::searchRaw([
    'query' => [
        'bool' => [
            'must' => [
                'match' => [
                    '_all' => 'Brazil'
                ]
            ]
        ]
    ]
]);



namespace App;

use SynergyScoutElastic\SearchRule;

class MySearch extends SearchRule
{
    // This method returns an array that represents a content of bool query.
    public function buildQueryPayload()
    {
        return [
            'must' => [
                'match' => [
                    'name' => $this->builder->query
                ]
            ]
        ];
    }
}

return [
   'must' => [
       'match' => [
           '_all' => $this->builder->query
       ]
   ]
];



namespace App;

use SynergyScoutElastic\Models\Searchable;
use Illuminate\Database\Eloquent\Model;
use SynergyScoutElastic\Models\SearchableInterface;

class MyModel extends Model implements SearchableInterface
{
    use Searchable;
    
    // You can set several rules for one model. In this case, the first not empty result will be returned.
    protected $searchRules = [
        MySearchRule::class
    ];
}

// You can set either a SearchRule class
App\MyModel::search('Brazil')
    ->rule(App\MySearchRule::class)
    ->get();
    
// or a callable
App\MyModel::search('Brazil')
    ->rule(function($builder) {
        return [
            'must' => [
                'match' => [
                    'Country' => $builder->query
                ]
            ]
        ];
    })
    ->get();

    App\MyModel::search('Brazil')
        ->first()
        ->explain();
    

    App\MyModel::search('Brazil')
        ->first()
        ->profile();
    

App\MyModel::search('Brazil')
    ->buildPayload();

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
php artisan vendor:publish --provider="SynergyScoutElastic\providers\SynergyScoutElasticServiceProvider"

php artisan make:index-configurator MyIndexConfigurator

php artisan elastic:create-index App\\MyIndexConfigurator

php artisan make:searchable-model MyModel --index-configurator=MyIndexConfigurator

php artisan elastic:update-mapping App\\MyModel