当前位置:首页 > 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. $Wind= new\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.  
  27. $Wind->fastBatchWrite(); 
  28.  
  29. // 所有数据全部导入完成后,接着构建索引(不一定非得紧接着调用,也可以在其它地方单独调用) 
  30.  
  31. $Wind->fastBuildIndex(); 

开始搜索

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

导入数据:

  1. //实例化引擎 
  2.  
  3. $Wind= new\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.  
  27. $Wind->batchWrite(); 

构建索引:

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

开始搜索:

  1. //实例化引擎 
  2.  
  3. $Wind= new\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.  
  35. $res= $Wind->search($query, $page, $listRows); 
  36.  
  37. // 返回的最终结果,可直接渲染到前台页面 
  38.  
  39. $resArr= $res['result']['_source'];

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

分享到: