您的位置:首页 > 其它

全文索引----solr服务器更新增量索引

2016-05-27 13:16 453 查看
在数据量较大时,频繁的更新索引会消耗系统性能,如果更新频率较低,则会影响短时的数据准确性,所以,更新时间的间隔是个很难界定。增量索引解决了这个问题,我们可以在较短的时间内只更新那些变化的数据,这样就避免了大批量的数据更新,因为数据量小,我们可以设置较短的时间间隔,大幅度的提高了用户体验度。本文介绍增量索引。

    一 配置数据源

    1.1 数据库

    为了便于同全量索引比较,我们使用同一个数据库和数据表。增量索引的关键是找到那些修改的数据,所以需要添加一个标识符,数据类型是时间戳,字段命名为updateTime,即四个字段,id,title,content,updateTime,其中updateTime数据类型为TimeStamp,默认值为CURRENT_TIMESTAMP.结构如下:



    solr本身提供了一个last_index_time,这个字段记录了每条记录导入的时间(包括增量和全量导入),我们只需要将updateTime和last_index_time比较即可得到上一次索引更新以后变化的记录。

    1.2 配置data-config.xml

    全量索引继续保留,所以原来的配置不需要修改,我们只需要添加增量索引的配置。首先,我们在索引中用到了updateTime字段,所以需要添加updateTime字段的索引;其次,增量索引的关键就是找到更新的那些数据,通过上边分析,我们首先需要使用last_index_time字段找到更新的记录代码如下:
deltaQuery="select id from blog where updateTime > '${dataimporter.last_index_time}'"

最后,我们根据获得的id更新索引即可,代码如下:
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>

    data-config.xml中一些属性说明:
        transformer 格式转化:HTMLStripTransformer 索引中忽略HTML标签
        query:查询数据库表符合记录数据
        deltaQuery:增量索引查询主键ID,注意这个只能返回ID字段   
        deltaImportQuery:增量索引查询导入的数据
        deletedPkQuery:增量索引删除主键ID查询,注意这个只能返回ID字段   

    1.3 配置schema.xml

    在全量索引的基础上,我们只需要添加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" />


    1.4 修改数据库,产生数据源

    我们直接修改数据库中的一条记录,提供增量索引数据,修改如下:



    二 通过solr Admin客户端更新索引

    2.1 更新操作如下:



    2.2 结果测试



    说明:使用solr Admin客户端方式,简单,快捷,直观性强,适用于数据测试。

    三 使用http请求更新索引

    3.1 原理

    我们前文已经介绍过原理,这里不再赘述。前文连接:点击打开链接

    3.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 结果测试



    四 使用官方Scheduler实现索引更新

    4.1 jar包配置

    下载apache-solr-dataimportscheduler-1.0.jar放到Tomcat的webapps的solr目录的WEB-INF的lib目录下。

    4.2 修改solr的WEB-INF目录下的web.xml文件,为<web-app>元素添加一个子元素,
<pre name="code" class="html"><listener>
<listener-class>
org.apache.solr.handler.dataimport.scheduler.ApplicationListener
</listener-class>
</listener>
</pre><pre style="box-sizing: border-box; font-family: Arial, 'Microsoft YaHei'; font-size: 13px; white-space: pre-wrap; padding: 0px; margin-top: 0px; margin-bottom: 0px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; border: 1px solid rgb(204, 204, 204); border-radius: 4px; overflow: hidden; width: 1309px; background-color: rgb(245, 245, 245);">
    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.1 概述

    在solr的使用过程中或者测试时,会产生一些脏数据,我们需要及时的删除这些脏数据,小编一solr Admin客户端介绍如何删除或清空索引。

    5.2 操作



    5.3 说明

    我们选择的是更新操作,文件类型选择XML格式,更新语句可以填写删除语句,如果删除某个索引,可以填写如下代码:

    方法一 
<delete><id>1</id></delete>
<commit/>
    方法二
<delete><query>id:1</query></delete>
<commit/>


    如果要清空所有索引,可以填写如下代码:

<delete><query>*:*</query></delete>
<commit/>


    六 总结

    增量索引提供了小批量数据更新的可能,在实际需要中,我们可以将全量索引和增量索引结合使用,达到短时间的数据同步和性能消耗的平衡。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  solr