天天看點

2.myql資料導入到solr,并建立solr索引(學習筆記)

要使用solr實作電商網站中商品搜尋。

電商中商品資訊在mysql資料庫中存儲了,将mysql資料庫中資料在solr中建立索引。

需要在solr的schema.xml檔案定義商品field。

2.myql資料導入到solr,并建立solr索引(學習筆記)

在schema.xml中配置域

商品id(pid)

這是商品的主鍵,由于schema檔案中已經有主鍵id了就不需要對它配置了

<field name="id"type="string" indexed="true" stored="true"required="true" multivalued="false"/>

商品名稱:

<field name="product_name"type="text_ik" indexed="true" stored="true" />

商品分類id:

<field name="product_catalog"type="string" indexed="true" stored="true" />

商品分類名稱:

<fieldname="product_catalog_name" type="string"indexed="true" stored="true" />

商品價格:

<field name="product_price"type="float" indexed="true" stored="true" />

商品描述:

<fieldname="product_description" type="text_ik"indexed="true" stored="false" />

商品圖檔:

<field name="product_pic"type="string" indexed="false" stored="true" />

<field name="product_keywords"type="text_ik" indexed="true" stored="false"multivalued="true"/>

<!--

使用複制域、将product_name和product_description

都複制到product_keywords,當搜尋product_keywords的時候

-->

<copyfieldsource="product_name" dest="product_keywords"/>

<copyfieldsource="product_description" dest="product_keywords"/>

schema.xml中配置的域的内容如下:

<!-- 商品名稱 -->

<field name="product_name" type="text_ik" indexed="true" stored="true" />

<!-- 商品分類id -->

<field name="product_catalog" type="string" indexed="true" stored="true" />

<!-- 商品分類名稱 -->

<field name="product_catalog_name" type="string" indexed="true" stored="true" />

<!-- 商品價格 -->

<field name="product_price" type="float" indexed="true" stored="true" />

<!-- 商品描述 -->

<field name="product_description" type="text_ik" indexed="true" stored="false" />

<!-- 商品圖檔 -->

<field name="product_pic" type="string" indexed="false" stored="true" />

<field name="product_keywords" type="text_ik" indexed="true" stored="false" multivalued="true"/>

         <!--

         使用複制域、将product_name和product_description

         都複制到product_keywords,當搜尋product_keywords的時候

         -->

         <copyfield source="product_name" dest="product_keywords"/>

         <copyfield source="product_description" dest="product_keywords"/>

dataimporthandler,它可以把資料從關系資料庫中查詢出來,然後倒入到索引庫中。

l  dataimport的jar

從d:\installed\solr-4.10.3\dist目錄下拷貝solr-dataimporthandler-4.10.3.jar和solr-dataimporthandler-extras-4.10.3.jar,複制到d:\installed\solr-resources\contrib\dataimporthandler\lib目錄:

2.myql資料導入到solr,并建立solr索引(學習筆記)

修改solrconfig.xml如下:

2.myql資料導入到solr,并建立solr索引(學習筆記)

即:<libdir="${solr.install.dir:../..}/contrib/dataimporthandler/lib"regex=".*\.jar"/>

l  資料庫驅動包

把mysql資料庫驅動包,拷貝到以下目錄:

2.myql資料導入到solr,并建立solr索引(學習筆記)

修改solrconfig.xml,如下:

2.myql資料導入到solr,并建立solr索引(學習筆記)

<libdir="${solr.install.dir:../..}/contrib/db/lib"regex=".*\.jar"/>

在solrconfig.xml檔案中配置dataimport請求url,如下資訊:

2.myql資料導入到solr,并建立solr索引(學習筆記)

配置的代碼如下:

<requesthandler name="/dataimport" class="org.apache.solr.handler.dataimport.dataimporthandler">

    <lst name="defaults">

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

    </lst>

</requesthandler>

在solrcore中conf目錄下,建立一個檔案:data-config.xml

2.myql資料導入到solr,并建立solr索引(學習筆記)

具體内容如下:

<?xml version="1.0" encoding="utf-8" ?> 

<dataconfig>  

         <datasource type="jdbcdatasource"  

                     driver="com.mysql.jdbc.driver"  

                     url="jdbc:mysql://localhost:3306/solr"

                     user="root"  

                     password="123456"/>

         <document>  

                   <entity name="product" query="select pid,name,catalog,catalog_name,price,description,picture from products">

                             <field column="pid" name="id"/>

                             <field column="name" name="product_name"/>

                             <field column="catalog" name="product_catalog"/>

                             <field column="catalog_name" name="product_catalog_name"/>

                             <field column="price" name="product_price"/>

                             <field column="description" name="product_description"/>

                             <field column="picture" name="product_picture"/>

                   </entity>  

         </document>

</dataconfig>

重新開機之後,先execute,然後再refresh.

2.myql資料導入到solr,并建立solr索引(學習筆記)

注意:到入資料前會先清空索引庫,然後再導入。

也就是說删除solrcore下面的data目錄。