solr鏈接數據庫

1. 向D:\Solr\home\conf\solrconfig.xml添加以下內容

<requestHandlername="/dataimport"class="org.apache.solr.handler.dataimport.DataImportHandler">  

    <lstname="defaults">  

          <strname="config">data-config.xml</str>  

    </lst> 

</requestHandler> 

 

2.在微軟的網站上下載JDBC for sqlserver,http://www.microsoft.com/zh-CN/download/details.aspx?id=11774

將其中的sqljdbc.jar或sqljdbc4.jar包放到web-inf\lib目錄中

 

3.在相同目錄下建立data-config.xml添加以下內容

<?xml version="1.0" encoding="UTF-8"?> 
<dataConfig> 
    <dataSource type="JdbcDataSource" driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=testDB" user="sa" password="sa"/>
    <document name="Goods">
        <entity name="Goods" query="select * from View_GoodsInfo" transformer="ClobTransformer">
            <field column="Goods_ID" name="Goods_ID" />
            <field column="Goods_Name" name="Goods_Name" />
            <field column="Goods_TypeID" name="Goods_TypeID" />
            <field column="Goods_TypeName" name="Goods_TypeName" />
            <field column="Goods_Introduction" name="Goods_Introduction" clob="true" />
        </entity>
    </document>
</dataConfig> 

解釋一下clob,Character Large Object,字符大對象,比如sql server中的text

注:如果連接默認實例,那麼一定要寫上端口號。如果是命名實例,一定不要寫上端口號,jdbc:sqlserver://localhost;instanceName=testName; DatabaseName=testDB""sa","sa"或者

jdbc:sqlserver://localhost//testName; DatabaseName=testDB""sa","sa"

4.在schema.xml的fields節點加入以下內容

<field name="Goods_ID" type="text" indexed="true" stored="true"/>
    <field name="Goods_Name" type="text" indexed="true" stored="true"/>
    <field name="Goods_TypeID" type="text" indexed="true" stored="true"/>
    <field name="Goods_TypeName" type="text" indexed="true" stored="true"/>
    <field name="Goods_Introduction" type="text" indexed="true" stored="true"/>

 

Fields 字段 :  結點內定義具體的字段(類似數據庫中的字段) , 就是 field ,

name: mandatory - the name for the field
     type: mandatory - the name of a previously defined type from the 
       <types> section
     indexed: true if this field should be indexed (searchable or sortable)
     stored: true if this field should be retrievable
     multiValued: true if this field may contain multiple values per document
     omitNorms: (expert) set to true to omit the norms associated with
       this field (this disables length normalization and index-time
       boosting for the field, and saves some memory).  Only full-text
       fields or fields that need an index-time boost need norms.
       Norms are omitted for primitive (non-analyzed) types by default.
     termVectors: [false] set to true to store the term vector for a
       given field.
       When using MoreLikeThis, fields used for similarity should be
       stored for best performance.
     termPositions: Store position information with the term vector.  
       This will increase storage costs.
     termOffsets: Store offset information with the term vector. This 
       will increase storage costs.
     default: a value that should be used if no value is specified
       when adding a document.

copeField(賦值字段): 建立一個拷貝字段 , 將所有的全文字段複製到一個字段中 , 以便進行統一的檢索 。


copyField commands copy one field to another at the time a document
        is added to the index.  It's used either to index the same field differently,
        or to add multiple fields to the same field for easier/faster searching. 


dynamicField:

Dynamic field definitions.  If a field name is not found, dynamicFields
        will be used if the name matches any of the patterns.
        RESTRICTION: the glob-like pattern in the name attribute must have
        a "*" only at the start or the end.
        EXAMPLE:  name="*_i" will match any field ending in _i (like myid_i, z_i)
        Longer patterns will be matched first.  if equal size patterns
        both match, the first appearing in the schema will be used.


5. 將解壓的solr文件夾中dist 目錄下的  apache-solr-dataimporthandler-3.6.1.jar 和 apache-solr-dataimporthandler-extras-3.6.1.jar複製到  D:\apache-tomcat-6.0.29\webapps\solr\WEB-INF\lib

 

6.訪問http://localhost:8080/solr/dataimport?command=full-import就可以導入數據

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章