Elasticsearch?Analyzer?內(nèi)置分詞器使用示例詳解
前置知識
主要介紹一下 Elasticsearch中 Analyzer 分詞器的構(gòu)成 和一些Es中內(nèi)置的分詞器 以及如何使用它們
es 提供了 analyze api 可以方便我們快速的指定 某個分詞器 然后對輸入的text文本進(jìn)行分詞 幫助我們學(xué)習(xí)和實(shí)驗(yàn)分詞器
POST _analyze { "analyzer": "standard", "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 ]
1.Analyzer
在ES中有很重要的一個概念就是 分詞,ES的全文檢索也是基于分詞結(jié)合倒排索引做的。所以這一文我們來看下何謂之分詞。如何分詞。
分詞器是專門處理分詞的組件,在很多中間件設(shè)計中每個組件的職責(zé)都劃分的很清楚,單一職責(zé)原則,以后改的時候好擴(kuò)展。
分詞器由三部分組成。
- Character Filters : 主要對原文本做處理, 例如 去除 html 標(biāo)簽
- Tokenizer : 按照規(guī)則 把文本切分為單詞, 也就是分詞
- Token Filters : 將切分后的單詞 進(jìn)行加工處理, 小寫,刪除stopwords 停頓詞, 增加同義詞 , 擴(kuò)展一些
分詞場景:
- 數(shù)據(jù)寫入index 的時候進(jìn)行分詞
- query 查詢時候 需要對查詢文本 進(jìn)行分詞
2.Elasticsearch 內(nèi)置分詞器
在es中有不少內(nèi)置分詞器
- Standard Analyzer : 默認(rèn)分詞器, 按Unicode文本分割算法拆分 , 轉(zhuǎn)化為小寫 , 支持中文(但是中文按照每個文字拆分,沒啥意義)
- Simple Analyzer : 按照非字母切分 并且轉(zhuǎn)化為小寫
- Stop Analyzer : 和 simple 一樣 但是多了 過濾停用詞(the a is) 默認(rèn)使用 stop token filter 的 _ _ english _ _ 預(yù)定義
- Whitespace Analyzer : 每當(dāng)遇到 空格的時候 會進(jìn)行分詞 , 不會轉(zhuǎn)小寫
- Keyword Analyzer : 不分詞 直接將輸入當(dāng)做輸出
- Patter Analyzer : 正則表達(dá)式
- Language : 語言分詞器 30多種
- Customer Analyzer : 自定義分詞器
3. Standard Analyzer
Standard 是es中默認(rèn)的分詞器 , 它是按照 Unicode 文本分割算法去 對文本進(jìn)行分詞的
POST _analyze { "analyzer": "standard", "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 ]
3.1 Definition
包括了 轉(zhuǎn)小寫的 token filter 和 stop token filter 去除停頓詞
Tokenizer
- [Standard Tokenizer]
Token Filters
- [Standard Token Filter] : 沒用只是作為保留的token filter (The
standard
token filter currently does nothing. It remains as a placeholder in case some filtering function needs to be added in a future version.) - [Lower Case Token Filter] : 轉(zhuǎn)小寫的 token filter
- [Stop Token Filter] : 停頓詞 token filter 默認(rèn)是沒有開啟
3.2 Configuration
- max_token_length : 最大的分詞長度,如果超過此長度 則直接分詞 default 255
- stopwords : 預(yù)定義的停頓詞列表 如: _ _ englisth _ _ 或者 停頓詞數(shù)組[] 默認(rèn) none 不設(shè)置
- stopwords_path : 包含停頓詞的文件路徑
3.3 實(shí)驗(yàn)
// 使用 自定義的分詞器 基于 standard PUT my_index { "settings": { "analysis": { "analyzer": { "my_english_analyzer": { "type": "standard", "max_token_length": 5, // 最大詞數(shù) "stopwords": "_english_" // 開啟過濾停頓詞 使用 englisth 語法 } } } } } GET my_index/_analyze { "analyzer": "my_english_analyzer", "text": "The hellogoodname jack" } // 可以看到 最長5個字符 就需要進(jìn)行分詞了, 并且停頓詞 the 沒有了 ["hello", "goodn", "ame", "jack"]
4. Simple Analyzer
簡單的分詞器 分詞規(guī)則就是 遇到 非字母的 就分詞, 并且轉(zhuǎn)化為小寫,(lowercase tokennizer )
POST _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 ]
4.1 Definition
Tokenizer
- Lower Case Tokenizer
4.2 Configuation
無配置參數(shù)
4.3 實(shí)驗(yàn)
simple analyzer 分詞器的實(shí)現(xiàn) 就是如下
PUT /simple_example { "settings": { "analysis": { "analyzer": { "rebuilt_simple": { "tokenizer": "lowercase", "filter": [ ] } } } } }
5. Stop Analyzer
stop analyzer 和 simple analyzer 一樣, 只是多了 過濾 stop word 的 token filter , 并且默認(rèn)使用 english 停頓詞規(guī)則
POST _analyze { "analyzer": "stop", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." } // 可以看到 非字母進(jìn)行分詞 并且轉(zhuǎn)小寫 然后 去除了停頓詞 [ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]
5.1 Definition
Tokenizer
- Lower Case Tokenizer : 轉(zhuǎn)小寫的
Token filters
- Stop Token Filter : 過濾停頓詞 默認(rèn)使用 規(guī)則 english
5.2 Configuration
- stopwords : 指定分詞的規(guī)則 默認(rèn) english , 或者分詞的數(shù)組
- stopwords_path : 指定分詞停頓詞文件
5.3 實(shí)驗(yàn)
如下就是對 Stop Analyzer 的實(shí)現(xiàn) , 先轉(zhuǎn)小寫 后進(jìn)行停頓詞的過濾
PUT /stop_example { "settings": { "analysis": { "filter": { "english_stop": { "type": "stop", "stopwords": "_english_" } }, "analyzer": { "rebuilt_stop": { "tokenizer": "lowercase", "filter": [ "english_stop" ] } } } } }
設(shè)置 stopwords 參數(shù) 指定過濾的停頓詞列表
PUT my_index { "settings": { "analysis": { "analyzer": { "my_stop_analyzer": { "type": "stop", "stopwords": ["the", "over"] } } } } } POST my_index/_analyze { "analyzer": "my_stop_analyzer", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." } [ quick, brown, foxes, jumped, lazy, dog, s, bone ]
6. Whitespace Analyzer
空格 分詞器, 顧名思義 遇到空格就進(jìn)行分詞, 不會轉(zhuǎn)小寫
POST _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. ]
6.1 Definition
Tokenizer
- Whitespace Tokenizer
6.2 Configuration
無配置
6.3 實(shí)驗(yàn)
whitespace analyzer 的實(shí)現(xiàn)就是如下, 可以根據(jù)實(shí)際情況進(jìn)行 添加 filter
PUT /whitespace_example { "settings": { "analysis": { "analyzer": { "rebuilt_whitespace": { "tokenizer": "whitespace", "filter": [ ] } } } } }
7. Keyword Analyzer
很特殊 它不會進(jìn)行分詞, 怎么輸入 就怎么輸出
POST _analyze { "analyzer": "keyword", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." } //注意 這里并沒有進(jìn)行分詞 而是原樣輸出 [ The 2 QUICK Brown-Foxes jumped over the lazy dog's bone. ]
7.1 Definition
Tokennizer
- Keyword Tokenizer
7.2 Configuration
無配置
7.3 實(shí)驗(yàn)
rebuit 如下 就是 Keyword Analyzer 實(shí)現(xiàn)
PUT /keyword_example { "settings": { "analysis": { "analyzer": { "rebuilt_keyword": { "tokenizer": "keyword", "filter": [ ] } } } } }
8. Patter Analyzer
正則表達(dá)式 進(jìn)行拆分 ,注意 正則匹配的是 標(biāo)記, 就是要被分詞的標(biāo)記 默認(rèn)是 按照 \w+ 正則分詞
POST _analyze { "analyzer": "pattern", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." } // 默認(rèn)是 按照 \w+ 正則 [ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
8.1 Definition
Tokennizer
- Pattern Tokenizer
Token Filters
- Lower Case Token Filter
- Stop Token Filter (默認(rèn)未開啟)
8.2 Configuration
pattern | A Java regular expression, defaults to \W+. |
---|---|
flags | Java regular expression. |
lowercase | 轉(zhuǎn)小寫 默認(rèn)開啟 true. |
stopwords | 停頓詞過濾 默認(rèn)none 未開啟 , Defaults to _none_. |
stopwords_path | 停頓詞文件路徑 |
8.3 實(shí)驗(yàn)
Pattern Analyzer 的實(shí)現(xiàn) 就是如下
PUT /pattern_example { "settings": { "analysis": { "tokenizer": { "split_on_non_word": { "type": "pattern", "pattern": "\\W+" } }, "analyzer": { "rebuilt_pattern": { "tokenizer": "split_on_non_word", "filter": [ "lowercase" ] } } } } }
9. Language Analyzer
提供了如下 這么多語言分詞器 , 其中 english 也在其中
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
.
GET _analyze { "analyzer": "english", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." } [ 2, quick, brown, foxes, jumped, over, lazy, dog, bone ]
10. Customer Analyzer
沒啥好說的 就是當(dāng)提供的 內(nèi)置分詞器不滿足你的需求的時候 ,你可以結(jié)合 如下3部分
- Character Filters : 主要對原文本做處理, 例如 去除 html 標(biāo)簽
- Tokenizer : 按照規(guī)則 把文本切分為單詞, 也就是分詞
- Token Filters : 將切分后的單詞 進(jìn)行加工處理, 小寫,刪除stopwords 停頓詞, 增加同義詞 , 擴(kuò)展一些
PUT my_index { "settings": { "analysis": { "analyzer": { "my_custom_analyzer": { "type": "custom", "char_filter": [ "emoticons" ], "tokenizer": "punctuation", "filter": [ "lowercase", "english_stop" ] } }, "tokenizer": { "punctuation": { "type": "pattern", "pattern": "[ .,!?]" } }, "char_filter": { "emoticons": { "type": "mapping", "mappings": [ ":) => _happy_", ":( => _sad_" ] } }, "filter": { "english_stop": { "type": "stop", "stopwords": "_english_" } } } } } POST my_index/_analyze { "analyzer": "my_custom_analyzer", "text": "I'm a :) person, and you?" } [ i'm, _happy_, person, you ]
總結(jié)
本篇主要介紹了 Elasticsearch 中 的一些 內(nèi)置的 Analyzer 分詞器, 這些內(nèi)置分詞器可能不會常用,但是如果你能好好梳理一下這些內(nèi)置 分詞器,一定會對你理解Analyzer 有很大的幫助, 可以幫助你理解 Character Filters , Tokenizer 和 Token Filters 的用處.
有機(jī)會再聊聊 一些中文分詞器 如 IKAnalyzer, ICU Analyzer ,Thulac 等等.. 畢竟開發(fā)中 中文分詞器用到更多些
以上就是Elasticsearch Analyzer 內(nèi)置分詞器使用示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Elasticsearch Analyzer分詞器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
microlog4android將Android Log日志寫到SD卡文件中實(shí)現(xiàn)方法
這篇文章主要介紹了microlog4android將Android Log日志寫到SD卡文件中實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-10-10微服務(wù)Spring?Boot?整合Redis?阻塞隊(duì)列實(shí)現(xiàn)異步秒殺下單思路詳解
這篇文章主要介紹了微服務(wù)Spring?Boot?整合Redis?阻塞隊(duì)列實(shí)現(xiàn)異步秒殺下單,使用阻塞隊(duì)列實(shí)現(xiàn)秒殺的優(yōu)化,采用異步秒殺完成下單的優(yōu)化,本文給大家分享詳細(xì)步驟及實(shí)現(xiàn)思路,需要的朋友可以參考下2022-10-10Java項(xiàng)目防止SQL注入的幾種方法總結(jié)
SQL注入是比較常見的網(wǎng)絡(luò)攻擊方式之一,在客戶端在向服務(wù)器發(fā)送請求的時候,sql命令通過表單提交或者url字符串拼接傳遞到后臺持久層,最終達(dá)到欺騙服務(wù)器執(zhí)行惡意的SQL命令,下面這篇文章主要給大家總結(jié)介紹了關(guān)于Java項(xiàng)目防止SQL注入的幾種方法,需要的朋友可以參考下2023-04-04mybatis-plus雪花算法增強(qiáng)idworker的實(shí)現(xiàn)
今天聊聊在mybatis-plus中引入分布式ID生成框架idworker,進(jìn)一步增強(qiáng)實(shí)現(xiàn)生成分布式唯一ID,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07