标签:
上篇文章我们介绍了全量更新solr索引,但是在数据量较大时,频繁的更新索引会消耗系统性能,如果更新频率较低,则会影响短时的数据准确性,所以,更新时间的间隔是个很难界定。增量索引解决了这个问题,我们可以在较短的时间内只更新那些变化的数据,这样就避免了大批量的数据更新,因为数据量小,我们可以设置较短的时间间隔,大幅度的提高了用户体验度。本文介绍增量索引。为了便于同全量索引比较,我们使用同一个数据库和数据表。增量索引的关键是找到那些修改的数据,所以需要添加一个标识符,数据类型是时间戳,字段命名为updateTime,即四个字段,id,title,content,updateTime,其中updateTime数据类型为TimeStamp,默认值为CURRENT_TIMESTAMP.结构如下:
deltaQuery="select id from blog where updateTime > '${dataimporter.last_index_time}'"deltaImportQuery="select * from blog where id='${dih.delta.id}'"<dataConfig>
<dataSource name="jfinal_demo" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.21.20:3306/jfinal_demo" user="root" password="123456" batchSize="-1" />
<document name="testDoc">
<entity name="blog" dataSource="jfinal_demo" pk="id"
query="select * from blog"
deltaImportQuery="select * from blog where id='${dih.delta.id}'"
deltaQuery="select id from blog where updateTime > '${dataimporter.last_index_time}'">
<field column="id" name="id"/>
<field column="title" name="title"/>
<field column="content" name="content"/>
<field column="updateTime" name="updateTime"/>
</entity>
</document>
</dataConfig>在全量索引的基础上,我们只需要添加updataTime字段的索引即可,代码如下:
<field name="id" type="text_general" indexed="true" stored="true" /> <field name="title" type="text_general" indexed="true" stored="true" /> <field name="content" type="text_general" indexed="true" stored="true" /> <field name="updateTime" type="text_general" indexed="true" stored="true" />
2.1 更新操作如下:
2.2 结果测试
我们继续使用HttpURLConnection对象来完成http请求,代码如下:
/**
* 访问URL,全量索引
*/
public static Boolean runHttpGet(){
Boolean flag = false;
//设置请求的路径
String strUrl="http://192.168.22.216:8983/solr/dataimport?command=delta-import";
//将请求的参数进行UTF-8编码,并转换成byte数组=
try {
//创建一个URL对象
URL url=new URL(strUrl);
//打开一个HttpURLConnection连接
HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
//设置连接超时的时间
urlConn.setDoOutput(true);
//在使用post请求的时候,设置不能使用缓存
urlConn.setUseCaches(false);
//设置该请求为post请求
urlConn.setRequestMethod("GET");
urlConn.setInstanceFollowRedirects(true);
//配置请求content-type
urlConn.setRequestProperty("Content-Type", "application/json, text/javascript");
//执行连接操作
urlConn.connect();
//发送请求的参数
DataOutputStream dos=new DataOutputStream(urlConn.getOutputStream());
dos.flush();
dos.close();
if(urlConn.getResponseCode()==200){
flag = true;
//显示
InputStreamReader isr = new InputStreamReader(urlConn.getInputStream(), "utf-8");
int i;
String strResult = "";
// read
while ((i = isr.read()) != -1) {
strResult = strResult + (char) i;
}
//System.out.println(strResult.toString());
isr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
等同前文,使用此方法,也可以使用quartz做任务调度,代码不再示范。3.3 结果测试
<pre name="code" class="html"><listener>
<listener-class>
org.apache.solr.handler.dataimport.scheduler.ApplicationListener
</listener-class>
</listener> 4.3 修改配置文件dataimport.properties:
在SOLR_HOME\solr目录下面新建一个目录conf(注意不是SOLR_HOME\solr\collection1下面的conf),然后用解压文件打开apache-solr-dataimportscheduler-1.0.jar文件,将里面的dataimport.properties文件拷贝过来,进行修改,下面是最终我的自动定时更新配置文件内容:
################################################# # # # dataimport scheduler properties # # # ################################################# # to sync or not to sync # 1 - active; anything else - inactive syncEnabled=1 # which cores to schedule # in a multi-core environment you can decide which cores you want syncronized # leave empty or comment it out if using single-core deployment syncCores=core1,core2 # solr server name or IP address # [defaults to localhost if empty] server=localhost # solr server port # [defaults to 80 if empty] port=8080 # application name/context # [defaults to current ServletContextListener's context (app) name] webapp=solr # URL params [mandatory] # remainder of URL params=/dataimport?command=delta-import&clean=false&commit=true # schedule interval # number of minutes between two runs # [defaults to 30 if empty] interval=1 # 重做索引的时间间隔,单位分钟,默认7200,即5天; # 为空,为0,或者注释掉:表示永不重做索引 reBuildIndexInterval=7200 # 重做索引的参数 reBuildIndexParams=/dataimport?command=full-import&clean=true&commit=true # 重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000; # 两种格式:2012-04-11 03:10:00 或者 03:10:00,后一种会自动补全日期部分为服务启动时的日期 reBuildIndexBeginTime=03:10:00
5.2 操作
<delete><id>1</id></delete> <commit/>方法二
<delete><query>id:1</query></delete> <commit/>
如果要清空所有索引,可以填写如下代码:
<delete><query>*:*</query></delete> <commit/>六 总结
标签:
原文地址:http://blog.csdn.net/u010942465/article/details/51354016