轉(zhuǎn)載自:https://blog.csdn.net/xiaomin1991222/article/details/50981874、https://segmentfault.com/a/1190000012553894
概念介紹
全文搜索引擎會用某種算法對要建索引的文檔進(jìn)行分析, 從文檔中提取出若干Token(詞元), 這些算法稱為Tokenizer(分詞器), 這些Token會被進(jìn)一步處理, 比如轉(zhuǎn)成小寫等, 這些處理算法被稱為Token Filter(詞元處理器),被處理后的結(jié)果被稱為Term(詞), 文檔中包含了幾個這樣的Term被稱為Frequency(詞頻)。 引擎會建立Term和原文檔的Inverted Index(倒排索引), 這樣就能根據(jù)Term很快到找到源文檔了。 文本被Tokenizer處理前可能要做一些預(yù)處理, 比如去掉里面的HTML標(biāo)記, 這些處理的算法被稱為Character Filter(字符過濾器), 這整個的分析算法被稱為Analyzer(分析器)。
整個分析過程,如下圖所示:

ES中的分詞器
從第一部分內(nèi)容可以看出:Analyzer(分析器)由Tokenizer(分詞器)和Filter(過濾器)組成。
1. ES內(nèi)置分析器

2. ES內(nèi)置分詞器

3. ES內(nèi)置過濾器
3.1 ES內(nèi)置的token filter


3.2 ES內(nèi)置的character filter

