2.myql数据导入到solr,并建立solr索引(学习笔记)
1.1?????業務域名的配置
1.1.1?? 需求
要使用solr實現電商網站中商品搜索。
電商中商品信息在mysql數據庫中存儲了,將mysql數據庫中數據在solr中創建索引。
需要在solr的schema.xml文件定義商品Field。
1.1.2?? 定義步驟
在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"/> |
?
1.2?????DataimportHandler
DataimportHandler,它可以把數據從關系數據庫中查詢出來,然后倒入到索引庫中。
1.2.1?? 添加jar包
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目錄:
修改solrconfig.xml如下:
即:<libdir="${solr.install.dir:../..}/contrib/dataimporthandler/lib"regex=".*\.jar"/>
l? 數據庫驅動包
把mysql數據庫驅動包,拷貝到以下目錄:
修改solrconfig.xml,如下:
<libdir="${solr.install.dir:../..}/contrib/db/lib"regex=".*\.jar"/>
?
1.2.2?? 配置dataimportHandler
在solrconfig.xml文件中配置dataimport請求url,如下信息:
配置的代碼如下:
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> ??? <lst name="defaults"> ????? <str name="config">data-config.xml</str> ??? </lst> </requestHandler> |
?
1.2.3?? 創建并配置data-config.xml
在SolrCore中conf目錄下,創建一個文件:data-config.xml
具體內容如下:
<?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> |
?
1.2.4?? 啟動Tomcat進行測試
重啟之后,先Execute,然后再refresh.
注意:到入數據前會先清空索引庫,然后再導入。
也就是說刪除solrCore下面的data目錄。
?
?
?
總結
以上是生活随笔為你收集整理的2.myql数据导入到solr,并建立solr索引(学习笔记)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1、solr包结构介绍,solrCore
- 下一篇: solrj的使用,环境准备,工程搭建,索