我希望能够根据 Meilisearch 和 Laravel scout 无法搜索的列过滤结果。
想象一个“评论”表,其中包含以下可搜索列:
public function toSearchableArray() {
$array = Arr::only(
$this->toArray(),
['id','title', 'link', 'raw_text', 'subchan', 'nsfw']
);
return $array;
}
但只能在特定日期后获得结果:
Comment::search($query, ['filters' => 'created_at > 795484800'])
为此,我需要添加 created_at
侦察兵的 toSearchableArray。这样做的问题是,当用户搜索时,created_at
的结果也会被查询。
答案 0 :(得分:0)
如果我理解正确,您希望能够根据 created_at
列进行过滤,但它不应该是可搜索的,即输入“795”作为查询不应返回“795”的所有结果" 是时间戳的一部分?
我认为目前 Scout 不会让您以简单的方式实现这一目标,但它应该仍然是可能的。
第 1 步是将 created_at
列添加到 toSearchableArray()
方法。这将确保数据被 Meili 索引。
第 2 步是更改可搜索模型的索引配置,以便从 searchable attributes 列表中排除 created_at
。这是伪代码并且没有记录,但它应该看起来像这样:
$dummy = new Comment();
// Should resolve to an engine: https://github.com/laravel/scout/blob/f8aa3c3182fe97f56a6436fd0b28fcacfcbabc11/src/Searchable.php#L279
$engine = $dummy->searchableUsing();
// Should resolve to MeiliSearch\Endpoints\Indexes via a magic method that resolves the underlying Meili driver:
// https://github.com/laravel/scout/blob/33bbff0e3bfb1abd0ea34236c331fc17cdeac0bc/src/Engines/MeiliSearchEngine.php#L298
// ->
// https://github.com/meilisearch/meilisearch-php/blob/f25ee49b658f407af3d3f1f9a402997e7974b6bb/src/Delegates/HandlesIndex.php#L23
$index = $engine->index($dummy->searchableAs());
// https://github.com/meilisearch/meilisearch-php/blob/f25ee49b658f407af3d3f1f9a402997e7974b6bb/src/Endpoints/Delegates/HandlesSettings.php#L55
$index->updateSearchableAttributes(
['id','title', 'link', 'raw_text', 'subchan', 'nsfw']
);
一旦 created_at
已编入索引但不可搜索,您希望根据该值进行过滤。美利有operators for numeric values。
第 3 步是使用 Scout 执行 custom search:
Comment::search($query, function (Indexes $meilisearch, $query, $options) {
$options['filters'] = 'created_at>795484800';
return $meilisearch->search($query, $options);
});
再说一次,这是伪代码——我没有测试它的任何部分。如果 Scout 支持自定义索引的设置 on creation 或公开更新设置的方法,例如允许您在配置文件中添加特定于驱动程序的设置,我将非常感激。