自定義分析器
ES允許用戶通過配置文件elasticsearch.yml自定義分析器Analyzer,如下:
index:
analysis:
analyzer:
myAnalyzer:
tokenizer: standard
filter: [standard, lowercase, stop]
上面配置信息注冊了一個分析器myAnalyzer,在次注冊了之后可以在索引或者查詢的時候直接使用。該分析器的功能和標(biāo)準(zhǔn)分析器差不多,tokenizer: standard,使用了標(biāo)準(zhǔn)分詞器 ;filter: [standard, lowercase, stop],使用了標(biāo)準(zhǔn)過濾器、轉(zhuǎn)小寫過濾器和停用詞過濾器。
中文分詞器es-ik
ElasticSearch默認(rèn)使用的標(biāo)準(zhǔn)分詞器在處理中文的時候會把中文單詞切分成一個一個的漢字,所以在很多時候我們會發(fā)現(xiàn)效果并不符合我們預(yù)期,尤其在我們使用中文文本切分之后本該為一個詞語卻成了單個的漢字,因此這里我們使用效果更佳的中文分詞器es-ik。
ik 帶有兩個分詞器:
- ik_max_word :會將文本做最細(xì)粒度的拆分;盡可能多的拆分出詞語
- ik_smart:會做最粗粒度的拆分;已被分出的詞語將不會再次被其它詞語占有
區(qū)別:
# ik_max_word
curl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=ik_max_word' -d '聯(lián)想是全球最大的筆記本廠商'
#返回
{
"tokens" : [
{
"token" : "聯(lián)想",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "全球",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "最大",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "的",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 4
},
{
"token" : "筆記本",
"start_offset" : 8,
"end_offset" : 11,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "筆記",
"start_offset" : 8,
"end_offset" : 10,
"type" : "CN_WORD",
"position" : 6
},
{
"token" : "本廠",
"start_offset" : 10,
"end_offset" : 12,
"type" : "CN_WORD",
"position" : 7
},
{
"token" : "廠商",
"start_offset" : 11,
"end_offset" : 13,
"type" : "CN_WORD",
"position" : 8
}
]
}
# ik_smart
curl -XGET 'http://localhost:9200/_analyze?pretty&analyzer=ik_smart' -d '聯(lián)想是全球最大的筆記本廠商'
# 返回
{
"tokens" : [
{
"token" : "聯(lián)想",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "是",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "全球",
"start_offset" : 3,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "最大",
"start_offset" : 5,
"end_offset" : 7,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "的",
"start_offset" : 7,
"end_offset" : 8,
"type" : "CN_CHAR",
"position" : 4
},
{
"token" : "筆記本",
"start_offset" : 8,
"end_offset" : 11,
"type" : "CN_WORD",
"position" : 5
},
{
"token" : "廠商",
"start_offset" : 11,
"end_offset" : 13,
"type" : "CN_WORD",
"position" : 6
}
]
}
下面我們來創(chuàng)建一個索引,使用 ik。創(chuàng)建一個名叫 iktest 的索引,設(shè)置它的分析器用 ik ,分詞器用 ik_max_word,并創(chuàng)建一個 article 的類型,里面有一個 subject 的字段,指定其使用 ik_max_word 分詞器。
curl -XPUT 'http://localhost:9200/iktest?pretty' -d '{
"settings" : {
"analysis" : {
"analyzer" : {
"ik" : {
"tokenizer" : "ik_max_word"
}
}
}
},
"mappings" : {
"article" : {
"dynamic" : true,
"properties" : {
"subject" : {
"type" : "string",
"analyzer" : "ik_max_word"
}
}
}
}
}'
批量添加幾條數(shù)據(jù),這里我指定元數(shù)據(jù) _id 方便查看,subject 內(nèi)容為我隨便找的幾條新聞的標(biāo)題
curl -XPOST http://localhost:9200/iktest/article/_bulk?pretty -d '
{ "index" : { "_id" : "1" } }
{"subject" : ""閨蜜"崔順實被韓檢方傳喚 韓總統(tǒng)府促徹查真相" }
{ "index" : { "_id" : "2" } }
{"subject" : "韓舉行"護(hù)國訓(xùn)練" 青瓦臺:決不許國家安全出問題" }
{ "index" : { "_id" : "3" } }
{"subject" : "媒體稱FBI已經(jīng)取得搜查令 檢視希拉里電郵" }
{ "index" : { "_id" : "4" } }
{"subject" : "村上春樹獲安徒生獎 演講中談及歐洲排外問題" }
{ "index" : { "_id" : "5" } }
{"subject" : "希拉里團(tuán)隊炮轟FBI 參院民主黨領(lǐng)袖批其“違法”" }
查詢 “希拉里和韓國”
curl -XPOST http://localhost:9200/iktest/article/_search?pretty -d'
{
"query" : { "match" : { "subject" : "希拉里和韓國" }},
"highlight" : {
"pre_tags" : ["<font color='red'>"],
"post_tags" : ["</font>"],
"fields" : {
"subject" : {}
}
}
}
'
#返回
{
"took" : 113,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 0.034062363,
"hits" : [ {
"_index" : "iktest",
"_type" : "article",
"_id" : "2",
"_score" : 0.034062363,
"_source" : {
"subject" : "韓舉行"護(hù)國訓(xùn)練" 青瓦臺:決不許國家安全出問題"
},
"highlight" : {
"subject" : [ "<font color=red>韓</font>舉行"護(hù)<font color=red>國</font>訓(xùn)練" 青瓦臺:決不許國家安全出問題" ]
}
}, {
"_index" : "iktest",
"_type" : "article",
"_id" : "3",
"_score" : 0.0076681254,
"_source" : {
"subject" : "媒體稱FBI已經(jīng)取得搜查令 檢視希拉里電郵"
},
"highlight" : {
"subject" : [ "媒體稱FBI已經(jīng)取得搜查令 檢視<font color=red>希拉里</font>電郵" ]
}
}, {
"_index" : "iktest",
"_type" : "article",
"_id" : "5",
"_score" : 0.006709609,
"_source" : {
"subject" : "希拉里團(tuán)隊炮轟FBI 參院民主黨領(lǐng)袖批其“違法”"
},
"highlight" : {
"subject" : [ "<font color=red>希拉里</font>團(tuán)隊炮轟FBI 參院民主黨領(lǐng)袖批其“違法”" ]
}
}, {
"_index" : "iktest",
"_type" : "article",
"_id" : "1",
"_score" : 0.0021509775,
"_source" : {
"subject" : ""閨蜜"崔順實被韓檢方傳喚 韓總統(tǒng)府促徹查真相"
},
"highlight" : {
"subject" : [ ""閨蜜"崔順實被<font color=red>韓</font>檢方傳喚 <font color=red>韓</font>總統(tǒng)府促徹查真相" ]
}
} ]
}
}
這里用了高亮屬性 highlight,直接顯示到 html 中,被匹配到的字或詞將以紅色突出顯示。若要用過濾搜索,直接將 match 改為 term 即可。