在php中使用sphinx

$sphinx = $_POST[‘word’];
$sphinx->SetServer(“localhost”,9312);
$sphinx->SetMatchMode(SPH_MATCH_ANY); //SPH_MATCH_ANY 拆词搜索
//SPH_MATCH_ALL 整个词搜索
$result = $sphinx ->query(“$keyword”,”*”);  //* 代表索引(包括主索引,增量索引,分布式索引),sphinx配置文件里面有很多索引
$opt=array(“before_match”=>”<font style=’color:red’>”,”after_match”=>”</font>”);//人为定义高亮显示的前后标签
$sphinx->buildExcerpts(“$row”,”main”,”$keyword”,$opts);
//$row=sphinx查询结果,mian=sphinx索引,$keyword=关键字
 
//建立实时索引
建立一个表sph_counter  记录一个id  和 一个最大的文章id
//修改配置文件
//主索引
sql_query_pre = replace into sph_counter select 1,max(id) from post  //更新主索引的时候同时更新sph_conunt记录的最大ID值
sql_query = select id,title,content from post where id<=(select max_doc_id from sph_counter where counter_id=1)         //读取最大的id值以下的文章做主索引
//增量索引
source delta : main
{
sql_query_pre = SET NAMES utf8
sql_query = select id,title,content from post where id >(select max_doc_id from sph_counter where counter_id=1)          //更新主索引的最大id值之后的数据
}
建立crontable 每5分钟运行一次 ./indexer delta –rotate         //每五分钟刷新一次增量索引 delta
建立crontable 每天2点钟运行一次 ./indexer main –rotate        //刷新一次主索引 main

发表评论