Package Data | |
---|---|
Maintainer Username: | vanry |
Maintainer Contact: | ivanry@163.com (vanry) |
Package Create Date: | 2017-09-23 |
Package Last Update: | 2021-11-20 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-16 15:03:18 |
Package Statistics | |
---|---|
Total Downloads: | 11,244 |
Monthly Downloads: | 37 |
Daily Downloads: | 6 |
Total Stars: | 172 |
Total Watchers: | 7 |
Total Forks: | 33 |
Total Open Issues: | 2 |
通过 composer
安装:
composer require vanry/laravel-scout-tntsearch
添加到服务提供者:
// config/app.php
'providers' => [
// ...
Laravel\Scout\ScoutServiceProvider::class,
Vanry\Scout\TNTSearchScoutServiceProvider::class,
],
在 .env
文件中添加 SCOUT_DRIVER=tntsearch
然后就可以将 scout.php
配置文件发布到 config
目录。
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
在 config/scout.php
中添加:
'tntsearch' => [
'storage' => storage_path('indexes'), //必须有可写权限
'fuzziness' => env('TNTSEARCH_FUZZINESS', false),
'searchBoolean' => env('TNTSEARCH_BOOLEAN', false),
'asYouType' => false,
'fuzzy' => [
'prefix_length' => 2,
'max_expansions' => 50,
'distance' => 2,
],
'tokenizer' => [
'driver' => env('TNTSEARCH_TOKENIZER', 'default'),
'jieba' => [
'dict' => 'small',
//'user_dict' => resource_path('dicts/mydict.txt'), //自定义词典路径
],
'analysis' => [
'result_type' => 2,
'unit_word' => true,
'differ_max' => true,
],
'scws' => [
'charset' => 'utf-8',
'dict' => '/usr/local/scws/etc/dict.utf8.xdb',
'rule' => '/usr/local/scws/etc/rules.utf8.ini',
'multi' => 1,
'ignore' => true,
'duality' => false,
],
],
'stopwords' => [
'的',
'了',
'而是',
],
],
你也可以在模型中直接添加 asYouType
选项, 参考下面的示例。
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
public $asYouType = true;
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
$array = $this->toArray();
// Customize array...
return $array;
}
}
同步数据到搜索服务:
php artisan scout:import App\\Post
使用模型进行搜索:
Post::search('世界杯直播')->get();
目前支持 jieba
, phpanalysis
和 scws
中文分词,在 .env
文件中配置 TNTSEARCH_TOKENIZER
可选值 为 jieba
, analysis
, scws
, default
, 其中 default
为 TNTSearch
自带的分词器。
考虑到性能问题,建议生产环境使用由
C
语言编写的scws
分词扩展。
使用 jieba
分词器,需安装 fukuball/jieba-php
composer require fukuball/jieba-php
使用 phpanalysis
分词器,需安装 lmz/phpanalysis
composer require lmz/phpanalysis
使用 scws
分词器,需安装 vanry/scws
composer require vanry/scws
分别在 config/scout.php
中的 jieba
, analysis
和 scws
中修改配置。
在 view composer
中引入 highlighter
use Vanry\Scout\Highlighter;
// ...
view()->composer('search', function ($view) {
$tokenizer = app('tntsearch.tokenizer')->driver();
$view->with('highlighter', new Highlighter($tokenizer));
});
// search.blade.php
{!! $highlighter->highlight($article->title, $query) !!}
{!! $highlighter->highlight($article->excerpt, $query) !!}
默认使用
em
作为高亮标签,在css
中设置样式即可。