php分词搜索thinkphp,TP5+TNTSearch实现中文分词搜索

安装

composer require teamtnt/tntsearch

composer require fukuball/jieba-php

环境要求

PHP >= 7.1

PDO PHP Extension

SQLite PHP Extension

mbstring PHP Extension

案例

1.创建搜索服务类。

namespace app\index\service;

use TeamTNT\TNTSearch\TNTSearch;

class TNTSearchService

{

protected $indexName="title";

protected $config=[];

protected $tnt;

public function __construct()

{

$config=[

'driver' => 'mysql',

'host' => config("database.hostname"),

'database' => config("database.database"),

'username' => config("database.username"),

'password' => config("database.password"),

'storage' => ROOT_PATH.'public/tntsearch/examples/',

'stemmer' => \TeamTNT\TNTSearch\Stemmer\PorterStemmer::class//optional

];

$this->config=$config;

$this->tnt = new TNTSearch;

$this->tnt->loadConfig($this->config);

}

/**

* 创建索引

* @param $token

*

*/

public function createIndex($token){

$indexer = $this->tnt->createIndex($this->indexName);

$indexer->query('SELECT id, title FROM goods;');

$indexer->setTokenizer($token);

$indexer->inMemory = false;

$indexer->run();

}

/**

*

* @param $keyword

* @return array

* @throws \TeamTNT\TNTSearch\Exceptions\IndexNotFoundException

*/

public function search($keyword){

$tnt=$this->tnt;

$tnt->selectIndex($this->indexName);

$tnt->fuzziness = true;

$tnt->setTokenizer();

$res = $tnt->search($keyword);

return $res;

}

}

2.配置索引存放地址(public/tntsearch/examples)。

3.文件夹添加读写权限。

3.创建分词类。

namespace app\index\base;

use Fukuball\Jieba\Jieba;

use Fukuball\Jieba\Finalseg;

use TeamTNT\TNTSearch\Support\TokenizerInterface;

class JiebaTokenizer implements TokenizerInterface

{

public function __construct(array $options = [])

{

Jieba::init($options);

if (isset($options['user_dict'])) {

Jieba::loadUserDict($options['user_dict']);

}

Finalseg::init($options);

}

public function tokenize($text,$stopwords = [])

{

$tokens = Jieba::cutForSearch($text);

return $tokens;

}

}

4.创建控制器。

namespace app\index\controller;

use app\index\base\JiebaTokenizer;

use app\index\service\TNTSearchService;

use think\Db;

class Search

{

/**

* 初始化索引文件

*/

public function index(){

$token=new JiebaTokenizer();

(new TNTSearchService())->createIndex($token);

}

/**

* 搜索

* @throws \TeamTNT\TNTSearch\Exceptions\IndexNotFoundException

*

*

*/

public function search(){

$res= (new TNTSearchService())->search('文玩铁核桃');

dump($res);

}

}

5.调用index方法初始化索引文件,在项目(public/tntsearch/examples/)下。

6.调用search方法返回搜索结果。

array(3) {

["ids"] => array(2) {

[0] => int(3)

[1] => int(2)

}

["hits"] => int(2)

["execution_time"] => string(8) "4.179 ms"

}

7.索引的增加修改删除,参考。

数据库

CREATE TABLE `goods` (

`id` int(11) unsigned NOT NULL AUTO_INCREMENT,

`title` varchar(255) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO `bbb`.`goods`(`id`, `title`) VALUES (1, '专柜正品双星武术鞋太极鞋练功鞋晨练功夫鞋牛筋底帆布木兰鞋男女');

INSERT INTO `bbb`.`goods`(`id`, `title`) VALUES (2, '文玩核桃 星狮子头虎头官帽异型-纯野生盘龙纹大双棒联体【精品】');

INSERT INTO `bbb`.`goods`(`id`, `title`) VALUES (3, '文玩把玩铁核桃 野生大铁狮子头 金蟾蛤蟆头古玩手玩 异形核桃');

你可能感兴趣的:(php分词搜索thinkphp)