PHP code example of friendsofcat / opensearch-scout-driver-plus

1. Go to this page and download the library: Download friendsofcat/opensearch-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/ */

    

friendsofcat / opensearch-scout-driver-plus example snippets


use OpenSearch\ScoutDriverPlus\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 OpenSearch\ScoutDriverPlus\Searchable;

    public function searchableRouting()
    {
        return $this->user->id;
    }
}

class Book extends Model
{
    use OpenSearch\ScoutDriverPlus\Searchable;

    public function toSearchableArray()
    {
        return [
            'title' => $this->title,
            'price' => $this->price,
            'author' => $this->author->only(['name', 'phone_number']),
        ];
    }
}

class Book extends Model
{
    use OpenSearch\ScoutDriverPlus\Searchable;

    public function toSearchableArray()
    {
        return [
            'title' => $this->title,
            'price' => $this->price,
            'author' => $this->author->only(['name', 'phone_number']),
        ];
    }

    public function searchableWith()
    {
        return ['author'];
    }
}

class Book extends Model
{
    use OpenSearch\ScoutDriverPlus\Searchable;

    public function searchableConnection(): ?string
    {
        return 'books';
    }
}