栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > PHP > php开源框架 > xunsearch

xunsearch增量索引改进版

xunsearch 更新时间:发布时间: 百科书网 趣学号

  最近测试了xunserach全文索引程序。xunsearch只有LINUX版,所以想用windows服务器请使用其它全文索引程序。 xunsearch本身不像coreseek那样自带增量索引的功能,所以很多从coreseek转过来的朋友很是不习惯。不过xunsearch拥有很 多的API和案例,使用这些程序很容易做出自己的增量索引脚本,只需要把这些脚本添加到LINUX服务器任务里就可以实现增量索引了。

   下面是实现增量索引的PHP程序,修改好账号密码,索引sql语句后把这个文件添加到crontab任务里就可以。

index; // 获取 索引对象
$doc = new XSDocument;  // 创建文档对象
   
//读取上次更新索引时间
$last_index_time = @file_get_contents(APP_PATH.'/last_index_time.txt');
$last_index_time = $last_index_time ? $last_index_time : time(); //这里也可以在last_index_time.txt文件中加个初始值
//删除过的id列表,如果字段有删除时间字段则不需要记录,如果是物理删除,需要记录删除日志,否则无法知道哪些文件曾被删除
$last_del_id = @file_get_contents(APP_PATH.'/last_del_id.txt');
   
$link = mysql_connect('localhost:3306', 'root', '123456') or exit(mysql_error());
mysql_select_db('phpcms', $link) or exit(mysql_error());
mysql_query("SET NAMES utf8");
   
//查询总数据量,并分批更新
$sql = "select count(*) as zongshu from phpcms_news,phpcms_news_data where phpcms_news.id=phpcms_news_data.id and phpcms_news.status=99 and phpcms_news.islink=0 and (phpcms_news.inputtime > {$last_index_time} or phpcms_news.updatetime > {$last_index_time})";
$zongshu = mysql_query($sql) or exit(mysql_error());
   
while($row = mysql_fetch_array($zongshu, MYSQL_ASSOC)){
    $zx[]=$row;
    }
   
$n=0;
$i = 1; $count=1; $add_total = 0; $edit_total=0;$addArray=array();$editArray=array();
//添加分批查询避免查询出过多的数据使PHP报错
do{
      $index->openBuffer(); // 开启缓冲区
        //增加,修改索引
        $sql = "select inch_cms_news.id as id,title,url,inputtime,updatetime,phpcms_news_data.content as content from phpcms_news,phpcms_news_data where phpcms_news.id=phpcms_news_data.id and phpcms_news.status=99 and phpcms_news.islink=0 and (phpcms_news.inputtime > {$last_index_time} or phpcms_news.updatetime > {$last_index_time}) limit $n,100";
        $res = mysql_query($sql) or exit(mysql_error());
        $restult = array();
           
        while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
            $row['title'] = preg_replace('/ |"/','',strip_tags($row['title']));
            $row['content'] = preg_replace('/ |"/','',strip_tags($row['content']));
            $edit_time = $row['updatetime']; $create_time = $row['inputtime'];
            unset($row['updatetime']);
            if($edit_time == $create_time){
                $add_total++;
                $addArray[] = $row;
            } else {
                $edit_total++;
                $editArray[] = $row;
            }
            $doc->setFields($row);
            $index->update($doc);
            $i++;
            //如果回收站回收的数据,然后从已删除记录中,清除该id
            if(strpos($last_del_id, ','.$row['id'].',')!==false){
                $last_del_id = str_replace($row['id'].',', '', $last_del_id);
            }
        }
  $n=$n+100;
  $index->closeBuffer(); // 关闭缓冲区
  }while($n<=$zx['0']['zongshu']);
     
     
$index->openBuffer(); // 开启缓冲区
   
   
   
//删除索引
$sql = "SELECT phpcms_news.id as id,title,url,inputtime,phpcms_news_data.content as content FROM phpcms_news,phpcms_news_data where phpcms_news.id=phpcms_news_data.id and phpcms_news.status!=99 and phpcms_news.islink=0";
$res = mysql_query($sql) or exit(mysql_error());
$del_total = 0; $ids = ''; $delArray = array();
while($row = mysql_fetch_array($res, MYSQL_ASSOC)){
    if(strpos($last_del_id, ','.$row['id'].',')===false){
        $ids .= $row['id'].',';
        $delArray[] = $row;
        $del_total++;
    }
}
if($ids) {
    $index->del(array(trim($ids,',')));
    $last_del_id = $last_del_id ? $last_del_id.$ids : ','.$ids;
}
   
$index->closeBuffer(); // 关闭缓冲区
   
$total = $add_total + $edit_total + $del_total;
if($total){
   
    //记录索引更新时间
    @file_put_contents(APP_PATH.'/last_index_time.txt', time());
    @file_put_contents(APP_PATH.'/last_del_id.txt', $last_del_id);
   
    //记录日志
    if($log){
        $currdate = date('Y-m-d H:i:s',time());
        if(!$first) @file_put_contents('/tmp/myindex_log.txt', "n@@@@@@@@@@@@@@@@@@@@@ {$currdate} 本次更新{$total}条记录,详情如下:@@@@@@@@@@@@@@@@@@@@@@nn", FILE_APPEND);
        if($add_total) addMyIndexLog('add', $add_total, $addArray);
        if($edit_total) addMyIndexLog('update', $edit_total, $editArray);
        if($del_total&&!$first) addMyIndexLog('delete', $del_total, $delArray);
    }
}
   
function addMyIndexLog($logtype, $logtatal, $logdata, $prefix='')
{
    @file_put_contents('/tmp/myindex_log.txt', $prefix.date('Y-m-d H:i:s',time()).' '.$logtype.' index num : '.$logtatal.' '.str_repeat('*', 50)."n".print_r($logdata, true) , FILE_APPEND);
}
   
mysql_free_result($res);
mysql_close($link);
if($total) $index->flushIndex();
?>

 

   程序保存文件为jishubu.net.php并在同一目录下新建last_index_time.txt和last_del_id.txt文件。

我添加的任务是每小时执行两次更新索引如下:

   2,32 * * * * /usr/local/php/bin/php /web/jishubu.net.php

转载于:https://www.cnblogs.com/manbuheiniu/p/3702386.html

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/1066354.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号