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

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 1.1     业务域名的配置1.1.1   需求要使用solr实现电商网站中商品搜索。电商中商品信息在mysql数据库中存储了,将mysql数据库中数据在solr中创建索引。需要在solr的schema.xml文件定义商品Field。1.1.2   定义步骤在schema.xml中配置域 商品id(pid)这是商品的主键,由于schema文件中已经有主键id了就不需要

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

 

 

 

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4月前
|
自然语言处理 搜索推荐 算法
数据库-Elasticsearch进阶学习笔记(分片、映射、分词器、即时搜索、全文搜索等)
数据库-Elasticsearch进阶学习笔记(分片、映射、分词器、即时搜索、全文搜索等)
141 0
|
6月前
|
索引
18Solr管理索引库
18Solr管理索引库
14 0
|
6月前
|
Java 关系型数据库 MySQL
21Solr批量导入数据
21Solr批量导入数据
23 0
|
关系型数据库 MySQL Java
Solr从myslq批量导入数据
Solr从myslq批量导入数据
64 0
Solr从myslq批量导入数据
|
XML 缓存 自然语言处理
Solr 的作用,为什么要用solr服务,
Solr 的作用,为什么要用solr服务,
241 0
|
数据可视化 关系型数据库 MySQL
Solr 中 core 创建后的数据导入
Solr 中 core 创建后的数据导入
148 0
Solr 中 core 创建后的数据导入
|
Java 关系型数据库 MySQL
将MySQL中的数据导入到solr索引库
利用solrJ向索引库导入数据http://www.bieryun.com/3229.html 需求:将MySQL中的数据导入到solr索引库 定义实体类: [java] view plain copy public class SearchItem implements Seri.
1572 0
|
分布式数据库 索引 Hbase
使用elasticsearch提高hbase基于列的查询效率
使用elasticsearch提高hbase基于列的查询效率 网上能查到的hbase提高基于列的查询效率基本上是建立二级索引的方法,介绍另外一种方法,使用分布式索引技术elasticsearch来提高效率, 基本思路和二级索引差不多,都是通过对要查询的列建立索引,先根据建立的列索引查询到rowkey,再根据rowkey查询到需要的数据,步骤如下: 1.
4324 0
|
关系型数据库 Java 数据库

热门文章

最新文章