PHP code example of ashkan-jafarzadeh / elastic-scout-driver-plus

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

    

ashkan-jafarzadeh / elastic-scout-driver-plus example snippets


use ElasticScoutDriverPlus\Support\Query;

// using a query builder
$query = Query::match()
    ->field('title')
    ->query('My book')
    ->fuzziness('AUTO');

// using a raw query
$query = [
    'match' => [
        'title' => [
            'query' => 'My book',
            'fuzziness' => 'AUTO'
        ] 
    ]
];

$builder = Book::searchQuery($query);

$builder = Book::searchQuery($query)
    ->size(2)
    ->sort('price', 'asc');

$searchResult = Book::searchQuery($query)->execute();

$hits = $searchResult->hits();
$models = $searchResult->models();
$documents = $searchResult->documents();
$highlights = $searchResult->highlights();

class Book extends Model
{
    use ElasticScoutDriverPlus\Searchable;
    
    public function shardRouting()
    {
        return $this->user->id;
    }
}

class Book extends Model
{
    use ElasticScoutDriverPlus\Searchable;
    
    public function toSearchableArray()
    {
        return [
            'title' => $this->title,
            'price' => $this->price,
            'author' => $this->author->only(['name', 'phone_number']),
        ];
    }
}

class Book extends Model
{
    use ElasticScoutDriverPlus\Searchable;
    
    public function toSearchableArray()
    {
        return [
            'title' => $this->title,
            'price' => $this->price,
            'author' => $this->author->only(['name', 'phone_number']),
        ];
    }
    
    public function searchableWith()
    {
        return ['author'];
    }
}