目录
- 必须极速安装~
- 还配置啥,立即开始用吧!
- 即用模式
- 专业模式
WindSearch 是一款基于中文分词的纯 PHP 全文检索引擎,旨在帮助开发者快速搭建 PHP 站点的站内搜索功能。该引擎具备零繁琐安装配置、无需后期维护调优以及不占用服务器内存等优势,能够与现有的 PHP 项目无缝融合。
GitHub 地址:github.com/rock365/windsearch
必须极速安装~
使用 composer 安装:
composer require rock365/windsearch
或 使用 Git 安装:
git clone git@github.com:rock365/windsearch.git
或 直接前往 github: github.com/rock365/windsearch
还配置啥,立即开始用吧!
WindSearch 提供即用模式和专业模式两种使用方式。其中,即用模式适用于简单的搜索场景,而专业模式则支持更复杂的搜索需求。
即用模式
“即用模式”允许用户立即导入数据,全程无需任何配置,并支持 int 主键和 uuid 主键,非常适合简单的搜索场景。该模式下的各种 API 均带有 fast 关键字。
“即用模式”的工作原理是对字符串进行 ngram 分词,搜索结果返回的是主键集合。开发者可以利用这些主键集合从 MySQL 等数据库中查询出原始数据。
引入文件:
WindSearch 安装完成后,需引入入口文件,请注意具体的文件路径。
require_once 'yourdirname/vendor/autoload.php';
导入数据
// 实例化对象
$Wind = new \WindSearch\Index\Wind('test'); //test 当前索引库的名称
// 清空之前的数据(如果之前使用即用模式导入过数据)
$Wind->deleteFastIndex();
// 批次导入数据
// $res 是从数据库查询的数据
foreach($res as $v){
$text = $v['title'];
$primarykey = $v['id'];
// $text是需要搜索的具体内容,比如title;$primarykey是主键值,比如id的值
$Wind->fastIndexer($text, $primarykey);
}
//每导入一批数据,就调用此方法进行保存
$Wind->fastBatchWrite();
// 所有数据全部导入完成后,接着构建索引(不一定非得紧接着调用,也可以在其它地方单独调用)
$Wind->fastBuildIndex();
开始搜索
// 开始搜索
$Wind = new \WindSearch\Index\Wind('test');
// 调用搜索方法
// $page 第几页 $listRows 每页多少条
$res = $Wind->fastSearch($text,$page,$listRows)
// $res:返回的主键(比如id)集合,你可以使用id集合从MySQL等数据库查询原始数据
每个索引库都可以使用即用模式导入数据,数据单独存放,跟专业模式的数据不冲突。由于即用模式属于某个索引库的下属模块,所以删除某个索引库时,同样会删除即用模式的索引数据,因此一个索引库名称尽量只使用一种模式。
注意,即用模式的搜索效果可能比不上专业模式,可根据情况作出取舍。
专业模式
(专业的部分配合文档使用更佳)
引入文件:
WindSearch 安装完成后,引入入口文件,注意具体文件路径
require_once 'yourdirname/vendor/autoload.php';
建索引库:
复制修改粘贴即可,跟mysql建表差不多
$mapping = [
//设置索引库的名称,比如对应的表名
'name' => 'test',
// 字段配置
'field' => [
[
'name' => 'id',// 主键名称 主键必须设置
'type' => 'primarykey', //数据类型为主键 必须设置
'primarykey_type' => 'Int_Incremental', // int递增
],
[
'name' => 'title',
'index' => true, // 是否索引此字段
'type' => 'text',
'analyzer' => 'segment', // 配置分词方式
],
[
'name' => 'tags',
'index' => true,
'type' => 'keyword',
]
[
'name' => 'score',
'type' => 'numeric',
],
[
'name' => 'time',
'type' => 'date'
],
[
'name' => 'descr',
'type' => 'text',
],
]
];
// 实例化对象
$Wind = new \WindSearch\Index\Wind('test'); //test 当前索引库的名称
//检查是否存在此索引库
$is_index = $Wind->checkIndex();
// 如果存在此索引库
if ($is_index) {
//删除索引库
$Wind->delIndex();
}
//创建索引库
$Wind->createIndex($mapping);
导入数据:
//实例化引擎
$Wind = new \WindSearch\Index\Wind('test');
// 初始化
$Wind->buildIndexInit();
// 开启分词,导入数据时,加true可加快速度
$Wind->loadAnalyzer(true);
// 数据量小(内容少于一万条),则可以一次性全部导入
// selectAll...
// $result:一次性查询的所有内容
foreach ($result as $v) {
$Wind->indexer($v);
}
// 批量写入文件保存
$Wind->batchWrite();
构建索引:
// 数据导入结束后,接着可立即调用此方法构建索引
// 注意,数据量大时,此步骤会比较耗时
$Wind->buildIndex();
开始搜索:
//实例化引擎
$Wind = new \WindSearch\Index\Wind('test');
//开启分词功能
$Wind->loadAnalyzer();
//开始搜索
// 搜索单个字段
$query = [
'match' => [
'field' => [
'name' => 'title',
'query' => $text,
],
'list_rows' => $listRows, //每页多少条数据
'page' => $page, //第几页
]
];
// 搜索接口
$res = $Wind->search($query, $page, $listRows);
// 返回的最终结果,可直接渲染到前台页面
$resArr = $res['result']['_source'];
以上就是PHP WindSearch实现站内搜索功能的详细内容,更多关于PHP WindSearch站内搜索的资料请关注本站其它相关文章!