简体   繁体   中英

laravel scout add additional attribute (only in meilisearch)

I try to migrate a SQL based search to meilisearch using laravel scout.

At the moment the whole search should be migrated to meilisearch, including all filter and sorting options.

A product has a relation to feedbacks (product model):

//returns all feedbacks for the product
public function allFeedbacks()
{
    return $this->hasMany('App\Models\Feedback');
}

I would like to include the amount of feedbacks to meilisearch, but not the whole relation, since it's not required for sorting.

How can I add additional fields to be index by meilisearch, without including a field into the mySQL database ( feedback_amount fe)?

In Product.php :

public function toSearchableArray() {
  $fields = [
    'feedback_amount' => $this->allFeedbacks()->count(),
    'price' => $this->price,
    'other_stuff' => $this->other_stuff
  ];

  return $fields;
}

Then flush and import the model again.

This will override the parent toSearchableArray() so you will want to include any other fields you want searchable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM