紧接上篇ansj分词整合后,开始需要做索引。索引分增量和全量,可以直接连接数据库去做,也可以通过程序去做,以下实现连接数据库去做,相对来说比较简单。
1.修改multicore/new_core/conf/solrconfig.xml文件(上篇提到过的),在里面新增
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler> <requestHandler name="/deltaimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">delta-data-config.xml</str> </lst> </requestHandler>
其中第一段是专门做全量索引的,第二段做增量索引(主要是靠DataImportHandler类实现)
2.新增multicore/new_core/conf/data-config.xml文件
<dataConfig> <dataSource name="jdbc" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.0.81:3306/new_mall?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&useUnicode=true" user="root" password="HyS_Db@2014"/> <document name="mall_goods"> <entity name="MallGoods" pk="id" query="select * from mall_goods limit ${dataimporter.request.length} offset ${dataimporter.request.offset}" transformer="RegexTransformer"> <field column="goods_id" name="id" /> <field column="title" name="title" /> <field column="subtitle" name="subtitle" /> <field column="cover_img_path" name="coverImgPath" /> <field column="description" name="description" /> <field column="update_date" name="updateDate" /> </entity> </document> </dataConfig>
dataSource不用说了,数据源配置来的
entity文档中的实体配置(注意pk="id" 不能随便改 ,需要和schema.xml中的<uniqueKey>id</uniqueKey>匹配,否则会报“ org.apache.solr.common.SolrException: Document is missing mandatory uniqueKey field: id”)
query 查询语句(可分页)
transformer 暂时不清楚干啥
field定义列名
3.新增multicore/new_core/conf/delta-data-config.xml文件
<dataConfig> <dataSource name="jdbc" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.0.81:3306/new_mall?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&useUnicode=true" user="root" password="HyS_Db@2014"/> <document name="mall_goods"> <entity name="MallGoods" pk="id" query="select * from mall_goods" deltaImportQuery="select * from mall_goods where goods_id='${dih.delta.id}'" deltaQuery="select goods_id as id from mall_goods where update_date > '${dih.last_index_time}'" transformer="RegexTransformer"> <field column="goods_id" name="id" /> <field column="title" name="title" /> <field column="subtitle" name="subtitle" /> <field column="cover_img_path" name="coverImgPath" /> <field column="description" name="description" /> <field column="update_date" name="updateDate" /> </entity> </document> </dataConfig>
deltaQuery查询出有更改过的id
deltaImportQuery根据id查询
4.修改multicore/new_core/conf/schema.xml文件,定义field索引配置
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="title" type="text_ansj" indexed="true" stored="true" required="true" multiValued="false"/> <field name="subtitle" type="text_ansj" indexed="true" stored="true" required="false" multiValued="false"/> <field name="coverImgPath" type="string" indexed="false" stored="true" required="true" multiValued="false" /> <field name="description" type="text_ansj" indexed="true" stored="true" required="false" multiValued="false"/> <field name="updateDate" type="text_ansj" indexed="true" stored="true" required="false" multiValued="false"/>
注意上面选择一下text_ansj
5.solr的war包可能还缺少部分jar包,需要把mysql的jar,以及solr项目中dist目录下的jar包都放到solr的web站点中
6.开始运行
全量:http://solr.xxxx.com:8082/new_core/dataimport?command=full-import&commit=true&clean=false&offset=0&length=100000(其中0到100000的数据建立索引)
增量:http://solr.ehaoyao.com:8082/new_core/deltaimport?command=delta-import&entity=MallGoods
entity:是document下面的标签(data-config.xml)。使用这个参数可以有选择的执行一个或多个entity 。使用多个entity参数可以使得多个entity同时运行。如果不选择此参数那么所有的都会被运行。
clean:选择是否要在索引开始构建之前删除之前的索引,默认为true
commit:选择是否在索引完成之后提交。默认为true
optimize:是否在索引完成之后对索引进行优化。默认为true
debug:是否以调试模式运行,适用于交互式开发(interactive development mode)之中。
请注意,如果以调试模式运行,那么默认不会自动提交,请加参数“commit=true”
注意:在做增量索引的时候
很容易出现deltaQuery has no column to resolve to declared primary key pk='id'这种异常
主要是因为ID" must be used as it is in 'deltaQuery' select statement as "select ID from ..."
(if you different name for ID column in database, then use 'as' keyword in select statement. In my case I had 'studentID' as primary key in student table. So I used it as "select studentID as ID from ..."
--> The same applies to 'deletedPkQuery'
At present its working fine for me. Any updation in database is reflected in Solr as well.
所以,delta-data-config.xml文件需要注意一下pk的值
参考连接:
http://shiyanjun.cn/archives/444.html
http://blog.duteba.com/technology/article/70.htm
http://www.devnote.cn/article/89.html
http://qiaqia26.iteye.com/blog/1004996
http://zzstudy.offcn.com/archives/8104
http://blog.csdn.net/duck_genuine/article/details/5426897
------------------------------------------------------------------------------------------------------------------------------
最后补充:
有时候需要删除索引数据,可以这样删除
http://xxxx/new_core/update/?stream.body=<delete><query>*:*</query></delete>&stream.contentType=text/xml;charset=utf-8&commit=true
new_core 表示你要删除哪个核下面的索引
相关推荐
Solr数据库插入(全量和增量)索引,全量一般用于第一次创建索引情况,批量一般更新数据部分创建索引。
NULL 博文链接:https://fengqingyuntan.iteye.com/blog/2254145
solr增量导入更新索引包
主要讲解了 solr客户端如何调用带账号密码的solr服务器调用,实现添加索引和查询索引,以及分组查询
solr服务开启、停止、重启: cd到solr的bin目录 ./solr start | restart | stop -force ./solr start -p 9923 -force ./solr stop -p 9923 -force ...solr 全量数据迁移方法:(数据结构及数据全部迁移出来了
NULL 博文链接:https://iamyida.iteye.com/blog/2215358
图解Solr6.6.0安装与MySQL配置(全量、增量更新)
利用solr搭建MYSQL集成环境,实现MYSQL数据库索引的添加和修改。
solr6定时增量更新jar包
搜索引擎solr环境配置、分词及索引操作
该文档主要用于创建在solr中创建hbase的二级索引的教程,新建solr的分片,以及后续修改和新建配置文件等,仅供参考
solr定时增量更新索引所需jar包包括:solr-dataimporthandler-4.0.0.jar、solr-dataimportscheduler.jar(6.x适用)、solr-dataimporthandler-extras-4.0.0.jar。
solr创建索引并查询,希望能够帮助有需要的人。。。
(solr系列:五) solr定时实时重建索引和增量更新-附件资源
将mysql表索引到solr中 在这里,我们将逐步进行过程。 要将mysql表索引到solr中,我们需要这些技术。 MySQL数据库 让我们从MySql开始聚会。 使用yum或任何您喜欢的方法,先安装mysql,再安装mysql-server。 yum ...
Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引;也可以通过Http Get操作提出查找请求,并得到XML格式的...
solr测试数据,数据文件
包括部署、配置、Solr Core、Solr DIH、全量导入、增量导入、索引、中文分词、查询组件、Solr Facet、高亮、查询建议,以及企业如何在真实的项目中使用Solr。不仅讲解了基本概念和使用方法,而且还分析了各组件的...