PHP?WindSearch實現(xiàn)站內(nèi)搜索功能
WindSearch是一個基于中文分詞,由純PHP開發(fā)全文檢索引擎,可快速搭建PHP站點(diǎn)的站內(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)用,也可以在其它地方單獨(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ù)庫查詢原始數(shù)據(jù)
每個索引庫都可以使用即用模式導(dǎo)入數(shù)據(jù),數(shù)據(jù)單獨(dú)存放,跟專業(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實現(xiàn)的支付寶網(wǎng)頁支付功能示例【基于TP5框架】
這篇文章主要介紹了php實現(xiàn)的支付寶網(wǎng)頁支付功能,結(jié)合實例形式分析了基于TP5框架框架的支付寶網(wǎng)頁支付功能具體操作步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2019-09-09
PHP中將ip地址轉(zhuǎn)成十進(jìn)制數(shù)的兩種實用方法
現(xiàn)在PHP中有很多時候都會用到ip地址,但是這個ip地址獲取的時候都不是10進(jìn)制的。那么PHP中如何將ip地址轉(zhuǎn)成十進(jìn)制數(shù),下面為大家介紹下兩種方法可以輕松實現(xiàn)2013-08-08
Laravel 5 框架入門(二)構(gòu)建 Pages 的管理功能
這篇文章主要介紹了Laravel 5 框架入門的第二篇文章,給大家講解的是構(gòu)建 Pages 的管理功能,十分的詳細(xì),有需要的小伙伴可以參考下。2015-04-04
Laravel5中實現(xiàn)模糊匹配加多條件查詢功能的方法
這篇文章主要介紹了Laravel5中實現(xiàn)模糊匹配加多條件查詢功能的方法,結(jié)合實例形式分析了Laravel5多條件模糊查詢及相關(guān)封裝操作技巧,需要的朋友可以參考下2018-03-03

