ES中的分詞器

轉(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 即可。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Elasticsearch 中文搜索時遇到幾個問題: 當(dāng)搜索關(guān)鍵詞如:“人民幣”時,如果分詞將“人民幣”分成“人”...
    永遠(yuǎn)de明天閱讀 909評論 0 0
  • 倒排索引 正排索引:文檔id到單詞的關(guān)聯(lián)關(guān)系 倒排索引:單詞到文檔id的關(guān)聯(lián)關(guān)系 示例:對以下三個文檔去除停用詞后...
    小旋鋒的簡書閱讀 4,762評論 1 11
  • 在上一篇文章中,我們知道client和ES交互的數(shù)據(jù)格式都是json,也知道了ES中的index和type的關(guān)系。...
    11舍的華萊士閱讀 3,485評論 0 1
  • Elasticsearch 中文搜索時遇到幾個問題: 當(dāng)搜索關(guān)鍵詞如:“人民幣”時,如果分詞將“人民幣”分成“人”...
    sudop閱讀 67,836評論 5 42
  • 我想坐在小時候的地頭 高鐵完成了現(xiàn)代人很多的不可能 我做到了田間 只是再也沒有兒時那種快樂 只有炙熱的天氣 和內(nèi)心...
    土的世界閱讀 193評論 0 0

友情鏈接更多精彩內(nèi)容