PHP Redis implementation of Ultra Mini full Text Search code example details

Source: Internet
Author: User
Tags php redis
This article mainly for you in detail the PHP Redis implementation of ultra-mini full-text retrieval of relevant data, with a certain reference value, interested in small partners can refer to

Scenario: Our platform has a lot of games, the operation of colleagues in the query of a game, the current use of the HTML Select drop-down list of the presentation form, the operation of the colleagues have to find one by one, and then select, time-consuming and cost-eye

Effect: Enter "three countries" or "country three", will automatically list all the games containing the "Three Kingdoms" name, enter an unlimited order; For example, the "Three Kingdoms" will still be found in the game "kill"

implementation: I use the Redis collection +php array_intersect () and MB series functions to achieve a super mini full-Text search function

principle: (Avenue But two or three words, is not worth a penny, haha)

1, read all the game names, split into a single character

2, these Chinese characters as a Redis collection of keys, write to Redis, each set of values is all those games in the name of the game containing this character ID

3, when the user input text through the Ajax asynchronous request, the user input to PHP

4, the input text is split into a single Chinese character, respectively, find these Chinese characters in the Redis collection value

5, take out, seek the intersection, found the same time contains these several Chinese characters of the game ID

6, finally to the database to find the corresponding game information can be

Cons: deleting data is inconvenient

PHP writes to Redis and retrieves the code:

Automatic completion//unlimited input of Chinese characters in the order: input "country three kill" and output "three kills" function Getautocomplate () {//$word = $this->input->post (' word ')    ;    $word = ' Three Kingdoms ';    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);    Because the number of arguments passed in is indeterminate and therefore cannot be directly intersected}} $arrGame = $this->gamemodel->getgamenameforautocomplate ($arrGid);    Var_dump ($arrGame); exit;    $jsonGame = Json_encode ($arrGame);  Exit ($jsonGame);    }//Auto-complete, set index 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); }    }      }

Ways to operate Redis

Auto complement function public  function setautocomplate ($key, $value)  {    $youxikey = ' youxi_ '. $key;    $this->sadd ($youxikey, $value);  }  Auto complement function public  function getautocomplate ($key)  {    $youxikey = ' youxi_ '. $key;    return $this->smembers ($youxikey);  }

The above is the PHP Redis implementation of ultra-mini Full Text Search code example of the details of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.