相關文章:第一篇要是安裝部署,第二篇如何定時同步mysql數據。第三篇solr實戰關鍵字查詢全庫
1.導入jar包。將目錄E:\solr\test\solr-5.5.4\dist下的兩個jar包,solr-dataimporthandler-5.5.4.jar solr-dataimporthandler-extras-5.5.4.jar ,還又mysql驅動的jar導入E:\solr\test\apache-tomcat-8.5.20\webapps\solr\WEB-INF\lib下。
2.復制E:\solr\test\solr-5.5.4\server\solr\configsets\data_driven_schema_configs\conf,放到
3.E:\solr\test\solrhome\core1下,編輯E:\solr\test\solrhome\core1\conf\solrconfig.xml
添加<lib dir="../webapps/solr/WEB-INF/lib" regex=".*\.jar" /> ,位置如圖,相對路徑,相對的是tomcat的bin路徑,和上面一樣的道理。然后搜索class="solr.SearchHandler",在此句上面添加
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
如圖

4.在和solrconfig.xml的同目錄項目添加文件data-config.xml
內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource name="source1" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solrdata" user="root" password="root" batchSize="-1" />
<document>
<entity name="goods" pk="id" dataSource="source1"
query="select * from goods"
deltaImportQuery="select * from goods where id='${dih.delta.id}'"
deltaQuery="select id from goods where updatetime> '${dataimporter.last_index_time}'">
<field column="id" name="id"/>
<field column="name" name="name"/>
<field column="number" name="number"/>
<field column="updatetime" name="updatetime"/>
</entity>
</document>
</dataConfig>
其中deltaImportQuery,deltaQuery設置的內容是自動更新mysql數據到solr引擎中來所需要的。如果多個表再並列添加一個<entity 標簽。
最后注釋:否則后面可能啟動報錯
solrconfig.xml配置文件注釋掉下面:
<searchComponent name="elevator">
<!-- pick a fieldType to analyze queries -->
<str name="queryFieldType">string</str>
<str name="config-file">elevate.xml</str>
</searchComponent>
5.修改managed-schema(在同目錄下面的)添加的內容如下圖

內容為:
<field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="false"/>
<field name="number" type="int" indexed="true" stored="false"/>
<field name="updatetime" type="date" indexed="true" stored="true" />
6.在solrhome文件夾里面創建conf文件,在conf里面創建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
# 修改成你所使用的core,我這里是我自定義了多個core,用逗號隔開
syncCores=testcore,big,core1
# 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=8080
# 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
7.然后在E:\solr\test\apache-tomcat-8.5.20\webapps\solr\WEB-INF\lib添加
apache-solr-dataimportscheduler-1.1.jar,或者dataimportscheduler-1.2.jar,這是是別人修改過的。
下載鏈接:http://download.csdn.net/download/qq_14926389/9937657,有幣的送幾個,沒有的q我吧
8.在數據庫中添加表,如下圖

alter table goods modify updatetime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,然后在客戶端執行這句話,用戶自動更新判別條件
然后重啟tomcat,訪問solr
說一solr 的操作,一半重啟之后要在solr中重新加載一下配置文件
如下圖,

最后這些qurey。點擊去又一個excute的按鈕,查詢所有,得到如下結果,core1的數據11條,當你去插入一條數據,updatetime的數據設置為當前時間,重新執行查詢,回發現多了一條數據

以下我的qq 1195748576
