0

我已经在我的应用程序中成功地通过 laravel scout 和 spatie-query-buildr 集成了 meilisearch,但是我在精细过滤返回的数据时遇到了问题。

这是我的控制器

public function list(Request $request): JsonResponse
{
    $defaultItems = 10;
    $searchIds = blank($request->kw) ? [] : Job::search($request->kw)->take(300)->keys();
    $lang = blank($request->lang) ? 'de' : $request->lang;

    $jobs = QueryBuilder::for(Job::class)
        ->select([
            'id',
            'uuid',
            'jid',
            'body',
            'responsibilities',
            'qualifications',
            'summary',
            'publish_start_date',
            'publish_end_date'
        ])
        ->allowedSorts(
            'id',
            'jid',
            'uuid',
            'title',
            'slug',
            'publish_start_date',
            'publish_end_date'
        )
        ->allowedAppends(
            'language',
            'source',
            'contracts',
            'tags',
            'locations',
            'level',
            'locations.country'
        )
        ->allowedFields([
            'id',
            'jid',
            'uuid',
            'publish_start_date',
            'publish_end_date',
            'responsibilities',
            'qualifications',
            'body',
            'slug',
            'language',
            'language_id',
            'language.name',
            'title',
            'fields',
            'contracts',
            'contracts.title',
            'tags',
            'tags.title',
            'level',
            'level_id',
            'level.title'

        ])
        ->allowedIncludes(
            'locations',
            'locations.country',
            'language',
            'fields',
            'level',
            //'levels',
            'tags',
            'tags',
            'contracts'
        )
        ->allowedFilters([
            AllowedFilter::exact('id'),
            AllowedFilter::exact('uuid'),
            AllowedFilter::exact('language.name'),
            AllowedFilter::exact('language_id'),
            AllowedFilter::exact('source_id'),
            AllowedFilter::exact('locations', 'locations.id'),
            AllowedFilter::exact('fields', 'fields.id'),
            AllowedFilter::exact('levels', 'level.id'),
            AllowedFilter::exact('jid'),
            AllowedFilter::exact('slug'),
            AllowedFilter::exact('tags.slug', 'tags.slug->' . $lang),
            'title',
            'tags'
        ])
        ->tap(function ($query) use ($searchIds) {
            return empty($searchIds) ? $query : $query->whereIn('id', $searchIds);
        })
        ->paginate($defaultItems)
        ->appends(request()
            ->query());

    return response()->json($jobs);
}

这是我的带有可搜索数组的模型

public function toSearchableArray()
{
    $array['uuid'] = $this->uuid;
    $array['jid'] = $this->jid;
    $array['jid_1'] = $this->jid_1;
    $array['jid_2'] = $this->jid_2;
    $array['jid_3'] = $this->jid_3;
    $array['jid_4'] = $this->jid_4;
    $array['title'] = $this->title;
    $array['slug'] = $this->slug;
    $array['body_raw'] = $this->body_raw;
    $array['id'] = $this->id;

    return $array;
}

如果jid_4查询完全匹配,则应排除所有其他结果。有没有办法挂钩 meilisearch 结果或其他方法进行微调?

4

0 回答 0