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

来源:互联网 发布:网络主播大揭秘工资 编辑:程序博客网 时间:2024/04/30 04:41

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:未完待续(表述的还有些问题)

0 0
原创粉丝点击