ik分詞器插件
es在7.3版本已經(jīng)支持中文分詞,由于中文分詞只能支持到單個(gè)字進(jìn)行分詞,不夠靈活與適配我們平常使用習(xí)慣,所以有很多對(duì)應(yīng)中文分詞出現(xiàn),最近使用的是ik分詞器,就說(shuō)說(shuō)它吧。
ik分詞器安裝
安裝可以百度下有很多教程,需要注意的是ik分詞器的版本要跟es版本對(duì)應(yīng)上,避免出現(xiàn)不必要的兼容問(wèn)題。
ik分詞模式
ik_max_word: 將文本拆分成最細(xì)粒度的詞語(yǔ)或者字
GET /test_analysis/_analyze
{
"text": "是否分詞",
"analyzer": "ik_max_word"
}
結(jié)果
{
"tokens" : [
{
"token" : "是否",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "是",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "否",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 2
},
{
"token" : "分詞",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 3
},
{
"token" : "分",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_WORD",
"position" : 4
},
{
"token" : "詞",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 5
}
]
}
ik_smart: 將文本拆分成最粗粒的詞語(yǔ)
GET /test_analysis/_analyze
{
"text": "是否分詞",
"analyzer": "ik_smart"
}
結(jié)果
{
"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
}
]
}
一般都用ik_max_word
es ik分詞測(cè)試
創(chuàng)建索引
PUT /test_analysis
{
"mappings": {
"properties": {
"message": {
"type": "text",
"analyzer": "ik_max_word"
},
"id": {
"type": "keyword"
}
}
}
}
添加數(shù)據(jù)
POST /test_analysis/_bulk
{"index":{}}
{"id":"111", "message":"我是一個(gè)小可愛(ài)"}
{"index":{}}
{"id":"222", "message":"只是為了測(cè)試一下結(jié)果是否分詞"}
{"index":{}}
{"id":"333", "message":"測(cè)試一下是否進(jìn)行了ik分詞"}
{"index":{}}
{"id":"444", "message":"搞一些假的數(shù)據(jù)吧"}
{"index":{}}
{"id":"555", "message":"實(shí)在不知道再寫(xiě)一些什么了"}
{"index":{}}
{"id":"666", "message":"就這樣吧"}
查詢文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-782721.html
GET /test_analysis/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"message": "是否分詞"
}
}
]
}
}
}
查詢分詞結(jié)果文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-782721.html
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 5.104265,
"hits" : [
{
"_index" : "test_analysis",
"_type" : "_doc",
"_id" : "MDXEe4wBS_Neyb68FBdy",
"_score" : 5.104265,
"_source" : {
"id" : "333",
"message" : "測(cè)試一下是否進(jìn)行了ik分詞"
}
},
{
"_index" : "test_analysis",
"_type" : "_doc",
"_id" : "LzXEe4wBS_Neyb68FBdy",
"_score" : 5.0611815,
"_source" : {
"id" : "222",
"message" : "只是為了測(cè)試一下結(jié)果是否分詞"
}
},
{
"_index" : "test_analysis",
"_type" : "_doc",
"_id" : "LjXEe4wBS_Neyb68FBdy",
"_score" : 0.728194,
"_source" : {
"id" : "111",
"message" : "我是一個(gè)小可愛(ài)"
}
}
]
}
}
到了這里,關(guān)于Elasticsearch之ik中文分詞篇的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!