文章出處

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的功能非常的像吧, 嘿嘿


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()