2.myql数据导入到solr,并建立solr索引(学习笔记)

来源:互联网 发布:手机淘宝app流量软件 编辑:程序博客网 时间:2024/05/18 00:05

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目录。

 

 

 

0 0
原创粉丝点击