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均有fast
关键字。
“即用模式”的原理:对字符串进行ngram分词,搜索的结果是主键集合,你可以使用这些集合从MySQL等数据库查询原始数据。
引入文件:
WindSearch安装完成后,引入入口文件,注意具体文件路径
1 | require_once 'yourdirname/vendor/autoload.php' ; |
导入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | // 实例化对象 $Wind = new WindSearchIndexWind( '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(); |
开始搜索
1 2 3 4 5 6 | // 开始搜索 $Wind = new WindSearchIndexWind( 'test' ); // 调用搜索方法 // $page 第几页 $listRows 每页多少条 $res = $Wind ->fastSearch( $text , $page , $listRows ) // $res:返回的主键(比如id)集合,你可以使用id集合从MySQL等数据库查询原始数据 |
每个索引库都可以使用即用模式导入数据,数据单独存放,跟专业模式的数据不冲突,由于即用模式属于某个索引库的下属模块,所以删除某个索引库时,同样会删除即用模式的索引数据,所以一个索引库名称尽量只使用一种模式。
注意,即用模式的搜索效果可能比不上专业模式,可根据情况作出取舍。
专业模式
(专业的部分配合文档使用更佳)
引入文件:
WindSearch安装完成后,引入入口文件,注意具体文件路径
1 | require_once 'yourdirname/vendor/autoload.php' ; |
建索引库:
复制修改粘贴即可,跟mysql建表差不多
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | $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 WindSearchIndexWind( 'test' ); //test 当前索引库的名称 //检查是否存在此索引库 $is_index = $Wind ->checkIndex(); // 如果存在此索引库 if ( $is_index ) { //删除索引库 $Wind ->delIndex(); } //创建索引库 $Wind ->createIndex( $mapping ); |
导入数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //实例化引擎 $Wind = new WindSearchIndexWind( 'test' ); // 初始化 $Wind ->buildIndexInit(); // 开启分词,导入数据时,加true可加快速度 $Wind ->loadAnalyzer(true); // 数据量小(内容少于一万条),则可以一次性全部导入 // selectAll... // $result:一次性查询的所有内容 foreach ( $result as $v ) { $Wind ->indexer( $v ); } // 批量写入文件保存 $Wind ->batchWrite(); |
构建索引:
1 2 3 | // 数据导入结束后,接着可立即调用此方法构建索引 // 注意,数据量大时,此步骤会比较耗时 $Wind ->buildIndex(); |
开始搜索:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //实例化引擎 $Wind = new WindSearchIndexWind( '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站内搜索的资料请关注IT俱乐部其它相关文章!