PHP?WindSearch實現(xiàn)站內(nèi)搜索功能
WindSearch是一個基于中文分詞,由純PHP開發(fā)全文檢索引擎,可快速搭建PHP站點的站內(nèi)搜索,他沒有任何繁瑣的安裝配置、不需要維護(hù)調(diào)優(yōu)、不占用服務(wù)器內(nèi)存、可與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包含即用模式、專業(yè)模式,即用模式適合簡單搜索場景,專業(yè)模式支持復(fù)雜搜索。
即用模式
“即用模式”可以立即導(dǎo)入數(shù)據(jù),無任何配置,支持int主鍵、uuid主鍵,適合簡單的搜索場景。即用模式的各種api均有fast
關(guān)鍵字。
“即用模式”的原理:對字符串進(jìn)行ngram分詞,搜索的結(jié)果是主鍵集合,你可以使用這些集合從MySQL等數(shù)據(jù)庫查詢原始數(shù)據(jù)。
引入文件:
WindSearch安裝完成后,引入入口文件,注意具體文件路徑
require_once 'yourdirname/vendor/autoload.php';
導(dǎo)入數(shù)據(jù)
// 實例化對象 $Wind = new \WindSearch\Index\Wind('test'); //test 當(dāng)前索引庫的名稱 // 清空之前的數(shù)據(jù)(如果之前使用即用模式導(dǎo)入過數(shù)據(jù)) $Wind->deleteFastIndex(); // 批次導(dǎo)入數(shù)據(jù) // $res 是從數(shù)據(jù)庫查詢的數(shù)據(jù) foreach($res as $v){ $text = $v['title']; $primarykey = $v['id']; // $text是需要搜索的具體內(nèi)容,比如title;$primarykey是主鍵值,比如id的值 $Wind->fastIndexer($text, $primarykey); } //每導(dǎo)入一批數(shù)據(jù),就調(diào)用此方法進(jìn)行保存 $Wind->fastBatchWrite(); // 所有數(shù)據(jù)全部導(dǎo)入完成后,接著構(gòu)建索引(不一定非得緊接著調(diào)用,也可以在其它地方單獨調(diào)用) $Wind->fastBuildIndex();
開始搜索
// 開始搜索 $Wind = new \WindSearch\Index\Wind('test'); // 調(diào)用搜索方法 // $page 第幾頁 $listRows 每頁多少條 $res = $Wind->fastSearch($text,$page,$listRows) // $res:返回的主鍵(比如id)集合,你可以使用id集合從MySQL等數(shù)據(jù)庫查詢原始數(shù)據(jù)
每個索引庫都可以使用即用模式導(dǎo)入數(shù)據(jù),數(shù)據(jù)單獨存放,跟專業(yè)模式的數(shù)據(jù)不沖突,由于即用模式屬于某個索引庫的下屬模塊,所以刪除某個索引庫時,同樣會刪除即用模式的索引數(shù)據(jù),所以一個索引庫名稱盡量只使用一種模式。
注意,即用模式的搜索效果可能比不上專業(yè)模式,可根據(jù)情況作出取舍。
專業(yè)模式
(專業(yè)的部分配合文檔使用更佳)
引入文件:
WindSearch安裝完成后,引入入口文件,注意具體文件路徑
require_once 'yourdirname/vendor/autoload.php';
建索引庫:
復(fù)制修改粘貼即可,跟mysql建表差不多
$mapping = [ //設(shè)置索引庫的名稱,比如對應(yīng)的表名 'name' => 'test', // 字段配置 'field' => [ [ 'name' => 'id',// 主鍵名稱 主鍵必須設(shè)置 'type' => 'primarykey', //數(shù)據(jù)類型為主鍵 必須設(shè)置 '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'); //test 當(dāng)前索引庫的名稱 //檢查是否存在此索引庫 $is_index = $Wind->checkIndex(); // 如果存在此索引庫 if ($is_index) { //刪除索引庫 $Wind->delIndex(); } //創(chuàng)建索引庫 $Wind->createIndex($mapping);
導(dǎo)入數(shù)據(jù):
//實例化引擎 $Wind = new \WindSearch\Index\Wind('test'); // 初始化 $Wind->buildIndexInit(); // 開啟分詞,導(dǎo)入數(shù)據(jù)時,加true可加快速度 $Wind->loadAnalyzer(true); // 數(shù)據(jù)量?。▋?nèi)容少于一萬條),則可以一次性全部導(dǎo)入 // selectAll... // $result:一次性查詢的所有內(nèi)容 foreach ($result as $v) { $Wind->indexer($v); } // 批量寫入文件保存 $Wind->batchWrite();
構(gòu)建索引:
// 數(shù)據(jù)導(dǎo)入結(jié)束后,接著可立即調(diào)用此方法構(gòu)建索引 // 注意,數(shù)據(jù)量大時,此步驟會比較耗時 $Wind->buildIndex();
開始搜索:
//實例化引擎 $Wind = new \WindSearch\Index\Wind('test'); //開啟分詞功能 $Wind->loadAnalyzer(); //開始搜索 // 搜索單個字段 $query = [ 'match' => [ 'field' => [ 'name' => 'title', 'query' => $text, ], 'list_rows' => $listRows, //每頁多少條數(shù)據(jù) 'page' => $page, //第幾頁 ] ]; // 搜索接口 $res = $Wind->search($query, $page, $listRows); // 返回的最終結(jié)果,可直接渲染到前臺頁面 $resArr = $res['result']['_source'];
以上就是PHP WindSearch實現(xiàn)站內(nèi)搜索功能的詳細(xì)內(nèi)容,更多關(guān)于PHP WindSearch站內(nèi)搜索的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php 創(chuàng)建以UNIX時間戳命名的文件夾(示例代碼)
這篇文章主要介紹了php創(chuàng)建以UNIX時間戳命名的文件夾(示例代碼)。需要的朋友可以過來參考下,希望對大家有所幫助2014-03-03php使用phpoffice/phpspreadsheet導(dǎo)出圖片實例
這篇文章主要為大家介紹了php使用phpoffice/phpspreadsheet導(dǎo)出圖片實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11基于PHP實現(xiàn)短信驗證碼接口(容聯(lián)運通訊)
本文分步驟給大家講解了短信驗證碼接口(容聯(lián)運通訊)實現(xiàn)代碼,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-09-09