安装导入WindSearch
环境要求:
- UTF-8编码
- PHP ≥7.3
- mbstring Extension
- PDO Extension
- SQLite Extension
开始安装:
在github上将WindSearch下载到本地你喜欢的文件夹,这是一个纯PHP的全文检索引擎
地址:https://github.com/rock365/windsearch(点个star吧亲亲O(∩_∩)O~~)
再引入入口文件,注意具体文件路径
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'); //$indexName 当前索引库的名称
//检查是否存在此索引库
$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还有更深入、更丰富的搜索功能等你挖掘:
github地址:https://github.com/rock365/windsearch
在线开发文档:https://rock365.github.io/ 偶尔访问不稳定,多刷新几次即可
在github点个star吧亲亲O(∩_∩)O~~谢谢大家!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。