When you use Laravel Scout for full-text search, you can search on all attributes of a model. But what if you want to search on specific attributes? 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 probably not work.

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

namespace App\\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

final class Document extends Model
{
    use Searchable;

    public function searchableAs(): string
    {
        return 'documents_index';
    }

    public function toSearchableArray(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'text' => $this->text,
        ];
    }
}

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

config/scout.php

return [
    'meilisearch' => [
        'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
        'key' => env('MEILISEARCH_KEY', null),
        'index-settings' => [
            Document::class => [
                'filterableAttributes'=> ['id', 'name', 'text'],
                'sortableAttributes' => [],
            ],
        ],
    ],
];

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

php artisan scout:sync-index-settings

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

use App\\Models\\Document;

Document::search($query)->get();

This will search on all attributes of the model. But what if I want to search on only the name attribute? You can do this by specifying the attributes you want to search on:

use App\\Models\\Document;
use Meilisearch\Endpoints\Indexes;

Document::search(
    $searchQuery,
    function (Indexes $searchEngine, string $query, array $options) {
        $options['attributesToSearchOn'] = ['name'];
        return $searchEngine->search($query, $options);
    }
)->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 attributesToSearchOn to specify the attributes we want to search on. This will cause the search to only search on the name attribute.

You can also use this to search on multiple attributes:

use App\\Models\\Document;
use Meilisearch\Endpoints\Indexes;

Document::search(
    $searchQuery,
    function (Indexes $searchEngine, string $query, array $options) {
        $options['attributesToSearchOn'] = ['name', 'text'];
        return $searchEngine->search($query, $options);
    }
)->get();

Be careful though, attributes passed to attributesToSearchOn must also be present in the searchableAttributes list.

There are many more options you can pass on to a search query in Meilisearch. The full list can be found here.