如何使用Algolia搜索仅索引我模型的相关字段?

时间:2017-10-17 17:47:02

标签: mongodb algolia

我正在我的一个应用程序中加入Algolia搜索并运行到10 KB的文档大小限制。我只想索引模型中的某些字段,而不是整个事物。我正在使用mongo,它有嵌入式和相关的文件,我不在乎。

我按照https://laravel.com/docs/5.3/scout

上的说明操作

现在,当我对数据编制索引时,我收到的警告是,我的2000个左右文档中大约有100个超过了10 KB的大小。

我如何告诉Algolia我只想索引某些字段并忽略其他一些数据,例如关系或嵌入文档?

1 个答案:

答案 0 :(得分:2)

您可以在您添加可搜索特征的班级中使用Laravel的toSearchableArray。您只需包含实际要搜索的部分。

public function toSearchableArray() {
  $array = $this->toArray();
  $array = [
    'id' => $this->id,
    'name' => $this->name,
    'description' => $this->description,
    'price' => (int)$this->price,
    'likes' => (int)$this->likes,
    'slug' => $this->slug
  ];
  return $array;
}

这就是你要找的东西吗?