1.solr 的全量更新
1.core/conf 目錄下的 solrconfig.xml,新增如下配置
<!--添加如下配置 增加resultHandler配置 --> <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
2 在core/conf 目錄下的新建 data-config.xml 文件,新增如下配置
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <!--數據源--> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.2.10:3306/xt" user="root" password=""/> <document> <entity name="solrTest" query="SELECT fid,ftime,fcontent,ftitle,flastupdatetime FROM solrTest"> <!--查詢的數據和數據庫索引意義對應column 是查詢的字段name 是solr索引對應的字段--> <field column="fid" name="fid"/> <field column="ftitle" name="ftitle"/> <field column="fcontent" name="fcontent"/> <field column="flastupdatetime" name="flastupdatetime"/> <field column="ftime" name="ftime"/> </entity> </document> </dataConfig>
3.修改core/conf 目錄下 managed-schema.xml 的文件,新增需要索引的列,示例:
<field name="fid" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="fpic" type="string" indexed="true" stored="true" multiValued="false" /> <field name="ftitle" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="fauthor" type="string" indexed="true" stored="true" multiValued="false" /> <field name="ftime" type="string" indexed="true" stored="true" multiValued="false" /> <field name="fdesc" type="string" indexed="true" stored="true" multiValued="false" /> <field name="fcontent" type="string" indexed="true" stored="true" multiValued="false" /> <field name="flastupdatetime" type="text_general" indexed="true" stored="true" required="true" multiValued="false" />
4.進入solr 的后台管理,進行全量更新
2. solr 的增量更新
1.首先要弄懂幾個必要的屬性,以及數據庫建表事項,和dataimporter.properties 、data-config.xml里面的數據
<!-- transformer 格式轉化:HTMLStripTransformer 索引中忽略HTML標簽 --->
<!-- query:查詢數據庫表符合記錄數據 --->
<!-- deltaQuery:增量索引查詢主鍵ID ---> 注意這個只能返回ID字段
<!-- deltaImportQuery:增量索引查詢導入數據 --->
<!-- deletedPkQuery:增量索引刪除主鍵ID查詢 ---> 注意這個只能返回ID字段
2.數據庫配置注意事項
1.如果只涉及添加,與修改業務,那么數據庫里只需額外有一個timpstamp字段
就可以了,默認值為當前系統時間,CURRENT_TIMESTAMP
2.如果還涉及刪除業務,那么數據里就需額外再多添加一個字段isdelete,int類型的
用0,1來標識,此條記錄是否被刪除
3.dataimporter.properties
這個配置文件很重要,它是用來記錄當前時間與上一次修改時間的,通過它能夠找出,那些,新添加的,修改的,或刪除的記錄標識,此條記錄是否被刪除的記錄
4.增量更新就是在全量更新的基礎上加上一些配置,配置如下:
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig> <!--數據源--> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.2.10:3306/xtjkqyfw" user="root" password="Biaopu8888"/> <document> <entity name="solrTest" query="SELECT fid,ftime,fcontent,ftitle,flastupdatetime FROM solrTest where flag = '0'" deltaImportQuery = "SELECT fid,ftime,fcontent,ftitle,flastupdatetime FROM solrTest where fid = '${dataimporter.delta.fid}'" deltaQuery = "SELECT fid FROM solrTest where flastupdatetime > '${dataimporter.last_index_time}' and flag = '0'" deletedPkQuery = "SELECT fid FROM solrTest where flag = '1'" > <!--查詢的數據和數據庫索引意義對應column 是查詢的字段name 是solr索引對應的字段--> <field column="fid" name="fid"/> <field column="ftitle" name="ftitle"/> <field column="fcontent" name="fcontent"/> <field column="flastupdatetime" name="flastupdatetime"/> <field column="ftime" name="ftime"/> </entity> </document> </dataConfig>
5.通過后台管理手動增量更新和通過瀏覽器手動更新
在瀏覽器直接輸入網站 : http://localhost:8089/solr/active/dataimport?command=delta-import^&clean=false^&commit=true
3. solr 常見的自動更新方式
1. solr 自帶的增量更新更新
1.下載jar包 apache-solr-dataimportscheduler.jar、solr-dataimporthandler-7.0.0.jar、solr-dataimporthandler-extras-7.0.0.jar到 solr 項目的\WEB-INF\lib 目錄下
apache-solr-dataimportscheduler 的下載地址 鏈接:https://pan.baidu.com/s/1h4hDmc2KWtO4VPMnDGXSCg 提取碼:qi7n
2. 修改web.xml文件配置監聽,在servlet節點前增加:
<listener> <listener-class> org.apache.solr.handler.dataimport.scheduler.ApplicationListener </listener-class> </listener>
3.在solrHome(存儲solr數據的目錄) 的目錄下創建conf文件夾,創建 dataimport.properties 文件,內容根據實際情況修改,內容如下:
################################################# # # # dataimport scheduler properties # # # ################################################# # tosync or not to sync # 1- active; anything else - inactive # 這里的配置不用修改 syncEnabled=1 # which cores to schedule # ina multi-core environment you can decide which cores you want syncronized # leave empty or comment it out if using single-core deployment # 修改成你所使用的core,我這里是我自定義的core:simple syncCores=active # solr server name or IP address # [defaults to localhost if empty] 這個一般都是localhost不會變 server=localhost # solr server port # [defaults to 80 if empty] # 安裝solr的tomcat端口,如果你使用的是默認的端口,就不用改了,否則改成自己的端口就好了 port=8089 # application name/context # [defaults to current ServletContextListener's context (app) name] # 這里默認不改 webapp=solr # URL params [mandatory] # remainder of URL # 這里改成下面的形式,solr同步數據時請求的鏈接 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=/select?qt=/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
最后重啟solr,在數據庫中添加一條數據,靜等一分鍾,然后query。因為我們設置的是一分鍾監聽一次
2.使用windows 的計划任務進行增量更新
1.在windows開始那里搜索“計划任務”
2.創建一個新的計划任務
3.對計划任務的常規、觸發器、操作 進行設置
4. 啟動計划任務
5.solr.bat 中的內容為(使用curl進行模擬請求,curl 需要下載):
curl http://localhost:8089/solr/active/dataimport?command=delta-import^&clean=false^&commit=true
6.windows 的curl 需要下載配置環境變量(和jdk的差不多),curl下載地址為:鏈接:https://pan.baidu.com/s/1Vvvz8QdQl1BPD5kdjRcsjg 提取碼:t8mt (64位系統的,32位的可以去官網下載)
3.linux下使用 crontab實現增量更新
1.修改crontab的配置文件:vim /etc/crontab
crontab -e */5 * * * * curl http://localhost:8089/solr/active/dataimport?command=delta-import^&clean=false^&commit=true
注:5分鍾執行一次增量索引(可根據實際業務調整時間)