zoukankan      html  css  js  c++  java
  • PHP操作Sphinx实例。

      

    <?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 的区别:
    //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>";
    }
    
    ?>
  • 相关阅读:
    Eclipse 的单步调试
    CALayer快速入门
    UITableView快速入门
    iOS程序启动原理
    iOS触摸事件
    UITableViewCell重用和性能优化
    Autolayout
    iOS适配
    NSTimer
    UIScrollView
  • 原文地址:https://www.cnblogs.com/lgqtecng/p/6415819.html
Copyright © 2011-2022 走看看