vanry / laravel-scout-tntsearch by vanry

包含中文分词的 Laravel Scout TNTSearch 驱动,支持 scws, phpanalysis 和 jieba 分词。
11,283
173
7
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-12-16 15:03:43
Package Statistics
Total Downloads: 11,283
Monthly Downloads: 39
Daily Downloads: 2
Total Stars: 173
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, phpanalysisscws 中文分词,在 .env 文件中配置 TNTSEARCH_TOKENIZER 可选值 为 jieba, analysis, scws, default, 其中 defaultTNTSearch 自带的分词器。

考虑到性能问题,建议生产环境使用由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, analysisscws 中修改配置。

高亮

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 中设置样式即可。

教程

laravel下TNTSearch+jieba-php实现全文搜索