Search code examples
laravellaravel-5algolialaravel-scout

Adding Index to Laravel Scout Conditionally (Algolia)


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?


Solution

  • 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