meilisearch / meilisearch

A lightning-fast search API that fits effortlessly into your apps, websites, and workflow
https://www.meilisearch.com
MIT License
47.27k stars 1.84k forks source link

Need Help - Touches Relationships Laravel scout & meilisearch #4431

Closed seifrached closed 6 months ago

seifrached commented 8 months ago

Framework: Laravel 8 with driver scout 9.5 Meilisearch v1.5.1

I'm creating a bookmark system for users by 3 models who User can make Many records Record Bookmarked by her owner and others Many Users Bookmark belongsTo Record and User

Models relationship

Record.php

class Record extends Model
{
    use HasFactory, Searchable, SoftDeletes;
    public $table = 'records';

    /**
     * All of the relationships to be touched.
     *
     * @var array
     */
    protected $touches = ['user'];

public function user(){
        return $this->belongsTo(User::class);
    }
    public function bookmarks(){    
        return $this->hasMany(Bookmark::class,'record_id')->withTrashed();
    }

}

User.php

public function bookmarks()
    {
        return $this->hasMany(Bookmark::class);
    }

    public function records()
    {
        return $this->hasMany(Record::class);
    }

Bookmark.php

class Bookmark extends Model
{
    use HasFactory, Searchable, SoftDeletes;
    public $table = 'bookmarks';
    protected $touches = ['user','record'];
    protected $fillable = ['record_id','user_id'];
    protected $dates = ['updated_at','created_at','deleted_at'];

public function user()
    {
        return $this->belongsTo(User::class);    
    }

    public function record()
    {
        return $this->belongsTo(Record::class);
    }

}

Everything is okey when the owner softDelete his record, The record un-indexed from Meilisearch Bookmarks Model and From User->bookmarks, the problem isnothing change for all others users who bookmarks this record, Meaning it is touching only her owner.

I tried to iterate all user->bookmarks to update the relationship but this solution it takes a lot of time and effort, and frankly, I do not consider it reasonable if the number of Bookmarkers is large.

Record.php

protected static function boot()
    {
        parent::boot();
        static::softDeleted(function ($model) {  
            $model->images()->delete();
            $model->bookmarks()->delete();
            $model->user->update();
            $model->images->unsearchable();
            $model->bookmarks->unsearchable();

            $bookmarkIds = $model->bookmarks()->pluck('id')->toArray();
            Bookmark::whereIn('id', $bookmarkIds)->unsearchable();
)};
       static::restored(function ($model) {
                $model->images()->restore();
                $model->bookmarks()->restore();
                $model->user->update();
                $model->images->searchable();
                $model->bookmarks->searchable();
                $model->searchable();

                $bookmarkIds = $model->bookmarks()->pluck('id')->toArray();
                Bookmark::whereIn('id', $bookmarkIds)->searchable();
        });
}

I'm skeptical whether that's the problem is from my Relationship design or Laravel scout or Meilisearch ! When I check Laravel Eloquent works fine Capture d'écran 2024-02-21 172410

Inside Meilisearch

Capture d'écran 2024-02-21 172317

please any suggest or solution.

Thanks

curquiza commented 8 months ago

Hello @seifrached

I'm not sure to understant what you do, and what you expect from Meilisearch, but I can say Meilisearch does not handle relation between documents and indexes. You have to update them on your side.


Also, for next time, you might have seen it when you opened the issue: opening an issue is not for support request.

Capture d’écran 2024-02-22 à 13 58 13
curquiza commented 6 months ago

I close this issue then. Feel free to ask to re-open if needed 😊