Skip to content

支援SCWS中文分詞功能的 Laravel Scout TNTSearch 驅動包

License

Notifications You must be signed in to change notification settings

JT501/laravel-scout-tntsearch-driver-chinese

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Scout TNTSearch 驅動包 (支援SCWS中文分詞) - Laravel 5.3/5.4/5.5/5.6/5.7

原版: https://github.com/teamtnt/laravel-scout-tntsearch-driver

安裝

通過 composer 安裝:

composer require JT501/laravel-scout-tntsearch-driver-chinese

(Laravel 5.5 以下版本) 添加服務提供者到 config/app.php:

'providers' => [
    // ...
    Laravel\Scout\ScoutServiceProvider::class,
    TeamTNT\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('TNTSearch'), //place where the index files will be stored
        'fuzziness' => env('TNTSEARCH_FUZZINESS', false),
        'fuzzy' => [
            'prefix_length' => 2,
            'max_expansions' => 50,
            'distance' => 2
        ],
        'asYouType' => false,
        'searchBoolean' => env('TNTSEARCH_BOOLEAN', false),
        'tokenizer' => [
            'scws' => [
                'charset' => 'utf-8',
                'dict' => '/usr/local/scws/etc/dict.utf8.xdb', // SCWS 詞典路徑
                'add_dict' => '/usr/local/scws/etc/dict_cht.utf8.xdb', // 加入額外詞典到 SCWS
                'rule' => '/usr/local/scws/etc/rules_chts.utf8.ini', // SCWS 規則路徑
                'multi' => false,
                'ignore' => true,
                'duality' => false,
            ],
        ],
        'stopwords' => [
            'a',
            'as',
            'the',
            '',
            '',
            '而是',
        ],
    ],

你也可以在模型中直接添加 asYouType 選項, 參考下面的示例。

用法

<?php

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();

        // 自定可搜索的Array

        return $array;
    }
}

同步數據到搜索服務:

php artisan scout:import App\\Post

使用模型進行搜索:

Post::search('你好世界')->get();

安裝中文分詞

目前默認只支援 scws 中文分詞。需要安裝SCWS拓展包SCWS PHP 擴展

安裝詳情