Skip to content

Commit

Permalink
更新 readme
Browse files Browse the repository at this point in the history
  • Loading branch information
vanry committed Feb 23, 2020
1 parent cf2edb2 commit d5882f0
Showing 1 changed file with 75 additions and 76 deletions.
151 changes: 75 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,84 +1,57 @@

> 说明: `2.x` 版本只支持 `Laravel 5.5` 以上版本,`Laravel 5.5`以下版本请使用 [1.x版本](https://github.com/vanry/laravel-scout-tntsearch/tree/1.x)
## 安装

通过 `composer` 安装:
通过 `composer` 安装

``` bash
composer require vanry/laravel-scout-tntsearch
```

添加到服务提供者:
### Laravel

```php
// config/app.php
'providers' => [
// ...
Laravel\Scout\ScoutServiceProvider::class,
Vanry\Scout\TNTSearchScoutServiceProvider::class,
],
```
`Laravel` 具有包自动发现功能,不用手动添加服务提供者。

`.env` 文件中添加 `SCOUT_DRIVER=tntsearch`

然后就可以将 `scout.php` 配置文件发布到 `config` 目录。
发布 `scout` 配置文件

```bash
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
```

`config/scout.php` 中添加:
如果要修改 `tntsearch` 默认配置,也可发布 `tntsearch` 配置文件

```bash
php artisan vendor:publish --provider="Vanry\Scout\TNTSearchScoutServiceProvider"
```

### Lumen

`Lumen` 需将服务提供者添加到 `bootstrap/app.php`

```php
// bootstrap/app.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' => [
'的',
'了',
'而是',
],
],
// 取消注释
$app->withEloquent()

// 注意先后顺序
$app->register(Vanry\Scout\LumenServiceProvider::class);
$app->register(Laravel\Scout\ScoutServiceProvider::class);
```

### 启用

`.env` 文件中添加

```bash
SCOUT_DRIVER=tntsearch
```

你也可以在模型中直接添加 `asYouType` 选项, 参考下面的示例。

## 用法

* 添加 `Searchable Trait` 到模型

```php
namespace App;
Expand All @@ -90,8 +63,6 @@ class Post extends Model
{
use Searchable;

public $asYouType = true;

/**
* Get the indexable data array for the model.
*
Expand All @@ -108,48 +79,76 @@ class Post extends Model
}
```

同步数据到搜索服务:
* 同步数据到搜索索引

`php artisan scout:import App\\Post`
```php
# scout 命令
php artisan scout:import App\\Post

# tntsearch 命令, 性能更好
php artisan tntsearch:import App\\Post
```

使用模型进行搜索:

`Post::search('世界杯直播')->get();`
* 使用模型进行搜索

```php
Post::search('laravel教程')->get();
```

## 中文分词

目前支持 `jieba`, `phpanalysis``scws` 中文分词,在 `.env` 文件中配置 `TNTSEARCH_TOKENIZER` 可选值 为 `jieba`, `analysis`, `scws`, `default`, 其中 `default``TNTSearch` 自带的分词器
目前支持 `scws`, `jieba``phpanalysis` 中文分词。

> 考虑到性能问题,建议生产环境使用由`C`语言编写的`scws`分词扩展。
### 对比

- 使用 `jieba` 分词器,需安装 `fukuball/jieba-php`
* `scws` 是用 `C` 语言编写的 `php` 扩展,性能最佳,分词效果好,但不支持 `Windows` 系统。
* `jieba``python` 版本结巴分词的 `php` 实现,分词效果最好,尤其是新词发现,不足之处是性能较差,占用内存大。
* `phpanalysis``php` 编写的一款轻量分词器,分词效果不错,性能介于 `scws``jieba` 两者之间。

composer require fukuball/jieba-php
### 安装

- 使用 `phpanalysis` 分词器,需安装 `lmz/phpanalysis`
- **scws**

composer require lmz/phpanalysis
```bash
composer require vanry/scws
```

- 使用 `scws` 分词器,需安装 `vanry/scws`
- **jieba**

composer require vanry/scws
```bash
composer require fukuball/jieba-php
```

- **phpanalysis**

```bash
composer require lmz/phpanalysis
```

### 配置

`.env` 文件中配置

```bash
TNTSEARCH_TOKENIZER=scws
```

> 可选值为 `default`, `scws`, `jieba`, `phpanalysis`, 其中 `default``TNTSearch` 自带的分词器。
分别在 `config/scout.php` 中的 `jieba`, `analysis``scws` 中修改配置。

## 高亮

`view composer` 中引入 `highlighter`
`view composer` 中引入 `highlighter`, 可使用以上任一分词器。

```php
use Vanry\Scout\Highlighter;
use Vanry\Scout\Tokenizers\PhpAnalysisTokenizer;

// ...

view()->composer('search', function ($view) {
$tokenizer = app('tntsearch.tokenizer')->driver();

$view->with('highlighter', new Highlighter($tokenizer));
$view->with('highlighter', new Highlighter(new PhpAnalysisTokenizer));
});
```

Expand Down

0 comments on commit d5882f0

Please sign in to comment.