提示:coreseek 完全兼容Sphinx
discuz X3.1中连接coreseek的代码解析source\module\search\search_forum.php
if($srchtype == 'fulltext' && $_G['setting']['sphinxon']) { //判断是否开启sphinx搜索引擎
require_once libfile('class/sphinx'); //包含sphinx 类文件
$s = new SphinxClient(); //实例化
$s->setServer($_G['setting']['sphinxhost'], intval($_G['setting']['sphinxport'])); //连接coreseek
$s->setMaxQueryTime(intval($_G['setting']['sphinxmaxquerytime'])); //设置最大执行时间
$s->SetRankingMode($_G['setting']['sphinxrank']); //评分模式
$s->setLimits(0, intval($_G['setting']['sphinxlimit']), intval($_G['setting']['sphinxlimit'])); //取条数
$s->setGroupBy('tid', SPH_GROUPBY_ATTR); //分组过滤,一个帖子只取一条数据
if($srchfilter == 'digest') { //过滤模式
$s->setFilterRange('digest', 1, 3, false);
}
if($srchfilter == 'top') { /过滤模式
$s->setFilterRange('displayorder', 1, 2, false);
} else {
$s->setFilterRange('displayorder', 0, 2, false);
}
if(!empty($srchfrom) && empty($srchtxt) && empty($srchuid) && empty($srchuname)) {
$expiration = TIMESTAMP + $cachelife_time;
$keywords = '';
if($before) {
$spx_timemix = 0;
$spx_timemax = TIMESTAMP - $srchfrom;
} else {
$spx_timemix = TIMESTAMP - $srchfrom;
$spx_timemax = TIMESTAMP;
}
} else {
$uids = array();
if($srchuname) {
$uids = array_keys(C::t('common_member')->fetch_all_by_like_username($srchuname, 0, 50));
if(count($uids) == 0) {
$uids = array(0);
}
} elseif($srchuid) {
$uids = array($srchuid);
}
if(is_array($uids) && count($uids) > 0) {
$s->setFilter('authorid', $uids, false);
}
if($srchtxt) {
if(preg_match("/\".*\"/", $srchtxt)) {
$spx_matchmode = "PHRASE";
$s->setMatchMode(SPH_MATCH_PHRASE); //匹配模式
} elseif(preg_match("(AND|\+|&|\s)", $srchtxt) && !preg_match("(OR|\|)", $srchtxt)) {
$srchtxt = preg_replace("/( AND |&| )/is", "+", $srchtxt);
$spx_matchmode = "ALL";
$s->setMatchMode(SPH_MATCH_ALL); //匹配模式
} else {
$srchtxt = preg_replace("/( OR |\|)/is", "+", $srchtxt);
$spx_matchmode = 'ANY';
$s->setMatchMode(SPH_MATCH_ANY); //匹配模式
}
$srchtxt = str_replace('*', '%', addcslashes($srchtxt, '%_'));
foreach(explode('+', $srchtxt) as $text) {
$text = trim(daddslashes($text));
if($text) {
$sqltxtsrch .= $andor;
$sqltxtsrch .= $srchtype == 'fulltext' ? "(p.message LIKE '%".str_replace('_', '\_', $text)."%' OR p.subject LIKE '%$text%')" : "t.subject LIKE '%$text%'";
}
}
$sqlsrch .= " AND ($sqltxtsrch)";
}
if(!empty($srchfrom)) {
if($before) {
$spx_timemix = 0;
$spx_timemax = TIMESTAMP - $srchfrom;
} else {
$spx_timemix = TIMESTAMP - $srchfrom;
$spx_timemax = TIMESTAMP;
}
$s->setFilterRange('lastpost', $spx_timemix, $spx_timemax, false);
}
if(!empty($specials)) {
$s->setFilter('special', explode(",", $special), false); //过滤模式
}
$keywords = str_replace('%', '+', $srchtxt).(trim($srchuname) ? '+'.str_replace('%', '+', $srchuname) : '');
$expiration = TIMESTAMP + $cachelife_text;
}
if($srchtype == "fulltext") {
$result = $s->query("'".$srchtxt."'", $_G['setting']['sphinxmsgindex']);
} else {
$result = $s->query($srchtxt, $_G['setting']['sphinxsubindex']);
}
//debug($result);
$tids = array();
if($result) {
if(is_array($result['matches'])) {
foreach($result['matches'] as $value) {
if($value['attrs']['tid']) {
$tids[$value['attrs']['tid']] = $value['attrs']['tid'];
}
}
}
}
if(count($tids) == 0) {
$ids = 0;
$num = 0;
} else {
$ids = implode(",", $tids);
$num = $result['total_found'];
}