solr链接数据库

来源:互联网 发布:纸质读书笔记 知乎 编辑:程序博客网 时间:2024/05/29 10:56

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就可以导入数据

原创粉丝点击