最近測試了xunserach全文索引程序。xunsearch只有LINUX版,因此想用windows服務器請使用其它全文索引程序。 xunsearch自己不像coreseek那樣自帶增量索引的功能,因此不少從coreseek轉過來的朋友非常不習慣。不過xunsearch擁有很 多的API和案例,使用這些程序很容易作出本身的增量索引腳本,只須要把這些腳本添加到LINUX服務器任務裏就能夠實現增量索引了。php
下面是實現增量索引的PHP程序,修改好帳號密碼,索引sql語句後把這個文件添加到crontab任務裏就能夠。mysql
<?php /** * 索引實時更新管理器 * 判斷標準: * 添加:pass_state=1 && create_time> 文件記錄值 * 修改:pass_state=1 && edit_time> 文件記錄值 * 刪除:status =0 若是有del_time也能夠經過上面的方法判斷,我這裏沒有 * 文件記錄值:上次更新索引時的時間 * 調用方法: * 正常調用:jishubu.net.php * 不開啓日誌:jishubu.net.php?log=no * 刪除日誌的說明以下: * 若是你是經過我這樣記錄已刪除id的方式,而且非物理刪除,而且開啓了日誌功能,第一次調用最好先在瀏覽器執行jishubu.net.php?first=yes,否則若是刪除數據較多,生成的日誌文件較多,浪費空間。 * 其餘:而後把文件放到crontab中,天天,小時,或分執行一次就好了。請根據本身項目狀況改進。 * @author www.jishubu.net on wish 2012.11.30 改進 */ error_reporting(E_ALL ^ E_NOTICE); define('APP_PATH',dirname(__FILE__)); $prefix = '/usr/local/xunsearch'; $element = "$prefix/sdk/php/app/mfnews.ini"; require_once "$prefix/sdk/php/lib/XS.php"; $first = (trim($_GET['first'])=='yes') ? true : false; $log = (trim($_GET['log'])=='no') ? false : true; $xs = new XS($element); // 創建 XS 對象 $index = $xs->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}條記錄,詳情以下:@@@@@@@@@@@@@@@@@@@@@@\n\n", 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
文件。web
我添加的任務是每小時執行兩次更新索引以下:sql
2,32 * * * * /usr/local/php/bin/php /web/jishubu.net.phpwindows