《PHP教程:PHP redis實現超迷你全文檢索》要點:
本文介紹了PHP教程:PHP redis實現超迷你全文檢索,希望對您有用。如果有疑問,可以聯系我們。
PHP實戰情景: 我們平臺有好多游戲, 運營的同事在查詢某一款游戲的時候, 目前使用的是html的select下拉列表的展現形式, 運營的同事得一個個去找,然后選中,耗時又費眼
PHP實戰效果: 輸入"三國"或者"國三", 將自動列出所有包含"三國"的游戲名字, 輸入不限順序; 例如輸入"殺三國",仍然會將"三國殺"這款游戲找出來
PHP實戰實現: 我用redis的集合+PHP的array_intersect()和mb系列函數, 實現了一個超迷你的全文檢索功能
PHP實戰原理: (大道不過兩三言,說穿不值一文錢,哈哈)
PHP實戰1、將所有的游戲名字讀出來,拆分成單個漢字
PHP實戰2、 將這些漢字作為redis集合的鍵,寫入redis,每個集合里的值是所有那些游戲名字中包含此漢字的游戲的id
PHP實戰3、當用戶輸入文字的時候通過ajax異步請求,將用戶輸入傳給PHP
PHP實戰4、將輸入的文字拆分成單個漢字, 分別找到這些漢字在redis中的集合值
PHP實戰5、取出來,求交集,就找到了同時包含這幾個漢字的游戲的id
PHP實戰6、最后到數據庫里查出來相應的游戲信息即可
PHP實戰缺點: 刪除數據不方便
PHP實戰PHP寫入redis和檢索的代碼:
PHP實戰
//自動補全
//不限輸入漢字的前后順序: 輸入"國三殺" => 輸出 "三國殺"
function getAutoComplate()
{
//$word = $this->input->post('word');
$word = '三國';
if (empty($word)) {
exit('0');
}
$intWordLength = mb_strlen($word, 'UTF-8');
$this->load->library('iredis');
if (1 == $intWordLength) {
$arrGid = $this->iredis->getAutoComplate($word);
} else {
$arrGid = array();
for ($i=0; $i < $intWordLength; $i++) {
$strOne = mb_substr($word, $i, 1, 'UTF-8');
$arrGidTmp = $this->iredis->getAutoComplate($strOne);
$arrGid = empty($arrGid) ? $arrGidTmp : array_intersect($arrGid, $arrGidTmp); //求交集,因為傳入的參數個數不確定,因此不能直接求交集
}
}
$arrGame = $this->gamemodel->getGameNameForAutoComplate($arrGid);
// var_dump($arrGame);exit;
$jsonGame = json_encode($arrGame);
exit($jsonGame);
}
//自動補全, 建立索引
function setAutoComplate()
{
$arrGame = $this->gamemodel->getAllGameNameForAutoComplate();
$arrIndex = array();
foreach ($arrGame as $gid => $gname) {
$intGnameLength = mb_strlen($gname, 'UTF-8');
for ($i=0; $i < $intGnameLength; $i++) {
$strOne = mb_substr($gname, $i, 1, 'UTF-8');
$arrIndex[$strOne][] = $gid;
}
}
$this->load->library('iredis');
foreach ($arrIndex as $word => $arrGid) {
foreach ($arrGid as $gid) {
$this->iredis->setAutoComplate($word, $gid);
}
}
}
PHP實戰操作redis的方法
PHP實戰
//自動補全功能
public function setAutoComplate($key, $value)
{
$youxikey = 'youxi_'.$key;
$this->sAdd($youxikey, $value);
}
//自動補全功能
public function getAutoComplate($key)
{
$youxikey = 'youxi_'.$key;
return $this->sMembers($youxikey);
}
PHP實戰以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持維易PHP.