当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP WindSearch实现站内搜索功能

发布:smiling 来源: PHP粉丝网  添加日期:2026-02-03 20:18:20 浏览: 评论:0 

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';

导入数据

  1. // 实例化对象 
  2.  
  3. $Windnew\WindSearch\Index\Wind('test'); //test 当前索引库的名称 
  4.  
  5. // 清空之前的数据(如果之前使用即用模式导入过数据) 
  6.  
  7. $Wind->deleteFastIndex(); 
  8.  
  9. // 批次导入数据 
  10.  
  11. // $res 是从数据库查询的数据 
  12.  
  13. foreach($resas$v){ 
  14.  
  15. $text$v['title']; 
  16.  
  17. $primarykey$v['id']; 
  18.  
  19. // $text是需要搜索的具体内容,比如title;$primarykey是主键值,比如id的值 
  20.  
  21. $Wind->fastIndexer($text$primarykey); 
  22.  
  23.  
  24. //每导入一批数据,就调用此方法进行保存 
  25.  
  26. $Wind->fastBatchWrite(); 
  27.  
  28. // 所有数据全部导入完成后,接着构建索引(不一定非得紧接着调用,也可以在其它地方单独调用) 
  29.  
  30. $Wind->fastBuildIndex(); 

开始搜索

  1. // 开始搜索 
  2.  
  3. $Windnew\WindSearch\Index\Wind('test'); 
  4.  
  5. // 调用搜索方法 
  6.  
  7. // $page 第几页 $listRows 每页多少条 
  8.  
  9. $res$Wind->fastSearch($text,$page,$listRows
  10.  
  11. // $res:返回的主键(比如id)集合,你可以使用id集合从MySQL等数据库查询原始数据 

每个索引库都可以使用即用模式导入数据,数据单独存放,跟专业模式的数据不冲突,由于即用模式属于某个索引库的下属模块,所以删除某个索引库时,同样会删除即用模式的索引数据,所以一个索引库名称尽量只使用一种模式。

注意,即用模式的搜索效果可能比不上专业模式,可根据情况作出取舍。

专业模式

(专业的部分配合文档使用更佳)

引入文件:

WindSearch安装完成后,引入入口文件,注意具体文件路径

require_once'yourdirname/vendor/autoload.php';

建索引库:

复制修改粘贴即可,跟mysql建表差不多

  1. $mapping= [ 
  2.  
  3. //设置索引库的名称,比如对应的表名 
  4.  
  5. 'name'=> 'test',  
  6.  
  7. // 字段配置 
  8.  
  9. 'field'=> [  
  10.  
  11.  
  12. 'name'=> 'id',// 主键名称 主键必须设置 
  13.  
  14. 'type'=> 'primarykey'//数据类型为主键 必须设置 
  15.  
  16. 'primarykey_type'=> 'Int_Incremental'// int递增 
  17.  
  18. ], 
  19.  
  20.  
  21. 'name'=> 'title'
  22.  
  23. 'index'=> true, // 是否索引此字段 
  24.  
  25. 'type'=> 'text'
  26.  
  27. 'analyzer'=> 'segment'// 配置分词方式 
  28.  
  29. ], 
  30.  
  31.  
  32. 'name'=> 'tags'
  33.  
  34. 'index'=> true, 
  35.  
  36. 'type'=> 'keyword',  
  37.  
  38.  
  39.  
  40. 'name'=> 'score'
  41.  
  42. 'type'=> 'numeric',  
  43.  
  44. ], 
  45.  
  46.  
  47. 'name'=> 'time'
  48.  
  49. 'type'=> 'date' 
  50.  
  51. ], 
  52.  
  53.  
  54. 'name'=> 'descr'
  55.  
  56. 'type'=> 'text'
  57.  
  58. ], 
  59.  
  60.  
  61. ]; 
  62.  
  63. // 实例化对象 
  64.  
  65. $Windnew\WindSearch\Index\Wind('test'); //test 当前索引库的名称 
  66.  
  67. //检查是否存在此索引库 
  68.  
  69. $is_index$Wind->checkIndex(); 
  70.  
  71. // 如果存在此索引库 
  72.  
  73. if($is_index) { 
  74.  
  75. //删除索引库 
  76.  
  77. $Wind->delIndex(); 
  78.  
  79.  
  80. //创建索引库 
  81.  
  82. $Wind->createIndex($mapping); 

导入数据:

  1. //实例化引擎 
  2.  
  3. $Windnew\WindSearch\Index\Wind('test'); 
  4.  
  5. // 初始化 
  6.  
  7. $Wind->buildIndexInit(); 
  8.  
  9. // 开启分词,导入数据时,加true可加快速度 
  10.  
  11. $Wind->loadAnalyzer(true); 
  12.  
  13. // 数据量小(内容少于一万条),则可以一次性全部导入 
  14.  
  15. // selectAll... 
  16.  
  17. // $result:一次性查询的所有内容 
  18.  
  19. foreach($resultas$v) { 
  20.  
  21. $Wind->indexer($v); 
  22.  
  23.  
  24. // 批量写入文件保存 
  25.  
  26. $Wind->batchWrite(); 

构建索引:

  1. // 数据导入结束后,接着可立即调用此方法构建索引 
  2.  
  3. // 注意,数据量大时,此步骤会比较耗时 
  4.  
  5. $Wind->buildIndex(); 

开始搜索:

  1. //实例化引擎 
  2.  
  3. $Windnew\WindSearch\Index\Wind('test'); 
  4.  
  5. //开启分词功能 
  6.  
  7. $Wind->loadAnalyzer(); 
  8.  
  9. //开始搜索 
  10.  
  11. // 搜索单个字段 
  12.  
  13. $query= [ 
  14.  
  15. 'match'=> [ 
  16.  
  17. 'field'=> [ 
  18.  
  19. 'name'=> 'title'
  20.  
  21. 'query'=> $text
  22.  
  23. ], 
  24.  
  25. 'list_rows'=> $listRows//每页多少条数据 
  26.  
  27. 'page'=> $page//第几页 
  28.  
  29.  
  30. ]; 
  31.  
  32. // 搜索接口 
  33.  
  34. $res$Wind->search($query$page$listRows); 
  35.  
  36. // 返回的最终结果,可直接渲染到前台页面 
  37.  
  38. $resArr$res['result']['_source'];

Tags: PHP站内搜索 WindSearch站内搜索

分享到: