zoukankan      html  css  js  c++  java
  • 利用redis完成自动补全搜索功能(二)

       前面介绍了自动完成的大致思路,现在把搜索次数的功能也结合上去。我采用的是hash表来做的,当然也可以在生成分词的时候,另外一个有序集合来维护排序, 然后2个有序集合取交集即可。这里介绍hash的方式来实现。

      产生分词 dist.php,此脚本可用linux的定时任务执行。

    <?php
    require './redis.php';
    
    //分词
    $words = ['花讯','nba','nba直播','nba赛事','nba季后赛','nba录像','花讯品牌','花讯女装','花','n'];
    
    //利用管道
    Cache::getInstance()->pipeline();
    foreach ($words as $val) {
    
        $len = mb_strlen($val,'utf-8');
        for ($i=0;$i<=$len;$i++){
    
            $key = mb_substr($val,0,$i+1,'utf-8');
            if ($i == $len) {
                $key .= '*';
                //初始化关键词搜索次数
                Cache::getInstance()->hSet("search:hits",$key,0);
            }
            //分词加入有序集合
            Cache::getInstance()->zAdd('search',0,$key);
        }
    
    }
    
    Cache::getInstance()->exec();

      自动提示 complete.php

    <?php
    require './redis.php';
    
    $key = isset($_GET['key']) ? $_GET['key'] : 'nb' ;
    
    //获取索引
    $index = Cache::getInstance()->zRank('search',$key);
    //获取以某字符串开头的集合 并格式化
    $res = Cache::getInstance()->zRange('search',$index,-1);
    if (!empty($res)) {
    
        $arr = [];
    
        foreach ($res as $val) {
            //strstr包含 (前缀匹配 开头即可)
            if (strpos($val, $key) === 0 && strrev($val)[0] == '*') {
                $arr[] = $val;
            }
        }
    
        //获取点击次数
        if ( $result = Cache::getInstance()->hMget("search:hits", $arr) ){
    //排序即可
            arsort($result);
            echo '<pre/>';
            print_r($result);
        }
    }

     客户端请求接口后,只需要把最后一位标识符(*)截取渲染即可。 

  • 相关阅读:
    训练深度学习网络时候,出现Nan 或者 震荡
    Jupyter Notebook 的快捷键
    pyspark RandomForestRegressor 随机森林回归
    深度学习图像标注工具VGG Image Annotator (VIA)使用教程
    python 中 with 用法
    python 报错 SyntaxError: Non-ASCII character
    YOLO 详解
    Spark与Pandas中DataFrame对比
    利用WGET下载文件,并保存到指定目录
    http 三次握手
  • 原文地址:https://www.cnblogs.com/loveyouyou616/p/5462645.html
Copyright © 2011-2022 走看看