#database #development #laravel #php

When you use Laravel Scout for full-text search, you can change the way it finds the results by using a proper matching strategy. In this post, I'll show you how to do that.

I'm assuming you are using Meilisearch in as your full-text search engine. When you use a different engine, this trick will not work.

The matching strategies

Meilisearch has two matching strategies:

  • last (the default): returns documents containing all the query terms first. If there are not enough results containing all query terms to meet the requested limit, Meilisearch will remove one query term at a time, starting from the end of the query.

    When you for example search for "big fat liar", Meilisearch will first return documents that contain all three words. If the results don't meet the requested limit, it will also return documents containing only the first two terms, big fat, followed by documents containing only big.

  • all: only returns documents that contain all query terms. Meilisearch will not match any more documents even if there aren't enough to meet the requested limit.

    When you for example search for "big fat liar", it would only return documents containing all three words.

Search setup

First, let's look at how the model we're using is setup. In my use-case, I'm having a Document class which contains both a searchable field name (the name of the document) and text (the actual text contents of the file).

To achieve, this, I've setup the following model:

app/Models/Document.php

 1namespace App\\Models;
 2
 3use Illuminate\Database\Eloquent\Model;
 4use Laravel\Scout\Searchable;
 5
 6final class Document extends Model
 7{
 8    use Searchable;
 9
10    public function searchableAs(): string
11    {
12        return 'documents_index';
13    }
14
15    public function toSearchableArray(): array
16    {
17        return [
18            'id' => $this->id,
19            'name' => $this->name,
20            'text' => $this->text,
21        ];
22    }
23}

In my Laravel scout configuration, I marked all of these attributes as searchable:

config/scout.php

 1return [
 2    'meilisearch' => [
 3        'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
 4        'key' => env('MEILISEARCH_KEY', null),
 5        'index-settings' => [
 6            Document::class => [
 7                'filterableAttributes'=> ['id', 'name', 'text'],
 8                'sortableAttributes' => [],
 9            ],
10        ],
11    ],
12];

After configuring the search, don't forget to sync the index settings before you start adding items to the index (as described here):

1php artisan scout:sync-index-settings

Specifying the matching strategy

I want to be able to search on both fields, it's simply doing:

1use App\\Models\\Document;
2
3Document::search($query)->get();

This will use the default matching strategy from Meilisearch.

To specify a different matching strategy, you can do the following:

 1use App\\Models\\Document;
 2use Meilisearch\Endpoints\Indexes;
 3
 4Document::search(
 5    $searchQuery,
 6    function (Indexes $searchEngine, string $query, array $options) {
 7        $options['matchingStrategy'] = 'all';
 8        return $searchEngine->search($query, $options);
 9    }
10)->get();

The function you specify in the search function allows you to customize the search options for this query. In this example, we used the option matchingStrategy to specify the attributes we want to search on.