search
HomeBackend DevelopmentPHP TutorialDetailed introduction to the code example of super mini full-text retrieval using PHP redis

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)