動態

詳情 返回 返回

PHP實現站內搜索的開源利器——WindSearch - 動態 詳情

WindSearch是一個基於中文分詞,由純PHP開發全文檢索引擎,可快速搭建PHP站點的站內搜索,他沒有任何繁瑣的安裝配置、不需要維護調優、不佔用服務器內存、可與PHP項目完美融合在一起。

github地址:https://github.com/rock365/windsearch

必須極速安裝~

使用composer安裝:

composer require rock365/windsearch

或 使用Git安裝:

git clone git@github.com:rock365/windsearch.git

或 直接前往github: https://github.com/rock365/windsearch

還配置啥,立即開始用吧!

WindSearch包含即用模式、專業模式,即用模式適合簡單搜索場景,專業模式支持複雜搜索。

即用模式

“即用模式”可以立即導入數據,無任何配置,支持int主鍵、uuid主鍵,適合簡單的搜索場景。即用模式的各種api均有fast關鍵字。

“即用模式”的原理:對字符串進行ngram分詞,搜索的結果是主鍵集合,你可以使用這些集合從MySQL等數據庫查詢原始數據。

引入文件:

WindSearch安裝完成後,引入入口文件,注意具體文件路徑

require_once 'yourdirname/vendor/autoload.php';

導入數據

// 實例化對象
$Wind = new \WindSearch\Index\Wind('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();

開始搜索

// 開始搜索
$Wind = new \WindSearch\Index\Wind('test');
// 調用搜索方法
// $page 第幾頁 $listRows 每頁多少條
$res = $Wind->fastSearch($text,$page,$listRows)
// $res:返回的主鍵(比如id)集合,你可以使用id集合從MySQL等數據庫查詢原始數據

每個索引庫都可以使用即用模式導入數據,數據單獨存放,跟專業模式的數據不衝突,由於即用模式屬於某個索引庫的下屬模塊,所以刪除某個索引庫時,同樣會刪除即用模式的索引數據,所以一個索引庫名稱儘量只使用一種模式。

注意,即用模式的搜索效果可能比不上專業模式,可根據情況作出取捨。

專業模式

(專業的部分配合文檔使用更佳)

引入文件:

WindSearch安裝完成後,引入入口文件,注意具體文件路徑

require_once 'yourdirname/vendor/autoload.php';

建索引庫:

複製修改粘貼即可,跟mysql建表差不多

$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 \WindSearch\Index\Wind('test'); //test 當前索引庫的名稱
//檢查是否存在此索引庫
$is_index = $Wind->checkIndex();
// 如果存在此索引庫
if ($is_index) {
    //刪除索引庫
    $Wind->delIndex();
}
//創建索引庫
$Wind->createIndex($mapping);

導入數據:

//實例化引擎
$Wind = new \WindSearch\Index\Wind('test');
// 初始化
$Wind->buildIndexInit();
// 開啓分詞,導入數據時,加true可加快速度
$Wind->loadAnalyzer(true);

// 數據量小(內容少於一萬條),則可以一次性全部導入
// selectAll...
// $result:一次性查詢的所有內容
foreach ($result as $v) {
    $Wind->indexer($v);
}
// 批量寫入文件保存
$Wind->batchWrite();

構建索引:

// 數據導入結束後,接着可立即調用此方法構建索引
// 注意,數據量大時,此步驟會比較耗時
$Wind->buildIndex();

開始搜索:

//實例化引擎
$Wind = new \WindSearch\Index\Wind('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還有更深入、更豐富的搜索功能等你挖掘:

在線開發文檔:https://rock365.github.io/ 偶爾訪問不穩定,多刷新幾次即可

如果你覺得WindSearch還不錯,可以點個star~

user avatar laoduan 頭像 wujingquan 頭像 skyselang 頭像 yujiaao 頭像 kinra 頭像 invalidnull 頭像 xingzoudedahuoji 頭像 zjkal 頭像 sy_records 頭像 seth9shi 頭像 headofhouchang 頭像 buildyuan 頭像
點贊 18 用戶, 點贊了這篇動態!
點贊

Add a new 評論

Some HTML is okay.