今天把coreseek和目前干的项目进行了整合了,前台用的是php,测试数据在mysql里面,共有11w条数据
如果采用普通的mysql查询,页面运行的时间是0.9s,索引以后搜索索引再根据索引得到的id查询数据库详细信息的页面执行时间是0.03s,速度快了30倍,我很满意,哈哈
以下是php搜索索引的核心部分代码
<?php require_once AN_ROOT."/inc/sphinxapi.php"; require_once AN_ROOT."/res/adodb/adodb.inc.php"; $keywords = $_REQUEST['keywords']; $info = getInfoCoreSeek($keywords,$_GET['page']); function getInfoCoreSeek($keywords,$page=1) { $mode = SPH_MATCH_ANY; $host = "localhost"; $port = 3312; $index = "*"; $limit = 4; $ranker = SPH_RANK_PROXIMITY_BM25; if(!$page) $page=1; $page = intval($page); $off = ($page-1)*$limit; $cl = new SphinxClient (); $cl->SetServer ( $host, $port ); $cl->SetConnectTimeout ( 1 ); $cl->SetWeights ( array ( 100, 1 ) ); $cl->SetMatchMode ( $mode ); $cl->SetLimits ( $off, $limit, ( $limit>10000 ) ? $limit : 10000 ); $cl->SetRankingMode ( $ranker ); $cl->SetArrayResult ( true ); $res = $cl->Query ( join(" ", $keywords), $index ); if($res) { $pageL = PageQuery($res['total'],$page,4,"act=search&keywords=".$_REQUEST['keywords']); foreach($res['matches'] as $v) { $arrID[] = $v['id']; } $rs = $GLOBALS['db']->GetAll("select id,title,content from info where id in(".join(',', $arrID).")"); $GLOBALS['tpl']->assign('pageL', $pageL); //echo '<pre>'; //print_r($res); //echo '</pre>'; return $rs; } } ?>