elasticsearch中文分词器IK的使用

IK中文分词器的安装

es中文分词器IK的使用

  1. 添加一个索引库
    PUT /test
  2. 利用该索引库进行分词测试
    GET /test/_analyze
{
    "analyzer": "ik_smart",
    "text": "我爱你中国"
}

分词结果

{
    "tokens": [
        {
            "token": "我爱你",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "中国",
            "start_offset": 3,
            "end_offset": 5,
            "type": "CN_WORD",
            "position": 1
        }
    ]
}

可以看到比es中默认的标准分词器standard对中文分词要好很多
3. 自定义词库
POST /test/analyze

{
    "analyzer" : "ik_smart" ,
    "text" :"公众号博思奥园"
}

默认情况下没有我们自定义的词库,它会将博思奥园拆分开,如果我们不想将它拆开,我们可以自定义词库

{
    "tokens": [
        {
            "token": "公众",
            "start_offset": 0,
            "end_offset": 2,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "号",
            "start_offset": 2,
            "end_offset": 3,
            "type": "CN_CHAR",
            "position": 1
        },
        {
            "token": "博",
            "start_offset": 3,
            "end_offset": 4,
            "type": "CN_CHAR",
            "position": 2
        },
        {
            "token": "思",
            "start_offset": 4,
            "end_offset": 5,
            "type": "CN_CHAR",
            "position": 3
        },
        {
            "token": "奥",
            "start_offset": 5,
            "end_offset": 6,
            "type": "CN_CHAR",
            "position": 4
        },
        {
            "token": "园",
            "start_offset": 6,
            "end_offset": 7,
            "type": "CN_CHAR",
            "position": 5
        }
    ]
}
  • 在ik/config目录下新建一个词典文件,myext.dic, 加入自己所需的词语
    公众号
    博思奥园
  • 在ik/config/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">myext.dic</entry>
	 <!--用户可以在这里配置自己的扩展停止词字典-->
	<entry key="ext_stopwords"></entry>
	<!--用户可以在这里配置远程扩展字典 -->
	<!-- <entry key="remote_ext_dict">words_location</entry> -->
	<!--用户可以在这里配置远程扩展停止词字典-->
	<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

重启es服务,在此访问接口

{
    "tokens": [
        {
            "token": "公众号",
            "start_offset": 0,
            "end_offset": 3,
            "type": "CN_WORD",
            "position": 0
        },
        {
            "token": "博思奥园",
            "start_offset": 3,
            "end_offset": 7,
            "type": "CN_WORD",
            "position": 1
        }
    ]
}

已经按照我们的配置进行了分词

  1. 拓展
    使用热更新 IK 分词
    目前该插件支持热更新 IK 分词,通过上文在 IK 配置文件中提到的如下配置
<!--用户可以在这里配置远程扩展字典 -->
<entry key="remote_ext_dict">location</entry>
<!--用户可以在这里配置远程扩展停止词字典-->
<entry key="remote_ext_stopwords">location</entry>

其中 location 是指一个 url,比如 http://localhost:8080/myext.dic,该请求只需满足以下两点即可完成分词热更新。
该 http 请求需要返回两个头部(header),一个是 Last-Modified,一个是 ETag,这两者都是字符串类型,只要有一个发生变化,该插件就会去抓取新的分词进而更新词库。
该 http 请求返回的内容格式是一行一个分词,换行符用 \n 即可。
满足上面两点要求就可以实现热更新分词了,不需要重启 ES 实例。
可以将需自动更新的热词放在一个 UTF-8 编码的 .txt 文件里,放在 nginx 或其他简易 http server 下,当 .txt 文件修改时,http server 会在客户端请求该文件时自动返回相应的 Last-Modified 和 ETag。可以另外做一个工具来从业务系统提取相关词汇,并更新这个 .txt 文件。

  1. 对比
    es也有自己自带的默认分词器standard,我们可以将es自带的分词器进行对比
    同样使用test索引,分别使用es自带的分词器和IK提供的分词器
  • standard
{
  "analyzer" : "standard" ,
  "text" :"公众号博思奥园,中国文化博大精深"
}

es默认的分词器standard会将每一个汉字分成一个词条

  • ik_max_word
{
    "analyzer" : "ik_max_word" ,
    "text" :"公众号博思奥园,中国文化博大精深"
}
  • ik_smart
{
    "analyzer" : "ik_smart" ,
    "text" :"公众号博思奥园,中国文化博大精深"
}

通过分析可以看出,

  • es提供的分词器对中文分词并不是很友好,将所有的文字都拆开了
  • 而ik分词器就能够很友好的识别成语,更好的体会中国文化的博大精深,而ik_max_word与ik_smart之间的区别
  • ik_max_word会将文本做最细粒度的拆分
  • ik_smart会做最粗粒度的拆分

参考链接

posted @ 2022-04-18 20:57  专职  阅读(245)  评论(0编辑  收藏  举报