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 数据源的设置,简 ...
随机推荐
- linux 杀死进程的方法
# kill -pid 注释:标准的kill命令通常都能达到目的.终止有问题的进程,并把进程的资源释放给系统.然而,如果进程启动了子进程,只杀死父进程,子进程仍在运行,因此仍消耗资源.为了防止这些所谓 ...
- sqlserver定时备份
前言:给客户部署好系统以后,如果不加一个定时备份,总感觉心里不放心,所以一定要做定时备份,并且定时备份是很简单的 新建作业--基本信息 新建步骤 ) ),) print @filename , NOF ...
- SGU 183 Painting the balls (优化的动态规划)
题意:给n个白球,选其中一些涂为黑色,且给了涂第i个球的花费为ci,要求每m个连续的球中至少有两个黑球,问最小花费是多少? 容易想到一个方程dp[i][j]=min{dp[k][i]}+c[j] dp ...
- C# 调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配
在dllimport中加入CallingConvention参数就行了,[DllImport(PCAP_DLL, CharSet = CharSet.Auto, CallingConvention = ...
- 使用iskindofclass来发现对象是否是某类或其子类的实例
发现对象是否是特定类或其子类的实例 要发现对象是否是某类或其子类的实例,请在对象上调用 isKindOfClass: 方法.当应用程序需要发现其响应的消息(实现的或继承的),它有时进行以上的检查. s ...
- PHP中的错误处理和异常处理
错误处理: 1.语法错误 2.运行时的错误 3.逻辑错误 错误报告: 错误E_ERROR 警告E_WARNING 注意E_NOTICE 开发 ...
- 6、网页制作Dreamweaver(HTML结构--dom操作)
一.基本语法:数据类型(字符串,小数,整数,布尔,时间) var, var s = "3.14"; var n = parseFloat(s); ; s += 5; var d = ...
- array_intersect() php筛选两个数组共有的元素
我们已经讲过如何筛选出连个数组中不共有的元素,今天就来看看php如何筛选出两个数组中共有的元素,例如筛选$array1和$array2共有的元素. 函数名:array_intersect(): 调用方 ...
- PHP CI分页类带多个参数
通过修改system中的pagination.php,给每个<a>都增加了class="pagination". view页面 <div class=" ...
- [转]LUA 学习笔记
Lua 学习笔记 入门级 一.环境配置 方式一: 1.资源下载http://www.lua.org/download.html 2.用src中的源码创建了一个工程,注释调luac.c中main函数,生 ...