前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >(三)solr的dataimport的配置以及中文分词

(三)solr的dataimport的配置以及中文分词

作者头像
qubianzhong
发布2018-09-19 13:37:14
1.1K0
发布2018-09-19 13:37:14
举报
文章被收录于专栏:行者常至行者常至行者常至
1、先来个建表文件products.sql(mysql)链接:http://pan.baidu.com/s/1o8wGwuI 密码:wm8s
2、在solr_home\solr\firstCore\conf下的solrconfig.xml中新增标签
  <!-- 添加dataimporthandler标签 -->
  <lib dir="${solr.install.dir:../../../..}/contrib/dataimporthandler/lib" regex=".*\.jar" />
  <!-- 添加mysql驱动标签 -->
  <lib dir="${solr.install.dir:../../../..}/contrib/db/lib" regex=".*\.jar" />
这里写图片描述
这里写图片描述
3、把solr-5.0.0\dist下的solr-dataimporthandler-5.0.0.jar拷贝进solr_home\contrib\dataimporthandler\lib下
这里写图片描述
这里写图片描述
把mysql-connector-java-5.1.12-bin.jar(可从上面网盘里下载获取官网下载)拷贝到solr_home\contrib\db\lib下
这里写图片描述
这里写图片描述
4、在solr_home\solr\firstCore\conf下的schema.xml中添加域字段
   <!-- 商品名称 -->
   <field name="product_name" type="string" 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="false"/>

   <!-- 商品价格 -->
   <field name="product_price" type="float" indexed="true" stored="true"/>

   <!-- 商品描述 -->
   <field name="product_description" type="string" indexed="true" stored="false"/>

   <!-- 商品图片地址 -->
   <field name="product_picture" type="string" indexed="true" stored="true"/>
   <!-- 目标域 -->
   <field name="product_keywords" type="string" indexed="true" stored="true" multiValued="true"/>

   <!-- 将商品名称添加到目标域 -->
   <copyField source="product_name" dest="product_keywords" />

   <!-- 将商品描述添加到目标域 -->
   <copyField source="product_description" dest="product_keywords" />
在稍后的中文分词中,我们会把product_name、product_description的type更改为我们自定义的中文分词器的fieldType。
5、在solr_home\solr\firstCore\conf下的solrconfig.xml中新增dataimport的requestHandler
  <!-- 配置dataimport的requestHandler -->
  <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">data-config.xml</str>
    </lst>
  </requestHandler>
这里写图片描述
这里写图片描述
6、在solr_home\solr\firstCore\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/suimi"
    user="root"
    password="qbz"
  />
<!-- 上面的数据库名称和用户名密码根据自己的实际来填写 -->
  <document>
    <entity name="product" query="SELECT pid,name,catalog_name,catalog,price,description,picture FROM products">
        <field column="pid" name="id"/>
        <field column="name" name="product_name"/>
        <field column="catalog_name" name="product_catalog_name"/>
        <field column="catalog" name="product_catalog"/>
        <field column="price" name="product_price"/>
        <field column="description" name="product_description"/>
        <field column="picture" name="product_picture"/>
        <!-- field中的column的值必须与数据库中的字段一致;name必须是schema.xml中存在的field字段(除了主键ID外,其他的都是我们刚才创建的) -->
    </entity>
  </document>

</dataConfig>
7、启动tomcat,输入网址http://localhost:8080/solr
可以在Analysis中看到自定义的field
这里写图片描述
这里写图片描述
进而执行Dataimport动作
这里写图片描述
这里写图片描述
8、全部导入后,点击Query,然后执行查询可查到数据库表中的所有记录
这里写图片描述
这里写图片描述
9、此时我们在q中输入 product_name:小黄人 是搜不到任何东西的
这里写图片描述
这里写图片描述
因为我们没有分词,接下来配置中文分词
在solr_home\solr\firstCore\conf下的schema.xml中添加
    <!-- 配置中文分词的FieldType -->
    <fieldType name="text_ik" class="solr.TextField" >

      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"></analyzer>

    </fieldType>

    <!-- 配置中文分词的Field -->
    <field name="content_ik" type="text_ik" indexed="true" stored="true"/>
并更改
<!-- 商品名称 -->
   <field name="product_name" type="text_ik" indexed="true" stored="true" />
   <!-- 商品描述 -->
   <field name="product_description" type="text_ik" indexed="true" stored="false"/>
   <!-- 目标域 -->
   <field name="product_keywords" type="text_ik" indexed="true" stored="true" multiValued="true"/>
将ik-analyzer-solr5-5.x.jar拷贝到:apache-tomcat-8.5.8\webapps\solr\WEB-INF\lib
这里写图片描述
这里写图片描述
在apache-tomcat-7.0.73\webapps\solr\WEB-INF\classes下新建文件IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">  
<properties>  
    <comment>IK Analyzer 扩展配置</comment>


    <!--用户可以在这里配置自己的扩展停止词字典-->
    <entry key="ext_stopwords">stopword.dic;</entry> 

    <!--用户可以在这里配置自己的扩展字典 ,多个词典用分号隔开
        <entry key="ext_dict">mydict.dic;</entry> 
    -->
    <entry key="ext_dict">moren.dic;qubianzhong.dic;</entry> 
</properties>

并可以在apache-tomcat-7.0.73\webapps\solr\WEB-INF\classes下新建自己的扩展词典

这里写图片描述
这里写图片描述
Q:未完待续(表述的还有些问题)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年12月02日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、先来个建表文件products.sql(mysql)链接:http://pan.baidu.com/s/1o8wGwuI 密码:wm8s
  • 2、在solr_home\solr\firstCore\conf下的solrconfig.xml中新增标签
  • 3、把solr-5.0.0\dist下的solr-dataimporthandler-5.0.0.jar拷贝进solr_home\contrib\dataimporthandler\lib下
  • 把mysql-connector-java-5.1.12-bin.jar(可从上面网盘里下载获取官网下载)拷贝到solr_home\contrib\db\lib下
  • 4、在solr_home\solr\firstCore\conf下的schema.xml中添加域字段
  • 在稍后的中文分词中,我们会把product_name、product_description的type更改为我们自定义的中文分词器的fieldType。
  • 5、在solr_home\solr\firstCore\conf下的solrconfig.xml中新增dataimport的requestHandler
  • 6、在solr_home\solr\firstCore\conf下新增文件data-config.xml
  • 7、启动tomcat,输入网址http://localhost:8080/solr
  • 可以在Analysis中看到自定义的field
  • 进而执行Dataimport动作
  • 8、全部导入后,点击Query,然后执行查询可查到数据库表中的所有记录
  • 9、此时我们在q中输入 product_name:小黄人 是搜不到任何东西的
  • 因为我们没有分词,接下来配置中文分词
  • 在solr_home\solr\firstCore\conf下的schema.xml中添加
  • 并更改
  • 将ik-analyzer-solr5-5.x.jar拷贝到:apache-tomcat-8.5.8\webapps\solr\WEB-INF\lib
  • 在apache-tomcat-7.0.73\webapps\solr\WEB-INF\classes下新建文件IKAnalyzer.cfg.xml
  • Q:未完待续(表述的还有些问题)
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档