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
```php
$this->id,
'name' => $this->name,
'text' => $this->text,
];
}
}
```
In my Laravel scout configuration, I marked all of these attributes as searchable:
_`config/scout.php`_```php
[
'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](https://laravel.com/docs/10.x/scout#configuring-filterable-data-for-meilisearch)):
```bash
php artisan scout:sync-index-settings
```
I want to be able to search on both fields, it's simply doing:
```php
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:
```php
search($query, $options);
}
)->get();
```
The function you specify in the search function allows you to [customize the search options](https://laravel.com/docs/10.x/scout#customizing-engine-searches) for this query. In this example, we used the option [`attributesToSearchOn`](https://www.meilisearch.com/docs/reference/api/search#customize-attributes-to-search-on-at-search-time) 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:
```php
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](https://www.meilisearch.com/docs/reference/api/search#search-parameters).
If this post was enjoyable or useful for you, please share it!
If you have comments, questions, or feedback, you can email my personal email.
To get new posts, subscribe use the RSS feed.