Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

orest's avatar
Level 13

set searchable attributes to Algolia

I'm trying to figure out how I can set the searchable attributes to an index during runtime.

I have the model Thread which is searchable and based on a query parameter, I want to search only the title of the index.

class Thread extends Model 
{
	use Searchable;
}

What I have tried :

1

if (request('only_title')){
	Thread::search('query', [
                'restrictSearchableAttributes' => [
                    'title',
                ],
}

2

Thread::setSettings(
  [
    'restrictSearchableAttributes' => ['title']
  ],

3

There is also a configuration file named scout-threads where I can set the restrictSearchableAttributes value.

config('scout-threads.restrictSearchableAttributes', 'title');

Any thoughts ?

0 likes
8 replies
bobbybouwmann's avatar
Level 88

I don't think that is possible, because Scout is a package that supports multiple drivers. Not all drivers support this functionality, so you can't make it available as a generic function for Scout.

In general, if you want to have such functionality you should create multiple indexes. One for all the fields of your thread model and one for only the title. So instead of making the search column specific, you make the index specific. However, I'm not sure if Scout supports creating multiple indexes for one model though.

2 likes
bobbybouwmann's avatar

@orest This package will not search by a specific key in the index. It will just fetch all the posts that match and then loop over the items and filter it based on the given columns you gave it.

2 likes
orest's avatar
Level 13

@bobbybouwmann

Thanks for the idea. I created 2 indices for the same model ( 1 of them is aggregator actually )

In my case, i want to search all attributes for threads and replies and that's why i created an aggregator to combine both models in 1 index.

Then i created another index only for the Thread. Then from the dashboard i set the searchable attribute to be only the title

So it is possible to create 2 indices and use the same model, at least when 1 of them is aggregator.

orest's avatar
Level 13

it seems like there is no pagination for the aggregator index.

Is there a package or another solution to this ?

orest's avatar
Level 13

i just noticed that the toSearchableArray() does not apply anymore to the index which is used only to search the thread's title, but it does work for the aggregator ( which consists of the Thread and Reply models)

orest's avatar
Level 13

@bobbybouwmann

Do the where clauses apply to the index or to the records that are retrieved from the database ?

$articles = Article::search('Star Trek')->where('views', '>', 100)->get();
bobbybouwmann's avatar

Scout allows you to add simple "where" clauses to your search queries. Currently, these clauses only support basic numeric equality checks and are primarily useful for scoping search queries by a tenant ID. Since a search index is not a relational database, more advanced "where" clauses are not currently supported:

Documentation: https://laravel.com/docs/8.x/scout#where-clauses

Please or to participate in this conversation.