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, '文玩把玩铁核桃 野生大铁狮子头 金蟾蛤蟆头古玩手玩 异形核桃');

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是ThinkPHP6实现Redis连接池以及Redis队列的详细代码实现。 首先,在ThinkPHP6中使用Redis需要安装`topthink/think-redis`扩展,可以通过以下命令进行安装: ``` composer require topthink/think-redis ``` 接下来,我们需要在项目的配置文件中配置Redis连接信息,可以在`config/database.php`文件中添加以下代码: ```php 'redis' => [ 'type' => 'redis', 'hostname' => '127.0.0.1', 'password' => '', 'port' => 6379, 'select' => 0, 'timeout' => 0, 'prefix' => '', 'persistent' => true, 'pool' => [ 'min_connections' => 1, 'max_connections' => 10, 'wait_timeout' => 3, 'max_idle_time' => 60, ], ], ``` 配置项说明: - `type`:数据库类型,这里填写`redis`。 - `hostname`:Redis主机地址。 - `password`:Redis密码,如果没有设置密码可以不填写。 - `port`:Redis端口号,默认为6379。 - `select`:选择的数据库,默认为0。 - `timeout`:连接Redis的超时时间,默认为0表示不限制。 - `prefix`:设置的键名前缀,默认为空。 - `persistent`:是否使用持久化连接,默认为true。 - `pool`:配置连接池信息,包括最小连接数、最大连接数、等待超时时间和最大空闲时间。 接下来,我们可以通过以下代码获取Redis连接并进行操作: ```php use think\facade\Cache; // 获取Redis连接 $redis = Cache::store('redis')->handler(); // 设置键值对 $redis->set('name', 'Tom'); // 获取键值对 $name = $redis->get('name'); echo $name; ``` 以上代码中,我们使用了ThinkPHP6的缓存门面`think\facade\Cache`来获取Redis连接,通过`store`方法指定使用`redis`缓存驱动,再通过`handler`方法获取Redis连接。 接下来,我们来实现Redis队列功能,具体的代码如下: ```php use think\queue\Job; use think\facade\Cache; // 定义任务处理类 class TestJob { public function fire(Job $job, $data) { // 获取Redis连接 $redis = Cache::store('redis')->handler(); // 从队列中取出任务数据 $name = $data['name']; // 进行任务处理 // ... // 任务处理完成后删除任务 $job->delete(); } } // 将任务加入队列 $jobHandlerClassName = 'TestJob'; // 任务处理类名 $jobData = ['name' => 'Tom']; // 任务数据 $queueName = 'test_queue'; // 队列名称 $delay = 0; // 延迟时间,默认为0 \think\Queue::later($delay, $jobHandlerClassName, $jobData, $queueName); ``` 以上代码中,我们首先定义了一个任务处理类`TestJob`,它实现了`fire`方法来处理任务。在`fire`方法中,我们首先获取Redis连接,然后从队列中取出任务数据,进行任务处理,并最终删除任务。 接下来,我们将任务加入队列。在代码中,我们使用了`think\Queue`门面的`later`方法来将任务加入队列,指定了任务处理类名、任务数据、队列名称和延迟时间(默认为0表示不延迟)。 以上就是ThinkPHP6实现Redis连接池和Redis队列的详细代码实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值