PHP?WindSearch實(shí)現(xiàn)站內(nèi)搜索功能
WindSearch是一個(gè)基于中文分詞,由純PHP開發(fā)全文檢索引擎,可快速搭建PHP站點(diǎn)的站內(nèi)搜索,他沒有任何繁瑣的安裝配置、不需要維護(hù)調(diào)優(yōu)、不占用服務(wù)器內(nèi)存、可與PHP項(xiàng)目完美融合在一起。
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è)模式,即用模式適合簡(jiǎn)單搜索場(chǎng)景,專業(yè)模式支持復(fù)雜搜索。
即用模式
“即用模式”可以立即導(dǎo)入數(shù)據(jù),無任何配置,支持int主鍵、uuid主鍵,適合簡(jiǎn)單的搜索場(chǎng)景。即用模式的各種api均有fast
關(guān)鍵字。
“即用模式”的原理:對(duì)字符串進(jìn)行ngram分詞,搜索的結(jié)果是主鍵集合,你可以使用這些集合從MySQL等數(shù)據(jù)庫查詢?cè)紨?shù)據(jù)。
引入文件:
WindSearch安裝完成后,引入入口文件,注意具體文件路徑
require_once 'yourdirname/vendor/autoload.php';
導(dǎo)入數(shù)據(jù)
// 實(shí)例化對(duì)象 $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)用,也可以在其它地方單獨(dú)調(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ù)庫查詢?cè)紨?shù)據(jù)
每個(gè)索引庫都可以使用即用模式導(dǎo)入數(shù)據(jù),數(shù)據(jù)單獨(dú)存放,跟專業(yè)模式的數(shù)據(jù)不沖突,由于即用模式屬于某個(gè)索引庫的下屬模塊,所以刪除某個(gè)索引庫時(shí),同樣會(huì)刪除即用模式的索引數(shù)據(jù),所以一個(gè)索引庫名稱盡量只使用一種模式。
注意,即用模式的搜索效果可能比不上專業(yè)模式,可根據(jù)情況作出取舍。
專業(yè)模式
(專業(yè)的部分配合文檔使用更佳)
引入文件:
WindSearch安裝完成后,引入入口文件,注意具體文件路徑
require_once 'yourdirname/vendor/autoload.php';
建索引庫:
復(fù)制修改粘貼即可,跟mysql建表差不多
$mapping = [ //設(shè)置索引庫的名稱,比如對(duì)應(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', ], ] ]; // 實(shí)例化對(duì)象 $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ù):
//實(shí)例化引擎 $Wind = new \WindSearch\Index\Wind('test'); // 初始化 $Wind->buildIndexInit(); // 開啟分詞,導(dǎo)入數(shù)據(jù)時(shí),加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ù)量大時(shí),此步驟會(huì)比較耗時(shí) $Wind->buildIndex();
開始搜索:
//實(shí)例化引擎 $Wind = new \WindSearch\Index\Wind('test'); //開啟分詞功能 $Wind->loadAnalyzer(); //開始搜索 // 搜索單個(gè)字段 $query = [ 'match' => [ 'field' => [ 'name' => 'title', 'query' => $text, ], 'list_rows' => $listRows, //每頁多少條數(shù)據(jù) 'page' => $page, //第幾頁 ] ]; // 搜索接口 $res = $Wind->search($query, $page, $listRows); // 返回的最終結(jié)果,可直接渲染到前臺(tái)頁面 $resArr = $res['result']['_source'];
以上就是PHP WindSearch實(shí)現(xiàn)站內(nèi)搜索功能的詳細(xì)內(nèi)容,更多關(guān)于PHP WindSearch站內(nèi)搜索的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
php 創(chuàng)建以UNIX時(shí)間戳命名的文件夾(示例代碼)
這篇文章主要介紹了php創(chuàng)建以UNIX時(shí)間戳命名的文件夾(示例代碼)。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-03-03php獲取指定日期之間的各個(gè)周和月的起止時(shí)間
本文推薦給大家一個(gè)php類文件,可以根據(jù)指定日期獲取所在周的起始時(shí)間和結(jié)束時(shí)間,并附上使用實(shí)例,有需要的小伙伴參考下吧2014-11-11php 網(wǎng)上商城促銷設(shè)計(jì)實(shí)例代碼
前一篇文章講的是購物車的設(shè)計(jì),這篇來講下關(guān)于促銷的。支持的促銷類似于,打折,買幾贈(zèng)幾,滿多少錢送禮品等等。用到策略模式2012-02-02php使用phpoffice/phpspreadsheet導(dǎo)出圖片實(shí)例
這篇文章主要為大家介紹了php使用phpoffice/phpspreadsheet導(dǎo)出圖片實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Zend Framework動(dòng)作助手Url用法詳解
這篇文章主要介紹了Zend Framework動(dòng)作助手Url用法,結(jié)合實(shí)例形式分析了動(dòng)作助手Url的功能,定義與相關(guān)使用技巧,需要的朋友可以參考下2016-03-03基于PHP實(shí)現(xiàn)短信驗(yàn)證碼接口(容聯(lián)運(yùn)通訊)
本文分步驟給大家講解了短信驗(yàn)證碼接口(容聯(lián)運(yùn)通訊)實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看看吧2016-09-09