friendsofcat/opensearch-scout-driver

OpenSearch driver for Laravel Scout

2.1.0 2023-05-19 17:09 UTC

This package is auto-updated.

Last update: 2024-11-19 20:28:46 UTC


README

OpenSearch driver for Laravel Scout.

Contents

Compatibility

The current version of OpenSearch Scout Driver has been tested with the following configuration:

  • PHP 7.4-8.2
  • OpenSearch 2.x
  • Laravel 7.x-10.x
  • Laravel Scout 7.x-10.x

Installation

The library can be installed via Composer:

composer require friendsofcat/opensearch-scout-driver

Note, that this library is just a driver for Laravel Scout, don't forget to install it beforehand:

composer require laravel/scout

After Scout has been installed, publish its configuration file using:

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

Then, change the driver option in the config/scout.php file to opensearch:

// config/scout.php

'driver' => env('SCOUT_DRIVER', 'opensearch'),

If you want to use OpenSearch Scout Driver with Lumen framework check this guide.

Configuration

OpenSearch Scout Driver uses friendsofcat/opensearch-client as a dependency. To change the client settings you need to publish the configuration file first:

php artisan vendor:publish --provider="OpenSearch\Laravel\Client\ServiceProvider"

In the newly created config/opensearch.client.php file you can define the default connection name using configuration hashes. Please, refer to the opensearch-client documentation for more details.

OpenSearch Scout Driver itself has only one configuration option at the moment - refresh_documents. If it's set to true (false by default) documents are indexed immediately, which might be handy for testing.

You can configure refresh_documents in the config/opensearch.scout_driver.php file after publishing it with the following command:

php artisan vendor:publish --provider="OpenSearch\ScoutDriver\ServiceProvider"

At last, do not forget, that with Scout you can configure the searchable data, the model id and the index name. Check the official Scout documentation for more details.

Note, that the _id field can't be part of the searchable data, so make sure the field is excluded or renamed in the toSearchableArray method in case you are using MongoDB as the database.

Basic usage

OpenSearch driver uses OpenSearch query string wrapped in a bool query under the hood. It means that you can use mini-language syntax when searching a model:

$orders = App\Order::search('title:(Star OR Trek)')->get();

When the query string is omitted, the match all query is used:

$orders = App\Order::search()->where('user_id', 1)->get();

Please refer to the official Laravel Scout documentation for more details and usage examples.

Advanced Search

In case the basic search doesn't cover your project needs check OpenSearch Scout Driver Plus, which extends standard Scout search capabilities by introducing advanced query builders. These builders give you possibility to use compound queries, custom filters and sorting, highlights and more.

Migrations

If you are looking for a way to control OpenSearch index schema programmatically check OpenSearch Migrations. OpenSearch Migrations allow you to modify application's index schema and share it across multiple environments with the same ease, that gives you Laravel database migrations.

Pitfalls

There are few things, which are slightly different from other Scout drivers:

  • As you probably know, Scout only indexes fields, which are returned by the toSearchableArray method. OpenSearch driver indexes a model even when toSearchableArray returns an empty array. You can change this behaviour by overwriting the shouldBeSearchable method of your model:
public function shouldBeSearchable()
{
    return count($this->toSearchableArray()) > 0;
}
$searchResult = App\Order::search('Star Trek')->raw();
  • To be compatible with other drivers and to not expose internal implementation of the engine, OpenSearch driver ignores callback parameter of the search method:
App\Order::search('Star Trek', function () {
    // this will not be triggered
})->get()