[solr] - 數據庫導入


這里使用的是mysql測試。

1、先在mysql中建一個表:solr_test

 

 

2、插入幾條測試數據:

 

3、用記事本打solrconfig.xml文件,在solrhome文件夾中。E:\solrhome\mycore\conf\solrconfig.xml

(solrhome文件夾是什么,參見:http://www.cnblogs.com/HD/p/3977799.html)

加入這個節點:

    <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
        <lst name="defaults">
            <str name="config">data-config.xml</str>
        </lst>
    </requestHandler>

 

4、新建一個data-config.xml文件,與solrconfig.xml同一個目錄下。內容為

<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},這個是參數,后面在做數據導入時,會使用到,以此條件為基准讀數據。

 

5、復制解壓出的solr jar包solr-dataimporthandler-4.10.0.jar和solr-dataimporthandler-extras-4.10.0.jar到tomcat solr webapp的WEB-INF\lib目錄下。

當然,也包括mysql的jdbc jar包:mysql-connector-java-5.1.7-bin.jar

(還有一種方法是在solrconfig.xml中加入lib節點,然后把jar包放到solrhome下,這樣可以不在WEB-INF\lib中加入jar包)

 

6、用記事本打開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>

 

7、打開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地址,就可以不段的去導入數據做索引了。(就這么簡單)

 

8、測試查詢:


 

 

當然,dataimport可以加入參數命令,讓其重新加載data-config.xml

http://localhost:8899/solr/#/mycore/dataimport/command=reload-config

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM