makeAllSearchableUsing无法使用elasticsearch加载关系

mcvgt66p  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(97)

在我的Laravel模型中有以下代码:

public function toSearchableArray() {
    return [
        'name' => $this->name,
        'description' => $this->description,
    ];
}

public function mappableAs(): array {
    return [
        'name' => 'text',
        'description' => 'text',
    ];
}

public function makeAllSearchableUsing($query) {
    return $query->with('users');
}

public function users() {
    return $this->belongsToMany(User::class, 'users');
}

我正在使用Explorer与Laravel Scout,我已经刷新并导入了数据。
当我运行查询Model::search('fish')->field('name')->get()时,输出仅包含以下数据:

id: 1,
name: "Ea nemo fish unde esse",
description: "Tempore nihil cumque eum quo"

但是,我希望得到的用户关系数据如下所示:

users: {
  {id: 1, name: 'User1'},
  {id: 2, name: 'User2'},
}

或者添加$query->withCount('users');的关系并按其排序。
是什么导致了这个问题?
这是我的用户模型

public function toSearchableArray(): array {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
        ];
    }

    public function mappableAs(): array {
        return [
            'id' => 'integer',
            'name' => 'text',
            'email' => 'text',
        ];
    }

public function models($owner = true) {
     return $this->hasMany(Model::class);
}

我的explorer.php

'indexes' => [
        Model::class,
        User::class,
    ],
kulphzqa

kulphzqa1#

您必须在toSearchableArray方法中包含users关系:

public function toSearchableArray() {
    return [
        'name' => $this->name,
        'description' => $this->description,
        'users' => $this->users,
    ];
}

相关问题