solr(三) : 导入数据库表数据

solr 除了能查询文档中的数据外, 还可以导入数据库中的数据. 

也就是说, solr 能查询其他数据库中的数据(solr本身也是一个数据库, 非关系型的).

那接下来就试一下导入mysql数据库中的数据.

一. 准备工作

1. 在solr_core下面新建lib文件夹. 然后将以下jar包拷贝进去

2. 修改 solrconfig.xml 文件

在文档的底部加入:

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

3. 在同级目录下, 新建 data-config.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource" 
            driver="com.mysql.jdbc.Driver" 
            url="jdbc:mysql://localhost:3306/test"
            user="root"
            password="root"/>
    <document>
        <entity name="tb_item" query="select id,title,sell_point,price,num,image from tb_item ">
            <field column="id" name="id" />
            <field column="title" name="product_title" />
            <field column="sell_point" name="product_sell_point" />
            <field column="price" name="product_price" />
            <field column="num" name="product_num" />            
            <field column="image" name="product_image" />
        </entity>
    </document>
</dataConfig>

在文件中配置, 数据库连接信息, 要查询的表字段映射配置

由于映射出来的字段, 是我自命名的, 在solr原来的域中, 可能并不存在, 所以, 要对我自定义的域进行配置

4. 配置managed-schema.xml

在文档的底部加上

    <!--tb_item 表-->
    <field name="product_title" type="text_ik_type" indexed="true" stored="true"/>
    <field name="product_sell_point" type="text_ik_type" indexed="true" stored="false"/>
    <field name="product_price" type="long" indexed="true" stored="true"/>
    <field name="product_num" type="int" indexed="true" stored="true"/>
    <field name="product_image" type="string" indexed="false" stored="true"/>
  
    <field name="product_keywords" type="text_ik_type" indexed="true" stored="false" multiValued="true"/>
    <copyField source="product_title" dest="product_keywords" />
    <copyField source="product_sell_point" dest="product_keywords" />

copyField : 将product_title, product_sell_point域拷贝到新域 product_keywords中. 方便在一次连接中查询多个域

 

二. 导数据

 

三. 查询结果

 从高亮的地方, 就可以看出这功能和之前Baidu的功能非常的像吧, 嘿嘿

posted @ 2017-12-28 22:10  Sniper_ZL  阅读(1355)  评论(2编辑  收藏  举报