使用SphinxAPI

  1. <form action="test_coreseek.php" method="post">  
  2.     <input type="text" value="" name='keyword' /><input type="submit" name='提交' />  
  3. </form>  
  4. <?php  
  5. header('Content-Type:text/html; charset=utf-8');   
  6. $keyword = $_REQUEST['keyword'];   
  7. if($keyword)   
  8. {   
  9.     require ( "sphinxapi.php" );   
  10.   
  11.     $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;   
  12.     $pagesize = 10;   
  13.     $start = ($page - 1) * $pagesize;    
  14.     $cl = new SphinxClient ();   
  15.     $cl->SetServer ( '127.0.0.1', 9312);   
  16.     $cl->SetConnectTimeout ( 3 );   
  17.   
  18.     $cl->SetArrayResult ( true );   
  19.     $cl->SetMatchMode ( SPH_MATCH_ANY);   
  20.     // 设置分页   
  21.     $cl->SetLimits($start, $pagesize, 1000);   
  22.     $res = $cl->Query ( $keyword , "*" );   
  23.     //dump($cl);   
  24.     //dump($res);   
  25.     $b = array_reduce($res['matches'], "rsum");   
  26.     $mysqli = new mysqli("localhost","root","root","test");    
  27.     $idsjoin(',',$b); //要把需要的id取出来。    
  28.     $sql = "SELECT * FROM documents WHERE id=$id";    
  29.     $result = $mysqli->query($sql);    
  30.     $data = array();   
  31.     $index = "mysql";   
  32.     $opts = array  
  33.     (   
  34.         "before_match"      => "<b style='color:red'>",   
  35.         "after_match"       => "</b>",   
  36.         "chunk_separator"   => " ... ",   
  37.     );   
  38.     while($row=$result->fetch_assoc()){   
  39.         $res = $cl->BuildExcerpts ( $row, $index, $keyword, $opts );   
  40.         echo "<font size=4>".$res[0]."</font></a></br>"; //标题    
  41.         echo "<font size=2>".$res[1]."</font></br>";  //摘要    
  42.         echo $res[2]."</p><hr>";  //添加时间   
  43.     }   
  44. }   
  45. // 将数组中对应的id值添加到新数组   
  46. function rsum($v = array(), $w)   
  47. {   
  48.     $v[] = $w['id'];   
  49.     return $v;   
  50. }   
  51.   
  52. function dump($var, $echo=true, $label=null, $strict=true) {   
  53.     $label = ($label === null) ? '' : rtrim($label) . ' ';   
  54.     if (!$strict) {   
  55.         if (ini_get('html_errors')) {   
  56.             $output = print_r($var, true);   
  57.             $output = "<pre>" . $label . htmlspecialchars($output, ENT_QUOTES) . "</pre>";   
  58.         } else {   
  59.             $output = $label . print_r($var, true);   
  60.         }   
  61.     } else {   
  62.         ob_start();   
  63.         var_dump($var);   
  64.         $output = ob_get_clean();   
  65.         if (!extension_loaded('xdebug')) {   
  66.             $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);   
  67.             $output = '<pre>' . $label . htmlspecialchars($output, ENT_QUOTES) . '</pre>';   
  68.         }   
  69.     }   
  70.     if ($echo) {   
  71.         echo($output);   
  72.         return null;   
  73.     }else   
  74.         return $output;   
  75. }   
  76. ?>  

posted on 2013-02-03 21:47  cnjack  阅读(250)  评论(0编辑  收藏  举报

导航