Stars: 174
Forks: 34
Pull Requests: 3
Issues: 22
Watchers: 7
Last Updated: 2021-11-20 06:47:57
包含中文分词的 Laravel Scout TNTSearch 驱动,支持 scws, phpanalysis 和 jieba 分词。
License: MIT License
Languages: PHP
说明:
2.x版本只支持Laravel 5.5以上版本,Laravel 5.5以下版本请使用 1.x版本。
需安装并开启
sqlite扩展
composer require vanry/laravel-scout-tntsearchscout 配置文件,已安装 scout 可省略。php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"tntsearch 默认配置,发布 tntsearch 配置文件。php artisan vendor:publish --provider="Vanry\Scout\TNTSearchScoutServiceProvider"Lumen 需将服务提供者添加到 bootstrap/app.php
// bootstrap/app.php
// 取消注释
$app->withFacades();
$app->withEloquent()
// 注意先后顺序
$app->register(Vanry\Scout\LumenServiceProvider::class);
$app->register(Laravel\Scout\ScoutServiceProvider::class);在根目录中创建 config 文件夹, 将 laravel scout 配置文件 scout.php 复制到 config 中。
如需修改 tntsearch 默认配置,则将配置文件 tntsearch.php 复制 config 中进行修改。
在 .env 文件中添加
SCOUT_DRIVER=tntsearchSearchable Traitnamespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
return [
'id' => $this->id,
'title' => $this->title,
'body' => strip_tags($this->body),
];
}
}# scout 命令
php artisan scout:import 'App\Post'
# tntsearch 命令, 性能更好
php artisan tntsearch:import 'App\Post'Post::search('laravel教程')->get();目前支持 scws, jieba 和 phpanalysis 中文分词,默认使用 phpanalysis 分词。
scws 是用 C 语言编写的 php 扩展,性能最好,分词效果好,但不支持 Windows 系统。jieba 为 python 版本结巴分词的 php 实现,分词效果最好,尤其是新词发现,不足之处是性能较差,占用内存大。phpanalysis 是 php 编写的一款轻量分词器,分词效果不错,性能介于 scws 和 jieba 两者之间。使用 scws 或者 jieba,需安装对应的分词驱动。
composer require vanry/scwscomposer require fukuball/jieba-php在 .env 文件中配置
# scws
TNTSEARCH_TOKENIZER=scws
# jieba
TNTSEARCH_TOKENIZER=jieba使用 jieba 分词可能会出现内存分配不足的错误信息:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)
在代码中增加内存限制即可
ini_set('memory_limit', '1024M');默认使用 em 作为高亮 html 标签,在 css 中设置高亮样式即可,也可以自定义高亮标签。
@highlight($text, $query, $tag);
- $text: 要高亮的字段
- $query: 搜索词
- $tag: 高亮的 html 标签
// 高亮 title 字段
@highlight($post->title, $query);
// 用 strong 作为高亮标签
@highlight($post->title, $query, 'strong');highlight($post->title, $query);
highlight($post->title, $query, 'strong');use Vanry\Scout\Highlighter;
// ...
app(Highlighter::class)->highlight($post->title, $query);
highlight帮助函数和Highlighter对象适合在api等非html视图中使用。