Home  >  Article  >  Backend Development  >  Detailed introduction to the code example of super mini full-text retrieval using PHP redis

Detailed introduction to the code example of super mini full-text retrieval using PHP redis

黄舟
黄舟Original
2017-03-06 09:25:451420browse

This article mainly introduces the relevant information of PHP redis to achieve super mini full-text retrieval in detail. It has certain reference value. Interested friends can refer to

Scenario: There are many games on our platform. When the operations colleagues query a certain game, they currently use the html select drop-down list display format. The operations colleagues have to look for it one by one and then select it, which is time-consuming and eye-catching.

Effect: Enter "Three Kingdoms" or "国三", all game names containing "Three Kingdoms" will be automatically listed, and the input order is not limited; for example, if you enter "Kill Three Kingdoms", the result will still be I will find out the game "Three Kingdoms"

Implementation: I use the redis collection + PHP's array_intersect() and mb series functions to implement a super mini full-text search function

Principle: (The road is only two or three words long, it’s not worth a penny to tell the truth, haha)

1. Read out all the game names and split them into individual Chinese characters

2. Use these Chinese characters as the keys of the redis collection and write them into redis. The value in each collection is the ID of all games whose names contain this Chinese character.

3. When the user enters When writing text, pass the user input to PHP through ajax asynchronous request

4, split the input text into individual Chinese characters, and find the set values ​​of these Chinese characters in redis

5, Take it out, find the intersection, and find the ID of the game that contains these Chinese characters at the same time

6. Finally, go to the database to find the corresponding game information

Disadvantages: Deleting data is inconvenient

PHP code for writing redis and retrieval:

//自动补全
  //不限输入汉字的前后顺序: 输入"国三杀" => 输出 "三国杀"
  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, &#39;UTF-8&#39;);
        $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, &#39;UTF-8&#39;);
      for ($i=0; $i < $intGnameLength; $i++) {
        $strOne = mb_substr($gname, $i, 1, &#39;UTF-8&#39;);
        $arrIndex[$strOne][] = $gid;
      }
    }
    
    $this->load->library(&#39;iredis&#39;);
    foreach ($arrIndex as $word => $arrGid) {
      foreach ($arrGid as $gid) {
        $this->iredis->setAutoComplate($word, $gid);
      }
    }
    
  }

Methods to operate redis

//自动补全功能
  public function setAutoComplate($key, $value)
  {
    $youxikey = &#39;youxi_&#39;.$key;
    $this->sAdd($youxikey, $value);
  }

  //自动补全功能
  public function getAutoComplate($key)
  {
    $youxikey = &#39;youxi_&#39;.$key;
    return $this->sMembers($youxikey);
  }

The above is the detailed introduction of PHP redis implementation of super mini full-text retrieval code example. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn