Adding Index to Laravel Scout Conditionally (Algol

2019-07-31 18:58发布

I'm trying to add index to Algolia using Laravel Scout based on a condition. For example I have a Article model and I only want to add this article to Algolia if the article is active. My first approach was this:

public function toSearchableArray()
{
   if($this->active) return $record;
   return [];

}

this only adds the active records but still attempts to add empty arrays which is considered as Operation in algolia ( I will be charged for it). The second approach was to use shouldBesearchable() function from scout:

public function shouldBeSearchable()
{
    if($this->active) return true;
    return false;

}

This doesn't work with php artisan scout:import "App\Article". Has anyone faced a similar problem?

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-31 19:45

It was a bug in Laravel Scout, shouldBeSearchable is not release yet (on master branch) so you may experience some issue like this one.

Although, good news: it was just fixed by this PR. https://github.com/laravel/scout/pull/250

查看更多
登录 后发表回答