xunsearch增量索引改进版
最近测试了xunserach全文索引程序。xunsearch只有LINUX版,所以想用windows服务器请使用其它全文索引程序。xunsearch本身不像coreseek那样自带增量索引的功能,所以很多从coreseek转过来的朋友很是不习惯。不过xunsearch拥有很多的API和案例,使用这些程序很容易做出自己的增量索引脚本,只需要把这些脚本添加到LINUX服务器任务里就可以实现增量索引了。
下面是实现增量索引的PHP程序,修改好账号密码,索引sql语句后把这个文件添加到crontab任务里就可以。
<?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
文件。
我添加的任务是每小时执行两次更新索引如下:
2,32 * * * * /usr/local/php/bin/php /web/jishubu.net.php
xunsearch增量索引改进版的更多相关文章
- [Solr] (源) Solr与MongoDB集成,实时增量索引
一. 概述 大量的数据存储在MongoDB上,需要快速搜索出目标内容,于是搭建Solr服务. 另外一点,用Solr索引数据后,可以把数据用在不同的项目当中,直接向Solr服务发送请求,返回xml.js ...
- coreseek增量索引合并
重建主索引和增量索引: [plain] view plain copy /usr/local/coreseek/bin/indexer--config /usr/local/coreseek/etc/ ...
- coreseek增量索引
1.在多数情况下,因为Coreseek索引速度高达10MB/s,所以只需要创建一个索引源即可满足需求,但是在数据量随时激增的大型应用中(如SNS.评论系统等),单一的索引源将会给indexer造成极大 ...
- sphinx通过增量索引实现近实时更新
一.sphinx增量索引实现近实时更新设置 数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少. 例如.原来的数据 ...
- Lucene.net 实现近实时搜索(NRT)和增量索引
Lucene做站内搜索的时候经常会遇到实时搜索的应用场景,比如用户搜索的功能.实现实时搜索,最普通的做法是,添加新的document之后,调用 IndexWriter 的 Commit 方法把内存中的 ...
- sphinx 增量索引 实现近实时更新
一.sphinx增量索引的设置 数据库中的已有数据很大,又不断有新数据加入到数据库中,也希望能够检索到.全部重新建立索引很消耗资源,因为我们需要更新的数据相比较而言很少.例如.原来的数据有几百万条 ...
- solr与.net系列课程(六)solr定时增量索引与安全
solr与.net系列课程(六)solr定时增量索引与安全 solr增量索引的方式,就是一个Http请求,但是这样的请求显然不能满足要求,我们需要的是一个自动的增量索引,solr官方提供了一个定时器 ...
- sphinx增量索引
首先建立一个计数表,保存数据表的最新记录ID CREATE TABLE `sph_counter` ( `id` int(11) unsigned NOT NULL, `max_id` int(1 ...
- Sphinx 增量索引更新
是基于PHP API调用,而不是基于sphinxSE.现在看来sphinxSE比API调用更简单的多,因为之前没有想过sphinxSE,现在先把API的弄明白.涉及到的:sphinx 数据源的设置,简 ...
随机推荐
- 对比学习UIKit和AppKit--入门级
UIKit是用来开发iOS的应用的,AppKit是用来开发Mac应用的,在使用过程中他们很相似,可是又有很多不同之处,通过对比分析它们的几个核心对象,可以避免混淆. UIKit和AppKit都有一个A ...
- 理解Objective C 中id
什么是id,与void *的区别 id在Objective C中是一个类型,一个complier所认可的Objective C类型,跟void *是不一样的,比如一个 id userName, 和vo ...
- 《Head First设计模式(中文版)》
<Head First设计模式(中文版)>共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式.前言先介绍<Head First设计模式(中文版)>的 ...
- Python清理内存中的密码
基本不太好搞.可以参考如下讨论: http://stackoverflow.com/questions/728164/securely-erasing-password-in-memory-pytho ...
- 检测INT3 软断点
“INT3”断点指令的机器码是 “0xcch” 检测思路,取函数地址,判断第一个字节是不是 “CCh” BYTE bFirst = ; ProcAddres = GetProcAddress(Load ...
- kali linux SET工具包
尝试使用SET生成Teensy脚本是出现问题 1.出现错误 [!] Something went wrong, printing the error: [Errno 2] No such file o ...
- Nullsoft教程 NSIS初学者图文教程一
Nullsoft教程 NSIS初学者图文教程一 来源:互联网 作者:佚名 时间:03-29 00:34:33 [大 中 小] Nullsoft Installation System(nsis) 是一 ...
- magento -- 添加中国省份列表
magento本身的数据库里不包含中国的省份信息,你可以执行以下的SQL语句来添加 添加到directory_country_region表 INSERT INTO `directory_count ...
- PM 时钟机制
PM 时钟机制 10.1 Minix3 PM 时钟机制概述在 MINIX3 中,除了前面所讲到的 CLOCK 时钟,在 pm 中也是维持了一个时钟, 我们暂且不分析为啥要这么做,我就分析是怎么实现这个 ...
- 神逸之作:国产快速启动软件神品ALTRun
http://xbeta.info/altrun.htm 作者: ET民工和塞壬 日期: 2010-09-15 分类: windows 标签: quick-launch <神逸之作:国产快速启动 ...