前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch 常用分词器介绍与 ik 分词器自定义词库添加

Elasticsearch 常用分词器介绍与 ik 分词器自定义词库添加

作者头像
用户3147702
发布2022-06-27 13:11:06
2.2K0
发布2022-06-27 13:11:06
举报
文章被收录于专栏:小脑斧科技博客

1. 引言

Elasticsearch 是一个基于 Lucene 的搜索服务器,拥有非常强大的全文检索能力。 用户完全可以通过搭建一个 Elasticsearch 集群来实现搜索引擎的基本功能。 但是,Elasticsearch 本身并不支持中文分词,但好在他支持编写和安装额外的分词管理插件,而开源的中文分词器 — ik 就非常强大,具有20万以上的常用词库,可以满足一般的常用分词功能。 本文,我们就来介绍如何安装 ik 分词库,如何为 ik 分词库添加自定义词库。

2. Elasticsearch 常用分词器

2.1. standard

处理英文能力强。 他会将词汇单元转换成小写形式,并去除停用词和标点符号。 对于非英文按单字切分。

2.2. whitespace

空格分析器。 针对英文,仅去除空格,没有其他任何处理。 不支持非英文。

2.3. simple

针对英文,通过非字母字符分割文本信息,然后将词汇单元统一为小写形式。 数字类型的字符会被去除。

2.4. stop

StopAnalyzer 的功能超越了 SimpleAnalyzer。 在 SimpleAnalyzer 的基础上增加了去除英文中的常用单词(如 the,a 等),也可以更加自己的需要设置常用单词。 不支持中文。

2.5. keyword

KeywordAnalyzer 把整个输入作为一个单独词汇单元,不会对文本进行任何拆分。 通常是用在邮政编码、电话号码等需要全匹配的字段上。

2.6. pattern

查询文本会被自动当做正则表达式处理,生成一组 terms 关键字,然后在对 Elasticsearch 进行查询。

2.7. language

一个用于解析特殊语言文本的 analyzer 集合。 包括:arabic,armenian, basque, brazilian, bulgarian, catalan, cjk, czech, danish, dutch, english, finnish, french,galician, german, greek, hindi, hungarian, indonesian, irish, italian, latvian, lithuanian, norwegian,persian, portuguese, romanian, russian, sorani, spanish, swedish, turkish, thai。 但不包含中文。

2.8. snowball

雪球分析器,在 standard 的基础上添加了 snowball filter,Lucene 官方不推荐使用。

2.9. custom

可以自己定义分次其中 filter 列表的分词器。

代码语言:javascript
复制
index :
    analysis :
        analyzer :
            myAnalyzer2 :
                type : custom
                tokenizer : myTokenizer1
                filter : [myTokenFilter1, myTokenFilter2]
                char_filter : [my_html]
                position_increment_gap: 256
        tokenizer :
            myTokenizer1 :
                type : standard
                max_token_length : 900
        filter :
            myTokenFilter1 :
                type : stop
                stopwords : [stop1, stop2, stop3, stop4]
            myTokenFilter2 :
                type : length
                min : 0
                max : 2000
        char_filter :
              my_html :
                type : html_strip
                escaped_tags : [xxx, yyy]
                read_ahead : 1024

2.10. ikAnalyzer

IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。 采用了特有的“正向迭代最细粒度切分算法”,支持细粒度和最大词长两种切分模式;具有83万字/秒(1600KB/S)的高速处理能力。 采用了多子处理器分析模式,支持:英文字母、数字、中文词汇等分词处理,兼容韩文、日文字符。 同事支持用户自定义词库。 它带有两个分词器:

  • ik_max_word — 将文本做最细粒度的拆分,尽可能多的拆分出词语
  • ik_smart — 做最粗粒度的拆分,已被分出的词语将不会再次被其它词语占有

2.11. pinyin

通过用户输入的拼音匹配 Elasticsearch 中的中文。

https://github.com/medcl/elasticsearch-analysis-pinyin

3. ik 分词器的安装

3.1. 安装

我们可以直接执行 Elasticsearch 提供的 elasticsearch-plugin 命令安装插件:

./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.10/elasticsearch-analysis-ik-5.6.10.zip

也可以从 github 上下载后直接安装,参考:

https://github.com/medcl/elasticsearch-analysis-ik

3.2. 重启 Elasticsearch

安装完成,重启 Elasticsearch 服务,可以看到日志中显示出了 analyzer-ik 的加载日志:

