laracms 的关键词搜索 TNTSearch 如何支持到搜索单页?
文章关键词搜索只能搜到文章列表(类型为article)的内容而不能搜到单页(类型为page)的?
了解到的相关内容如下,尝试过屏蔽 static::addGlobalScope方法重新生成articles.index也不能解决问题;
1.控制器的方法是:
$query = $request->input('query');
$articles = Article::search($query)->get()->toArray();
2.Article类有定义了个全局的搜索类型方法:
protected static function boot()
{
parent::boot();
static::addGlobalScope('type', function (Builder $builder) {
$builder->where($builder->qualifyColumn('type'), '=', request('type','article'));
//$builder->with(['created_user','updated_user']);
});
}
public function toSearchableArray()
{
// $array = $this->toArray();
$array = [
'id' => $this->id,
'title' => $this->title,
'subtitle' => $this->subtitle,
'keywords' => $this->keywords,
'description' => $this->description,
'author' => $this->author,
'content' => $this->content,
];
return $array;
}
3.laracms/vendor/laravel/scout/src/Searchable.php执行的方法是:
/**
* Perform a search against the model's indexed data.
*
* @param string $query
* @param \Closure $callback
* @return \Laravel\Scout\Builder
*/
public static function search($query = '', $callback = null)
{
return app(Builder::class, [
'model' => new static,
'query' => $query,
'callback' => $callback,
'softDelete'=> static::usesSoftDelete() && config('scout.soft_delete', false),
]);
}
4.了解到这个搜索使用TNTSearch生成的articles.index来搜索的:
<?php
/**
* LaraCMS - CMS based on laravel
*
* @category LaraCMS
* @package Laravel
* @author Wanglelecc <wanglelecc@gmail.com>
* @date 2018/06/06 09:08:00
* @copyright Copyright 2018 LaraCMS
* @license https://opensource.org/licenses/MIT
* @github https://github.com/wanglelecc/laracms
* @link https://www.laracms.cn
* @version Release 1.0
*/
namespace Wanglelecc\Laracms\Console\Commands;
use Illuminate\Console\Command;
use TeamTNT\TNTSearch\TNTSearch;
use Wanglelecc\Laracms\Models\Article;
use Wanglelecc\Laracms\Handlers\TokenizerHandler;
/**
* 手动生成文章分词索引
*
* Class IndexArticle
* @package Wanglelecc\Laracms\Console\Commands
*/
class IndexArticle extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'laracms:article-index';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Index the article table';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle(Article $article)
{
$driver = config('database.default');
$connections = config("database.connections.$driver");
$prefix = $connections['prefix'];
$config = config('scout.tntsearch') + $connections;
$table = $prefix.$article->getTable();
$tnt = new TNTSearch;
$tnt->loadConfig($config);
// $tnt->setTokenizer(new TokenizerHandler(config('scout.tntsearch.tokenizer.jieba')));
$tnt->setDatabaseHandle(app('db')->connection()->getPdo());
$indexer = $tnt->createIndex('articles.index');
$indexer->query("SELECT id, alias, title, subtitle, keywords, description, author, content FROM {$table}");
// $indexer->setLanguage('no');
$indexer->run();
}
}
个人猜测可能在生成索引的时候就写好对应sql了的;
了解过相关处理的望告知下解决方案或相关原理,谢谢;
直接用laravel的模糊查询做了,不用tntsearch;