<?php
//检查sphinx是否能连接,不能重试两次,能则连接,不用mysql协议,仅供参考
function checkSphinxNoMysql() {
$flag = true;
$retries = 0;
while ( $flag && $retries < 2 ) {
$s = new SphinxClient ();
$s->setServer ( $_ENV ['db_host'], $_ENV ['current_sphinx_port'] );
if (! $s->open ()) {
//此处为如果连接不上就更改配置文件,根据项目需要来写
//global $configDefault;
//updateConfig ( $configDefault );
$retries ++;
} else {
$flag = false;
break;
}
}
return $s;
if ($retries >= 2) {
//sendemail or not
return false;
}
}
$order_column = 'id DESC,time DESC';//排序规则
//$s = checkSphinx ();
$s = new SphinxClient ();
$s->setServer ( 'sphinx_host', 'sphinx_port');
//以上两句代码亦可使用$s = checkSphinx ();来代替
$indexname = "page_keyword";//索引名字
$s->setMatchMode ( SPH_MATCH_PHRASE );
$s->SetSortMode ( SPH_SORT_EXTENDED, $order_column );
$s->setMaxQueryTime ( 100000 );
$s->setLimits ( 0, $limit_total, $limit_total );
$keyword_sphinx =
iconv ( "gbk", "utf-8", $keyword );
$result = $s->query ( $keyword_sphinx, $indexname );
$s->close ();
if ($result ['total'] > 0) {
var_dump($result ['matches']);
//根据打印出的结果进行相应的读取
}
?>