3.3. 查看已安装插件

Elasticsearch 启动后,通过下面的请求可以查看已安装插件:

GET /_cat/plugins?v&s=component&h=name,component,version,description

4. 使用分词库

我们新建索引 es_ik:

代码语言:javascript
复制
{
    "mappings":{
        "a_info":{
            "dynamic":"false",
            "_all":{
                "enabled":false
            },
            "properties":{
                "seqno":{
                    "type":"text"
                },
                "title":{
                    "type":"text",
                    "index":"analyzed",
                    "analyzer":"ik_smart",
                    "search_analyzer":"ik_smart"
                }
            }
        }
    }
}

然后我们就可以通过下面命令实现对分词库的测试:

curl ’http://127.0.0.1:9200/es_ik/_analyze' -d '{"analyzer" : "ik_smart", "text": "欢迎关注小脑斧科技博客"}' -H "Content-Type: application/json"

打印出了:

代码语言:javascript
复制
{
    "tokens":[
        {
            "token":"欢迎",
            "start_offset":0,
            "end_offset":2,
            "type":"CN_WORD",
            "position":0
        },
        {
            "token":"关注",
            "start_offset":2,
            "end_offset":4,
            "type":"CN_WORD",
            "position":1
        },
        {
            "token":"小脑",
            "start_offset":4,
            "end_offset":6,
            "type":"CN_WORD",
            "position":2
        },
        {
            "token":"斧",
            "start_offset":6,
            "end_offset":7,
            "type":"CN_CHAR",
            "position":3
        },
        {
            "token":"科技",
            "start_offset":7,
            "end_offset":9,
            "type":"CN_WORD",
            "position":4
        },
        {
            "token":"博客",
            "start_offset":9,
            "end_offset":11,
            "type":"CN_WORD",
            "position":5
        }
    ]
}

可以看到分词基本是符合我们预期的。

5. 配置自定义分词库

上面的测试中,因为 ik 本身的词库中并没有 “小脑斧” 这个词,所以分成了 “小脑” 和 “斧” 两个词,如果我们想让 ik 分词器识别 “小脑斧” 我们就必须自己定义词库了。

5.1. 创建分词库

{es_home}/config/analysis-ik/ 下创建 custom 目录,custom 目录中加入 my.dic。my.dic 文件中可以任意加入自定义分词,每个分词占用一行。编辑完成后,打开 {es_home}/config/analysis-ik/IKAnalyzer.cfg.xml 添加相应配置:

代码语言:javascript
复制
<?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">custom/my.dic</entry>
         <!--用户可以在这里配置自己的扩展停止词字典-->
        <entry key="ext_stopwords"></entry>
        <!--用户可以在这里配置远程扩展字典 -->
        <!-- <entry key="remote_ext_dict">words_location</entry> -->
        <!--用户可以在这里配置远程扩展停止词字典-->
        <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>

然后,重启 Elasticsearch 服务,重新执行:

curl ’http://127.0.0.1:9200/stock_news/_analyze' -d '{"analyzer" : "ik_smart", "text": "小脑斧"}' -H "Content-Type: application/json&quot;

可以看到:

代码语言:javascript
复制
{"tokens":[{"token":"小脑斧","start_offset":0,"end_offset":3,"type":"CN_WORD","position":0}]}

6. 参考资料

https://www.elastic.co/guide/cn/elasticsearch/guide/current/query-dsl-intro.html。 https://www.elastic.co/guide/en/elasticsearch/reference/0.90/analysis-custom-analyzer.html。 https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-fingerprint-analyzer.html。 https://github.com/medcl/elasticsearch-analysis-ik。 https://zhuanlan.zhihu.com/p/29183128。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-01-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小脑斧科技博客 微信公众号,前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 引言
  • 2. Elasticsearch 常用分词器
    • 2.1. standard
      • 2.2. whitespace
        • 2.3. simple
          • 2.4. stop
            • 2.5. keyword
              • 2.6. pattern
                • 2.7. language
                  • 2.8. snowball
                    • 2.9. custom
                      • 2.10. ikAnalyzer
                        • 2.11. pinyin
                        • 3. ik 分词器的安装
                          • 3.1. 安装
                            • 3.2. 重启 Elasticsearch
                              • 3.3. 查看已安装插件
                              • 4. 使用分词库
                              • 5. 配置自定义分词库
                                • 5.1. 创建分词库
                                • 6. 参考资料
                                相关产品与服务
                                Elasticsearch Service
                                腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档