PHPackages                             oaktree/oaktree-tntsearch - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. oaktree/oaktree-tntsearch

ActiveLibrary[Search &amp; Filtering](/categories/search)

oaktree/oaktree-tntsearch
=========================

包含中文分词的 Laravel Scout TNTSearch 驱动，支持 scws, phpanalysis 和 jieba 分词。

01PHPCI failing

Since Sep 28Pushed 6y ago1 watchersCompare

[ Source](https://github.com/oaktree180/oaktree_tntsearch)[ Packagist](https://packagist.org/packages/oaktree/oaktree-tntsearch)[ RSS](/packages/oaktree-oaktree-tntsearch/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

安装
--

[](#安装)

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

教程
--

[](#教程)

[laravel下TNTSearch+jieba-php实现全文搜索](https://baijunyao.com/article/154)

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity1

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6368c4ee74a54960e263a8ab9b509ab0166c9b6d5ea6fb860fbc1d909a9d16e4?d=identicon)[oaktree180](/maintainers/oaktree180)

---

Top Contributors

[![oaktree180](https://avatars.githubusercontent.com/u/38369934?v=4)](https://github.com/oaktree180 "oaktree180 (4 commits)")

### Embed Badge

![Health badge](/badges/oaktree-oaktree-tntsearch/health.svg)

```
[![Health](https://phpackages.com/badges/oaktree-oaktree-tntsearch/health.svg)](https://phpackages.com/packages/oaktree-oaktree-tntsearch)
```

###  Alternatives

[ircmaxell/filterus

A library for filtering variables in PHP

44613.4k6](/packages/ircmaxell-filterus)[awesome-nova/dependent-filter

Dependent filters for Laravel Nova

26190.2k](/packages/awesome-nova-dependent-filter)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
