zoukankan      html  css  js  c++  java
  • 【PHP操作sphinx】

    【index.php】

    【find.php】

    <?php
    header("Content-type:text/html;charset=utf-8");
    
    $keyword = $_GET['word'];
    //实例化Sphinx对象
    $sphinx=new SphinxClient();
    //连接sphinx服务器
    $sphinx->SetServer("localhost",9312);
    //拆词
    //SPH_MATCH_ALL 和 SPH_MATCH_ANY 的区别:
    //搜索“LAMP兄弟连”,ALL的结果:完整包含“LAMP兄弟连”才能被搜出来,
    //单纯包含“LAMP”或单纯包含“兄弟连”的搜索不出来,没有拆词的功能。
    //ANY则可以搜索出来拆开后的词的结果。此处使用ANY
    $sphinx->SetMatchMode(SPH_MATCH_ANY);
    //通过query方法搜索,“*”表示在所有的索引中搜索,相当于命令行里面的“./indexer --all”
    $result=$sphinx->query("$keyword","*");
    //打印搜索的结果
    //echo "<pre>";
    //print_r($result);
    //echo "</pre>";
    
    //上面打印的结果中,数组的 [matches]循环便利,下标就是搜索到的文档的主键Id
    //使用PHP中的 array_keys()函数即可拿到下标,即:要查找的文档的主键
    //print_r(array_keys($result['matches']));
    //结果如下:Array([0]=>1)
    
    //使用implode或者 join用逗号把查询出来的主键连接起来:
    $ids = join(',',array_keys($result['matches']));
    //echo $ids; //6,7
    
    /*连接数据库的操作*/
    $p1 = mysql_connect("localhost","root","123456");
    mysql_select_db("test");
    mysql_query("set names utf8");
    $sql="select * from post where id in ($ids)";
    $rst=mysql_query($sql);
    $opts=array(
       "before_match"=>"<font color='red'>",
       "after_match"=>"</font>",
    );
    while($row=mysql_fetch_assoc($rst)){
        //print_r($row); 
    
        //下面是高亮显示所需,具体可以查手册    
        $final=$sphinx->buildExcerpts($row,"main",$keyword,$opts);
        echo "<pre>";
        print_r($final);
        echo "</pre>";
    }
    
    ?>

    【当前数据内容】

  • 相关阅读:
    【洛谷P4708】—画画(Burnside引理)
    【HDU 6087】—Rikka with Sequence(可持久化平衡树)
    【HDU 6087】—Rikka with Sequence(可持久化平衡树)
    【Codeforces 643G】—Choosing Ads(线段树)
    【Codeforces 643G】—Choosing Ads(线段树)
    less sass学习总结(——待续哦——)
    网站常识
    网站发布流程
    解惑 和 遇到的问题
    页面无法访问 css文件加载问题
  • 原文地址:https://www.cnblogs.com/rxbook/p/6003593.html
Copyright © 2011-2022 走看看