编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

搜索引擎(八)中文分词IKAnalyzer的配置和使用

wxchong 2024-10-23 15:49:21 开源技术 7 ℃ 0 评论

前面讲了Solr搜索引擎的使用和系统架构,今天要来说一说搜索引擎中重要的部分:中文分词。

有的时候,用户搜索的关键字,可能是一个词语,也可能是一句话,不是很规范。那么怎么识别出来用户真正想要查询的是什么呢?怎么在一句话中找出关键的词呢? 这里就需要用到中文分词,将用户输入的关键字进行分词。

目前有很多优秀的中文分词组件。本篇就以 IKAnalyzer 分词为例,讲解如何在 solr 中及集成中文分词,使用 IKAnalyzer的原因 IK 比其他中文分词维护的勤快,和 Solr 集成也相对容易。具体就不多介绍,这里直接solr 集成 IK 的方法。


1、下载IKAnalyzer

下载地址:http://files.cnblogs.com/files/zhangweizhong/ikanalyzer-solr5.zip

注意:以前老的IK 不支持Solr 5.3的版本 ,请注意下载最新的。


2、集成到Solr系统

将下载下来的IKAnalyzer 解压,然后拷贝到 webapps\solr\WEB-INF\lib 目录下。


3、增加自定义字段类型

增加自定义的fildType 字段类型,该字段类型使用IK解析,具体配置如下:

在 solr_home\mycore1\conf\schema.xml 增加如下配置

         <!-- 我添加的IK分词 -->
         <fieldType name="text_ik" class="solr.TextField">   
                   <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>   
                   <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>   
         </fieldType>

同时,把需要分词的字段,设置为text_ik,

  <field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="name" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="title" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="category" type="int" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="content" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="price" type="double" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="color" type="string" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="orderBy" type="int" indexed="true" stored="true" required="true" multiValued="false" />
   <field name="updatetime" type="date" indexed="true" stored="true" required="true" multiValued="false" />


4、重启服务

注意:如果之前已经创建了索引,需要将之前的索引删掉,重新创建分词后的索引。


5、在admin后台, analysis 下查看分词效果

1. 中文分词效果

    

2. 索引查询效果

    


6、配置IKAnalyzer分词器的扩展词典,停止词词典

1. 将 文件夹下的IKAnalyzer.cfg.xml , ext.dic和stopword.dic 三个文件 复制到/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_dict">ext.dic;</entry> 

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

2. 在ext.dic 里增加自己的扩展词典,例如,婴儿奶粉3段

     


注意: 记得将stopword.dic,ext.dic的编码方式为UTF-8 无BOM的编码方式。


最后

以上就把solr中文分词介绍完了,如有不足或不正确之处,欢迎大家批评指正,相互交流。

这个系列课程的完整源码,也会提供给大家。大家关注我的头条号(章为忠学架构)。获取这个系列课程的完整源码。


推荐阅读:

搜索引擎(七)高可用的solr搜索引擎服务架构

搜索引擎(六)SolrNet的复杂查询,分页,高亮,Facet查询

搜索引擎(五)SolrNet的基本用法及CURD

搜索引擎(四)Solr的API接口及查询参数

搜索引擎Solr(三)熟悉Solr管理后台

搜索引擎Solr(一)Solr服务器的安装与配置

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表