• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    PHP WindSearch实现站内搜索功能

    发布者: 网神之王 | 发布时间: 2025-6-14 13:14| 查看数: 68| 评论数: 0|帖子模式

    WindSearch是一个基于中文分词,由纯PHP开发全文检索引擎,可快速搭建PHP站点的站内搜索,他没有任何繁琐的安装配置、不需要维护调优、不占用服务器内存、可与PHP项目完美融合在一起。
    github地址:github.com/rock365/windsearch

    必须极速安装~

    使用composer安装:
    1. composer require rock365/windsearch
    复制代码
    或 使用Git安装:
    1. git clone git@github.com:rock365/windsearch.git
    复制代码
    或 直接前往github: github.com/rock365/windsearch

    还配置啥,立即开始用吧!

    WindSearch包含即用模式、专业模式,即用模式适合简单搜索场景,专业模式支持复杂搜索。

    即用模式

    “即用模式”可以立即导入数据,无任何配置,支持int主键、uuid主键,适合简单的搜索场景。即用模式的各种api均有
    1. fast
    复制代码
    关键字。
    “即用模式”的原理:对字符串进行ngram分词,搜索的结果是主键集合,你可以使用这些集合从MySQL等数据库查询原始数据。
    引入文件:
    WindSearch安装完成后,引入入口文件,注意具体文件路径
    1. require_once 'yourdirname/vendor/autoload.php';
    复制代码
    导入数据
    1. // 实例化对象
    2. $Wind = new \WindSearch\Index\Wind('test'); //test 当前索引库的名称
    3. // 清空之前的数据(如果之前使用即用模式导入过数据)
    4. $Wind->deleteFastIndex();
    5. // 批次导入数据
    6. // $res 是从数据库查询的数据
    7. foreach($res as $v){
    8.     $text = $v['title'];
    9.     $primarykey = $v['id'];
    10.     // $text是需要搜索的具体内容,比如title;$primarykey是主键值,比如id的值
    11.         $Wind->fastIndexer($text, $primarykey);
    12. }
    13. //每导入一批数据,就调用此方法进行保存
    14. $Wind->fastBatchWrite();

    15. // 所有数据全部导入完成后,接着构建索引(不一定非得紧接着调用,也可以在其它地方单独调用)
    16. $Wind->fastBuildIndex();
    复制代码
    开始搜索
    1. // 开始搜索
    2. $Wind = new \WindSearch\Index\Wind('test');
    3. // 调用搜索方法
    4. // $page 第几页 $listRows 每页多少条
    5. $res = $Wind->fastSearch($text,$page,$listRows)
    6. // $res:返回的主键(比如id)集合,你可以使用id集合从MySQL等数据库查询原始数据
    复制代码
    每个索引库都可以使用即用模式导入数据,数据单独存放,跟专业模式的数据不冲突,由于即用模式属于某个索引库的下属模块,所以删除某个索引库时,同样会删除即用模式的索引数据,所以一个索引库名称尽量只使用一种模式。
    注意,即用模式的搜索效果可能比不上专业模式,可根据情况作出取舍。

    专业模式

    (专业的部分配合文档使用更佳)
    引入文件:
    WindSearch安装完成后,引入入口文件,注意具体文件路径
    1. require_once 'yourdirname/vendor/autoload.php';
    复制代码
    建索引库:
    复制修改粘贴即可,跟mysql建表差不多
    1. $mapping = [
    2.           //设置索引库的名称,比如对应的表名
    3.     'name' => 'test',
    4.     // 字段配置
    5.     'field' => [
    6.         [
    7.             'name' => 'id',// 主键名称 主键必须设置
    8.             'type' => 'primarykey', //数据类型为主键 必须设置
    9.             'primarykey_type' => 'Int_Incremental', // int递增
    10.         ],
    11.         [
    12.             'name' => 'title',
    13.             'index' => true, // 是否索引此字段
    14.             'type' => 'text',
    15.             'analyzer' => 'segment', // 配置分词方式
    16.         ],
    17.         [
    18.             'name' => 'tags',
    19.             'index' => true,
    20.             'type' => 'keyword',
    21.         ]
    22.         [
    23.             'name' => 'score',
    24.             'type' => 'numeric',
    25.         ],
    26.         [
    27.             'name' => 'time',
    28.             'type' => 'date'
    29.         ],

    30.         [
    31.             'name' => 'descr',
    32.             'type' => 'text',
    33.         ],

    34.     ]

    35. ];

    36. // 实例化对象
    37. $Wind = new \WindSearch\Index\Wind('test'); //test 当前索引库的名称
    38. //检查是否存在此索引库
    39. $is_index = $Wind->checkIndex();
    40. // 如果存在此索引库
    41. if ($is_index) {
    42.     //删除索引库
    43.     $Wind->delIndex();
    44. }
    45. //创建索引库
    46. $Wind->createIndex($mapping);
    复制代码
    导入数据:
    1. //实例化引擎
    2. $Wind = new \WindSearch\Index\Wind('test');
    3. // 初始化
    4. $Wind->buildIndexInit();
    5. // 开启分词,导入数据时,加true可加快速度
    6. $Wind->loadAnalyzer(true);

    7. // 数据量小(内容少于一万条),则可以一次性全部导入
    8. // selectAll...
    9. // $result:一次性查询的所有内容
    10. foreach ($result as $v) {
    11.     $Wind->indexer($v);
    12. }
    13. // 批量写入文件保存
    14. $Wind->batchWrite();
    复制代码
    构建索引:
    1. // 数据导入结束后,接着可立即调用此方法构建索引
    2. // 注意,数据量大时,此步骤会比较耗时
    3. $Wind->buildIndex();
    复制代码
    开始搜索:
    1. //实例化引擎
    2. $Wind = new \WindSearch\Index\Wind('test');

    3. //开启分词功能
    4. $Wind->loadAnalyzer();

    5. //开始搜索

    6. // 搜索单个字段
    7. $query = [
    8.     'match' => [
    9.         'field' => [
    10.             'name' => 'title',
    11.             'query' => $text,
    12.         ],
    13.         'list_rows' => $listRows, //每页多少条数据
    14.         'page' => $page, //第几页

    15.     ]

    16. ];

    17. // 搜索接口
    18. $res = $Wind->search($query, $page, $listRows);
    19. // 返回的最终结果,可直接渲染到前台页面
    20. $resArr = $res['result']['_source'];
    复制代码
    以上就是PHP WindSearch实现站内搜索功能的详细内容,更多关于PHP WindSearch站内搜索的资料请关注脚本之家其它相关文章!

    来源:https://www.jb51.net/program/336330p5r.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表