|
废话不多话,先上图
测试代码
PHP复制代码
<?php if( ! defined('BASEPATH')) die('No Access');
class Search_page extends CI_Controller {
public function __construct (){
parent ::__construct ();
}
public function search (){
$this->load->helper('url');
$this->load->view('search');
}
public function result (){
header('content-type: text/html;charset=utf-8');
$words = $this->input->get('words');
if($words===NULL) $words = '';
$this->load->library('sphinx_client', NULL, 'sphinx');
$index = "test1";
$opts = array
(
"before_match" => '<span style="color:red;">',
"after_match" => "</span>",
"chunk_separator" => " ... ",
"limit" => 60,
"around" => 3,
);
$this->sphinx->SetServer('192.168.23.128',9312);
$this->sphinx->SetConnectTimeout(3);
$this->sphinx->SetArrayResult(TRUE);
$this->sphinx->SetMatchMode(SPH_MATCH_ANY );
$this->sphinx->SetLimits(0,20);
$res = $this->sphinx->Query($words, 'test1');
if($res===FALSE){
var_dump($this->sphinx->GetLastError());
exit;
}
echo "关键词 <b>{$words}</b> ,找到约 <b>{$res['total_found']}</b> 结果,用时 <b>{$res['time']}</b>s";
echo '<br/><hr/><br/>';
if(array_key_exists('words', $res) && is_array($res['words'])){
foreach($res['words'] as $k => $v){
echo $k . ' : ' . $v['docs'] . ' - ' . $v['hits'] . '<br/>';
}
}
echo '<br/><hr/><br/>';
$this->load->database();
$idarr = array();
if(array_key_exists('matches', $res) && is_array($res['matches'])){
foreach($res['matches'] as $v){
$idarr[] = $v['id'];
}
}
if(count($idarr)>0){
$this->db->from('shop_goods_info');
$this->db->select('pname,cretime');
$this->db->where_in('id', $idarr);
$result = $this->db->get()->result_array();
echo '<ul>';
$name_arr = array();
foreach($result as $k=>$v){
$name_arr[$k] = $v['pname'];
}
$name_arr = $this->sphinx->BuildExcerpts($name_arr, $index, $words, $opts);
foreach($result as $k=>$v){
echo '<li>' . $name_arr[$k] . '(' . date('Y-m-d H:i:s', $v['cretime']) . ')</li>';
}
echo '</ul>';
}
$this->sphinx->Close();
}
}
?>
复制代码
更多请参阅我的个人博客http://www.du52.com/text.php?id=552
|
|