這裏使用的是mysql測試。html
一、先在mysql中建一個表:solr_testjava
二、插入幾條測試數據:mysql
三、用記事本打solrconfig.xml文件,在solrhome文件夾中。E:\solrhome\mycore\conf\solrconfig.xmlweb
(solrhome文件夾是什麼,參見:http://www.cnblogs.com/HD/p/3977799.html)sql
加入這個節點:數據庫
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
四、新建一個data-config.xml文件,與solrconfig.xml同一個目錄下。內容爲apache
<dataConfig> <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/test" user="root" password="root" /> <document> <entity name="solr_test" transformer="DateFormatTransformer" query="SELECT id, subject, content, last_update_time FROM solr_test WHERE id >= ${dataimporter.request.id}"> <field column='last_update_time' dateTimeFormat='yyyy-MM-dd HH:mm:ss' /> </entity> </document> </dataConfig>
說明:這裏使用了一個${dataimporter.request.id},這個是參數,後面在作數據導入時,會使用到,以此條件爲基準讀數據。json
五、複製解壓出的solr jar包solr-dataimporthandler-4.10.0.jar和solr-dataimporthandler-extras-4.10.0.jar到tomcat solr webapp的WEB-INF\lib目錄下。tomcat
固然,也包括mysql的jdbc jar包:mysql-connector-java-5.1.7-bin.jarapp
(還有一種方法是在solrconfig.xml中加入lib節點,而後把jar包放到solrhome下,這樣能夠不在WEB-INF\lib中加入jar包)
六、用記事本打開schema.xml,在在solrhome文件夾中(同第3點)。內容爲:
<?xml version="1.0" ?> <schema name="my core" version="1.1"> <fieldtype name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/> <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/> <fieldType name="text_cn" class="solr.TextField"> <analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer" /> <analyzer type="query" class="org.wltea.analyzer.lucene.IKAnalyzer" /> </fieldType> <!-- general --> <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/> <field name="subject" type="text_cn" indexed="true" stored="true" /> <field name="content" type="text_cn" indexed="true" stored="true" /> <field name="last_update_time" type="date" indexed="true" stored="true" /> <field name="_version_" type="long" indexed="true" stored="true"/> <!-- field to use to determine and enforce document uniqueness. --> <uniqueKey>id</uniqueKey> <!-- field for the QueryParser to use when an explicit fieldname is absent --> <defaultSearchField>subject</defaultSearchField> <!-- SolrQueryParser configuration: defaultOperator="AND|OR" --> <solrQueryParser defaultOperator="OR"/> </schema>
七、打開solr web:
說明:
Custom Parameters填入id=1,這是在第4點中設置的參數。
Clean選項,是指是否刪除未匹配到的數據。也就是在數據庫select結果中沒有,而solr索引庫中存在,則刪除。
也能夠使用這個地址直接訪問:
http://localhost:8899/solr/mycore/dataimport?command=full-import&clean=true&commit=true&wt=json&indent=true&entity=solr_test&verbose=false&optimize=false&debug=false&id=1
將返回結果:
配置好後,以後咱們只須要使用這個url地址,就能夠不段的去導入數據作索引了。(就這麼簡單)
八、測試查詢:
固然,dataimport能夠加入參數命令,讓其從新加載data-config.xml
http://localhost:8899/solr/#/mycore/dataimport/command=reload-config