包括搜索和更新状态两个功能使用。
//实例化 require_once("sphinxclient.class.php"); $sphinx = new SphinxClient; //搜索功能 $mode = SPH_MATCH_EXTENDED2; //匹配模式 $ranker = SPH_RANK_PROXIMITY_BM25; //统计相关度计算模式,仅使用BM25评分计算 $sphinx->SetServer('127.0.0.1', '9312'); $sphinx->SetArrayResult(true); $sphinx->SetMatchMode($mode); $sphinx->SetRankingMode($ranker); //过滤status字段,只显示正常帖子,不显示已删除帖子。字段要在sphinx配置为索引 $sphinx->SetFilter('status', array(0)); //过滤时间段 if($StartTime > 0) { $sphinx->SetFilterRange('senddate', $StartTime, time(), false); } //设置字段的权重 $sphinx->SetFieldWeights(array('title' => 10, 'description' => 5, 'body' => 5)); //设置排序,先按权重,再按id $sphinx->SetSortMode( SPH_SORT_EXTENDED, "@weight DESC, @id desc" ); //分页 $limitstart = 0; $row = 10; $sphinx->SetLimits($limitstart, (int)$row, ($row>1000) ? $row : 1000); //结果 $res = array(); $res = $sphinx->Query($this->Keywords." @flag !s", 'mysql, delta'); var_dump($res); //删除索引,删除帖子时更新状态(主索引和增量索引),不让搜索时搜索出来 $sphinx->UpdateAttributes('mysql', array('status'),array($aid => array(-2))); $sphinx->UpdateAttributes('delta', array('status'),array($aid => array(-2))); =========================================================================可以在创建索引时,配置文件中设置以下属性: sql_attr_bool = del is_deleted 默认值为 0 ,当 记录要删除时,调用接口中的 UpdateAttributes设置其属性值为 1,在搜索和合并索引时增加过滤器只让 is_deleted 为 0 的 记录通过
select id, title, content, 1 as delfrom table
$s->UpdateAttributes('post_main, post_delta', array('del'), array($id => array(0)));================================================================== public $client; public function __construct($id, $module = null) { parent::__construct ( $id, $module ); $this->client = new SphinxClient (); } public function actionTest(){ $this->client->UpdateAttributes ( "products", array("deleted"), array(649768=>array(1)) ); $this->client->UpdateAttributes ( "products_delta", array("deleted"), array(649768=>array(1)) ); }在合并索引的时候怎么设置过程器?相关的开关是--merge-dst-range: indexer --merge main delta --merge-dst-range is_deleted 0 0 这个开关允许您在合并过程中对目标索引实施过滤。过滤器可以有多个,只有满足全部过滤 条件的文档才会在最终合并后的索引中出现。在此例子中,过滤器只允许“is_deleted”为 0 的那些条件通过,而去除所有标记为已删除(“is_deleted” 为非0)的 记录