简体   繁体   English

Laravel Scout与TNTSearch

[英]Laravel Scout with TNTSearch

I am using Laravel Scout to replace my search functionality as it was previously one big SQL LIKE statement like similar to the below. 我正在使用Laravel Scout替换我的搜索功能,因为它以前是一条大型SQL LIKE语句,类似于以下内容。

$users = User::where('username', 'like', '%'.$request->get('q').'%')
    ->orWhere('displayName', 'like', '%'.$request->get('q').'%')
    ->orWhere('email', 'like', '%'.$request->get('q').'%')
    ->orWhere('role', 'like', '%'.$request->get('q').'%')
    ->orWhere('department', 'like', '%'.$request->get('q').'%')
    ->orWhere('location', 'like', '%'.$request->get('q').'%')
    ->orWhere('directDialIn', 'like', '%'.$request->get('q').'%')
    ->orWhere('mobileNumber', 'like', '%'.$request->get('q').'%')
    ->get();

I was doing this for multiple models I have such as Article, Event, etc. and the script ends up bloated. 我为多个模型(例如Article,Event等)执行此操作,脚本最终变得肿。 I followed the necessary steps in the documentation and set up Laravel Scout to use TNTSearch using the Laravel package made available by them. 我按照文档中的必要步骤进行了操作,并使用Laravel Scout使用它们提供的Laravel软件包来使用TNTSearch。

It says in the Laravel documentation that Laravel Scout is not so good at advanced where clauses, so following the documentation a bit further down the page, I did something like this at the top of my Controller. 它说在Laravel文档中,Laravel Scout在高级where子句方面不那么擅长,因此,在文档深入页面之后,我在Controller的顶部做了类似的事情。

use Searchable;

public function shouldBeSearchable()
{
    return $this->published === "open";
}

/**
 * Get the indexable data array for the model.
 *
 * @return array
 */
public function toSearchableArray()
{
    $array = $this->toArray();

    // Customize array...

    return $array;
}

It should only return models where published equals 'open' within my database table (or so I thought). 它应该只返回数据库表中已发布等于“ open”的模型(或者我认为如此)。 However, I run the following command: 但是,我运行以下命令:

php artisan scout:import "App\\Article"

Then perform a search, it still returns closed articles. 然后执行搜索,它仍然返回已关闭的文章。 I thought by defining shouldBeSearchable it would prevent this? 我认为通过定义shouldBeSearchable可以防止这种情况?

Also, is it possible to index relations on a model when performing a Scout search? 另外,执行Scout搜索时是否可以为模型上的索引建立索引? For instance, if a user has a profile can I use Scout to search the profile related to the user? 例如,如果用户有个人资料,我可以使用Scout搜索与该用户相关的个人资料吗? I want to be able to type text into a box, and have Scout scan the text and return the user the profile belongs to. 我希望能够在框中输入文本,并让Scout扫描文本并返回个人资料所属的用户。

I assume you are using teamtnt/laravel-scout-tntsearch-driver? 我假设您正在使用teamtnt / laravel-scout-tntsearch-driver? If so, it doesn't yet support Scout's shouldBeSearchable function. 如果是这样,它尚不支持Scout的shouldBeSearchable函数。

Here is the relevant issue ticket: https://github.com/teamtnt/laravel-scout-tntsearch-driver/issues/153 这是相关的问题单: https : //github.com/teamtnt/laravel-scout-tntsearch-driver/issues/153

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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