Sphnix的PHP客户端Demo

前段时间做JobUI(www.jobui.com )站点的优化,对帖子搜索部分用上了全文搜索,使用的是sphnix的php客户端,现将一个测试demo贴出来给大家拍砖。

服务端的sphnix配置会在将来贴出来,服务端是与mysql整合的,稍微复杂。

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
td{
	padding:3px;
}
tr{
	margin-bottom:5px;
	border-bottom:1px solid #000;
}
tr:hover{
	background-color:gray;
	color:white;
}
tr:hover a{
	color:white;
}
</style>
</head>
<?php
 $keyword = (empty($_POST['keyword'])?$_GET['keyword']:$_POST['keyword']);
	$keyword = urldecode($keyword); 
 ?>
<div style="clear:both;width:700px;margin-top:20px;margin-left:auto;margin-right:auto;text-align:center;">
	<form name="searchForm" action="sphinx.php" method="post" >
		<input type="text" name="keyword" value="<?php echo $keyword;?>" /><input type="submit" name="submit" value="搜索"/>
	</form>
</div>
<div style="clear:both;width:800px;margin-top:20px;margin-left:auto;margin-right:auto;text-align:center;">
<?php
if(empty($keyword)){
	die('请输入关键字查询!');
}
$nowPage =(empty($_GET['nowPage'])?$_POST['nowPage']:$_GET['nowPage']);
empty($nowPage)?$nowPage=0:''; 
$perPage = 20;
$searchIndex = 'postIndex';

$s = new SphinxClient(); 
$s->setServer("192.168.1.200", 3312); 				//连接
$s->setMatchMode(SPH_MATCH_EXTENDED);					//设置匹配模式
$s->setMaxQueryTime(30);							//最大查询时间,单位毫秒,默认为0,即不限制
$s->setArrayResult(false); 							//设置返回格式:false(default) 以PHP Hash模式返回,true 以普通数组格式返回,区别:普通数组格式时$result['matches']字段每一项从0开始,每一项由一个id属性,标示文档,而PHP Hash数组每一项的下标是文档ID
$s->setLimits($nowPage * $perPage,$perPage,1000);	//给服务器端结果集设置一个偏移量(第一个参数)和从那个偏移量起向客户端返回的匹配项数目限制(第二参数)
//$s->setRankingMode(SPH_RANK_PROXIMITY_BM25);		//需要SPH_MATCH_EXTENDED2匹配模式支持
$s->setSortMode(SPH_SORT_RELEVANCE);				//排序模式
//$s->setFieldWeights(array('P_ID'=>2));			//设置字段权重
//$s->setIDRange(500,1000);							//筛选文档ID范围500-1000
//$s->setGroupBy('P_Title',SPH_GROUPBY_ATTR,'@group desc');			//按p_title属性降序排列
//以p_lastreplyuserarea聚合,统计不重复的p_title的数量,并降序
//$s->setGroupBy('p_lastreplyuserarea',SPH_GROUPBY_ATTR,'@count desc');
//$s->setGroupDistinct('p_title');

//$s->setFilter('P_LastReplyUserArea',array(387));	//添加整数筛选器
//$s->setGeoAnchor();
$s->setSortMode(SPH_SORT_ATTR_DESC);				
$result = $s->query($keyword,$searchIndex) or die($s->getLastError()."<br>".$s->getLastWarning());			//查询,第一个参数是查询的字符串,第二个是索引,如果索引参数不传,则会搜索所有索引
//$result = $s->addQuery('中国','postIndex') or die($s->getLastError()."<br>".$s->getLastWarning());
//$result = $s->runQueries();
//print_r($result);
if(empty($result['matches'])){ 
	die('没有找到匹配的结果');
}
//die();
$total_found = $result['total_found'];
$ids = implode(',',array_keys($result['matches']));
$db = mysql_connect('192.168.1.x','user','123456');
mysql_select_db('jobui_u_postbar');
$rs = mysql_query("select * from tb_post where P_ID IN ($ids)") or die("Invalid query: " . mysql_error());
$rowCount =mysql_num_rows($rs); 
if($rowCount>0){
	$hitDocs = implode('+',array_keys($result['words']));
	echo "搜索 $hitDocs 获得大约 {$result['total_found']} 条查询结果,检索总数:{$result['total']},以下是第 1-20 条。 (搜索用时 {$result['time']} 秒)<br>";
	echo "<table style='border:1px solid #000'>
		<thread>
		  <tr>
		  	<td width='50'>ID</td>
		  	<td  width='50'>点击</td>
		  	<td width='50'>回复</td>
		  	<td>标题</td>
		  	<td  width='70'>作者</td>
		  	<td  width='70'>最后回复</td>
		  </tr>
		  </thread>
	";
}
while($row = mysql_fetch_assoc($rs)){
	?>
	<tr>
	  	<td><?php echo $row['P_ID']; ?></td>
	  	<td><?php echo $row['P_ViewTotal']; ?></td>
	  	<td><?php echo $row['P_ReplyTotal']; ?></td>
	  	<td><a href="http://ba.jobui.com/post/<?php echo $row['P_ID']; ?>/" target="_blank" ><?php echo $row['P_Title']; ?></a></td>
	  	<td><?php echo $row['P_UserID']; ?></td>
	  	<td><?php echo $row['P_LastReplyUserID']; ?></td>
	</tr>	
	<?php 
}
if($rowCount>0){
	echo "</table>";
}

if($total_found>20){
	$totalPage = ceil($total_found / $perPage);
	echo '<div style="text-align:right;">';
	if($totalPage>20){
		$i = $nowPage;
		$end = $nowPage+20;
	}else{
		$i = 1;
		$end = $totalPage;
	}
	for( ;$i<=$end;$i++){
		if($nowPage == $i){
			echo "<a href='?nowPage=$i&keyword=".urlencode($keyword)."'><span style='color:red;font-weight:bold;'> $i </span></a>&nbsp;";
		}else{
			echo "<a href='?nowPage=$i&keyword=".urlencode($keyword)."'> $i </a>&nbsp;";
		}	
		if($totalPage>$end && $i==$end){
			echo <<<EOF
<form name="searchForm" action="sphinx.php" method="post" >
<input type="hidden" name="keyword" value="$keyword" />
<input type='text' value='$nowPage' name='nowPage' size='5' />
<input type="submit" name="submit" value="GO"/>
</form>	
EOF;
		}	
	}
	echo '</div>';

}
?>
</div>






 

更多精彩博文,请查看:baicaier.iteye.com

你可能感兴趣的:(thread,PHP,mysql,css,全文检索)