问题描述
我正在尝试根据条件使用 Laravel Scout 向 Algolia 添加索引.例如,我有一个 Article
模型,如果文章是 active
,我只想将这篇文章添加到 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 [];
}
这只会添加活动记录,但仍会尝试添加在 algolia 中被视为操作的空数组(我将为此付费).第二种方法是使用来自 scout 的 shouldBesearchable()
函数:
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;
}
这不适用于 php artisan scout:import "AppArticle"
.有没有人遇到过类似的问题?
This doesn't work with php artisan scout:import "AppArticle"
. Has anyone faced a similar problem?
推荐答案
这是 Laravel Scout 中的一个错误,shouldBeSearchable
尚未发布(在 master 分支上)所以你可能会遇到这样的问题一个.
It was a bug in Laravel Scout, shouldBeSearchable
is not release yet (on master branch) so you may experience some issue like this one.
不过,好消息是:这个 PR 刚刚修复了它.https://github.com/laravel/scout/pull/250
Although, good news: it was just fixed by this PR.https://github.com/laravel/scout/pull/250
这篇关于有条件地向 Laravel Scout 添加索引(Algolia)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!