上一篇,什麼是倒排索引以及原理是什麼。本篇講解 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?