Elasticsearch給IK分詞器添加自定義詞匯


案例

汽車改裝鯊魚鰭 這句進行分詞

GET /_analyze
{
  "analyzer": "ik_smart",
  "text": ["汽車改裝鯊魚鰭"]
}

結果如下:

{
  "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
    },
    {
      "token" : "鯊",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "魚鰭",
      "start_offset" : 5,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 3
    }
  ]
}

可見,鯊魚鰭被分成了鯊、魚鰭,現在我們需要鯊魚鰭這三個字不要拆分,就得添加自定義詞匯。

步驟

進入IK配置目錄(我這邊是docker環境,步驟基本一樣)

docker exec -it es /bin/bash
cd plugins/ik/config/

創建自定義分詞文件

touch myDict.dic
vi myDict.dic
加上鯊魚鰭這三個字,保存退出

在這里插入圖片描述

讓IK分詞器識別自定義文件

vi IKAnalyzer.cfg.xml
在這里加上之前創建的分詞文件名,然后保存退出

在這里插入圖片描述

重啟ElasticSearch服務,再次測試,結果如下

{
  "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
    },
    {
      "token" : "鯊魚鰭",
      "start_offset" : 4,
      "end_offset" : 7,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}

這樣就說明配置生效了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM