上一篇,什么是倒排索引以及原理是什么。本篇讲解 Analyzer,了解 Analyzer 是什么 ,分词器是什么,以及 Elasticsearch 内置的分词器,最后再讲解中文分词是怎么作的。html
1、Analysis 与 Analyzer正则表达式
Analysis 文本分析是把全文本转换一系列单词(term/token)的过程,也叫分词
,Analysis 是经过 Analyzer 来实现的。 Elasticsearch 有多种 内置的分析器,若是不知足也能够根据本身的需求定制化分析器,除了在数据写入时转换词条,匹配 Query 语句时候也须要用相同的分析器对查询语句进行分析。elasticsearch
2、Analyzer 的组成函数
3、Analyzer 内置的分词器ui
例子:The 2 QUICK Brown-Foxes jumped over the lazy dog's bone.spa
Standard Analyzer插件
#standard GET _analyze { "analyzer": "standard", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." }
输出:code
[the,2,quick,brown,foxes,a,jumped,over,the,lazy,dog's,bone]orm
Simple Analyzerhtm
#simpe GET _analyze { "analyzer": "simple", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." }
输出:
[the,quick,brown,foxes,jumped,over,the,lazy,dog,s,bone]
Stop Analyzer
GET _analyze { "analyzer": "stop", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." }
输出:
[quick,brown,foxes,jumped,over,lazy,dog,s,bone]
Whitespace Analyzer
#stop GET _analyze { "analyzer": "whitespace", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." }
输出:
[The,2,QUICK,Brown-Foxes,jumped,over,the,lazy,dog's,bone.]
Keyword Analyzer
#keyword GET _analyze { "analyzer": "keyword", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." }
输出:
[The 2 QUICK Brown-Foxes jumped over the lazy dog's bone.]
Patter Analyzer
GET _analyze { "analyzer": "pattern", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." }
输出:
[the,2,quick,brown,foxes,jumped,over,the,lazy,dog,s,bone]
Language Analyzer
支持语言:arabic, armenian, basque, bengali, bulgarian, catalan, czech, dutch, english, finnish, french, galician, german, hindi, hungarian, indonesian, irish, italian, latvian, lithuanian, norwegian, portuguese, romanian, russian, sorani, spanish, swedish, turkish.
#english GET _analyze { "analyzer": "english", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." }
输出:
[2,quick,brown,fox,jump,over,the,lazy,dog,bone]
中文分词要比英文分词难,英文都以空格分隔,中文理解一般须要上下文理解才能有正确的理解,好比 [苹果,不大好吃]和
[苹果,不大,好吃],这两句意思就不同。
ICU Analyzer
ElasticSearch 默认以每一个字对中文分隔,没法知足咱们的需求。ICU Analyzer 使用国际化组件 Unicode (ICU) 函数库提供丰富的处理 Unicode ,更好支持中文分词,ICU Analyzer 不是默认分词器,须要先安装插件,安装命令 sudo bin/elasticsearch-plugin install analysis-icu。
POST _analyze { "analyzer": "icu_analyzer", "text": "他说的确实在理”" }
输出:
[他,说的,确实,在,理]
POST _analyze { "analyzer": "standard", "text": "他说的确实在理”" }
输出:
[他,说,的,确,实,在,理]
ICU 只是其中一种中文分词器,在 Github 上能够查找到其余中文分词器,好比 IK,THULAC,这些就不在这里说起,有兴趣能够自行了解。
4、总结
本篇对 Analyzer 进行详细讲解,ES 内置分词器是如何工做的,经过 ICU Analyzer 对中文分词的效果,下面总结内置的全部分词器的特色,作一个简单对比。
Standard Analyzer -- 默认分词器,按词切分,小写处理
Simple Analyzer -- 按照非字母切分(符号被过滤),小写处理
Stop Analyzer -- 小写处理,停用词过滤(the,a, is)
Whitespace Analyzer -- 按照空格切分,不转小写
Keyword Analyzer -- 不分词,直接将输入看成输出
Patter Analyzer -- 正则表达式,默认\W+ (非字符分隔)
Language Analyzer -- 提供了 30 多种常见语言的分词器
Customer Analyzer -- 自定义分词器
【Elasticsearch 7 探索之路】(三)倒排索引
【Elasticsearch 7 探索之路】(二)文档的 CRUD 和批量操做
【Elasticsearch 7 搜索之路】(一)什么是 Elasticsearch?