鍍金池/ 問答/PHP/ 對于 Laravel/Scout 全文搜索包的終極困惑?

對于 Laravel/Scout 全文搜索包的終極困惑?

使用默認的方法是沒問題的(如下):

public function toSearchableArray()
    {
        #_ Read Data
        $Arr_Posts = $this -> toArray();
        #_ Back to Scout
        return $Arr_Posts;
    }

但是我想加條件的話

public function toSearchableArray()
    {
        #_ Read Data
        $Arr_Posts = $this -> select(['title','content']) -> get() ->toArray();
        #_ Back to Scout
        return $Arr_Posts;
    }

就會出現(xiàn)如下提示:

Record at the position 6 objectID=10 is too big size=24300 bytes. Contact us if you need an extended quota
回答
編輯回答
哎呦喂

再次自問自答一波,反復(fù)看了 文檔 以及
Github 上的一些 issue 得出的結(jié)論:

  • 首先看文檔
默認情況下,「索引」會從模型的 toArray 方法中讀取數(shù)據(jù)來做持久化。如果要自定義同步到搜索索引的數(shù)據(jù),可以覆蓋模型上的 toSearchableArray 方法:
...
public function toSearchableArray()
{
    $array = $this->toArray();

    // Customize array...

    return $array;
}
...
  • 看來我之前的理解是錯誤的,文檔的意思是你必須用基于 toArray() 方法返回的數(shù)據(jù)才行,所以如果你需要篩選要索引的數(shù)據(jù)的話,你必須圍繞它來作文章,例如你只想要索引文章的標題和內(nèi)容的話:
...
public function toSearchableArray()
{
    #_ Read Data & Filter Field
    $Arr_Posts = array_only($this -> toArray(), ['title', 'content']);
    #_ Back to Scout
    return $Arr_Posts;
}
...
2017年2月7日 01:30