要使用solr实现电商网站中商品搜索。
电商中商品信息在mysql数据库中存储了,将mysql数据库中数据在solr中创建索引。
需要在solr的schema.xml文件定义商品field。
在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目录:
修改solrconfig.xml如下:
即:<libdir="${solr.install.dir:../..}/contrib/dataimporthandler/lib"regex=".*\.jar"/>
l 数据库驱动包
把mysql数据库驱动包,拷贝到以下目录:
修改solrconfig.xml,如下:
<libdir="${solr.install.dir:../..}/contrib/db/lib"regex=".*\.jar"/>
在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>
在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>
重启之后,先execute,然后再refresh.
注意:到入数据前会先清空索引库,然后再导入。
也就是说删除solrcore下面的data目录。