Elasticsearch常用分词器

Elasticsearch 是一个基于 Lucene 的搜索服务器,拥有非常强大的全文检索能力

用户完全可以通过搭建一个 Elasticsearch 集群来实现搜索引擎的基本功能

但是,Elasticsearch 本身并不支持中文分词,但好在他支持编写和安装额外的分词管理插件,而开源的中文分词器 -- ik 就非常强大,具有20万以上的常用词库,可以满足一般的常用分词功能

本文,我们就来介绍如何安装 ik 分词库,如何为 ik 分词库添加自定义词库

 

standard

处理英文能力强

他会将词汇单元转换成小写形式,并去除停用词和标点符号

对于非英文按单字切分

 

whitespace

空格分析器

针对英文,仅去除空格,没有其他任何处理

不支持非英文

 

simple

针对英文,通过非字母字符分割文本信息,然后将词汇单元统一为小写形式

数字类型的字符会被去除

 

stop

StopAnalyzer 的功能超越了 SimpleAnalyzer

在 SimpleAnalyzer 的基础上增加了去除英文中的常用单词(如 the,a 等),也可以更加自己的需要设置常用单词

不支持中文

 

keyword

KeywordAnalyzer 把整个输入作为一个单独词汇单元,不会对文本进行任何拆分

通常是用在邮政编码、电话号码等需要全匹配的字段上

 

pattern

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

 

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

但不包含中文

 

snowball

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

 

custom

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

index :
    analysis :
        analyzer :
            myAnalyzer2 :
                type custom
                tokenizer myTokenizer1
                filter [myTokenFilter1, myTokenFilter2]
                char_filter [my_html]
                position_increment_gap256
        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

ikAnalyzer

IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。

采用了特有的“正向迭代最细粒度切分算法”,支持细粒度和最大词长两种切分模式;具有83万字/秒(1600KB/S)的高速处理能力。

采用了多子处理器分析模式,支持:英文字母、数字、中文词汇等分词处理,兼容韩文、日文字符

同事支持用户自定义词库

它带有两个分词器:

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

pinyin

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

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

 

安装

我们可以直接执行 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

 

重启 Elasticsearch

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

查看已安装插件

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

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

 

我们新建索引 es_ik:

{
    "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"

{
    "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
        }
    ]
}

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

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

 

创建分词库

在 ${es_home}/config/analysis-ik/ 下创建 custom 目录,custom 目录中加入 my.dic

my.dic 文件中可以任意加入自定义分词,每个分词占用一行

编辑完成后,打开 ${es_home}/config/analysis-ik/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">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"

{"tokens":[{"token":"","start_offset":0,"end_offset":3,"type":"CN_WORD"
,"position":0}]}
 来源: http://112.126.74.142/article/list/10183300