標籤:jdbc psi 原理 from pre 欄位 upd 啟動 sea
首先在solrconfig.xml(我的是儲存在/usr/local/tomcat/solrhome/mycore/conf/下)的<requestHandler name="/select" class="solr.SearchHandler">之上添加
(我的本地的solrconfig.xml中有這個/dtatimport 只需把class修改為下面的就可以了)
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
然後在conf的solr-data-config.xml檔案。裡面內容如下:
<dataConfig> <dataSource name="source1" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/user(資料庫名)" user="root" password="root" batchSize="-1" /> <document> <entity name="news" pk="id" dataSource="source1" query="select * from news" deltaImportQuery="select * from news where id=‘${dih.delta.id}‘" deltaQuery="select id from news where updateTime> ‘${dataimporter.last_index_time}‘"> <field column="id" name="id"/> <field column="title" name="title"/> <field column="synopsis" name="synopsis"/> <field column="updateTime" name="updateTime"/> </entity> </document> </dataConfig>
dataSource是資料庫資料來源。Entity就是一張表對應的實體,pk是主鍵,query是查詢語句。Field對應一個欄位,column是資料庫裡的column名,後面的name屬性對應著Solr的Filed的名字。其中user是資料庫名,news是表名。
其中deltaQuery是增量索引,原理是從資料庫中根據deltaQuery指定的SQL語句查詢出所有需要增量匯入的資料的ID號。然後根據deltaImportQuery指定的SQL語句返回所有這些ID的資料,即為這次增量匯入所要處理的資料。核心思想是:通過內建變數“${dih.delta.id}”和 “${dataimporter.last_index_time}”來記錄本次要索引的id和最近一次索引的時間。
然後把mysql所需的jar包(去下載相應的jar包)和solr-6.2.0\dist下的solr-dataimporthandler-6.2.0.jar和solr-dataimporthandler-extras-6.2.0.jar都複製到項目WEB-INF\lib下(該步驟在安裝solr的時候已做)
啟動Tomcat,輸入http://localhost:8080/solr/index.html按如下選擇,
Solr6.5與mysql整合建立索引