Elasticsearch下安裝IK中文分詞器

  • 2019 年 10 月 6 日
  • 筆記

環境:elasticsearch版本是5.5.2,其所在目錄為/usr/local/elasticsearch-5.5.2

  1. 下載

解壓到 /usr/local/elasticsearch-5.5.2/plugins/ , 目錄結構如下

├── plugins  │   └── elasticsearch-analysis-ik  │       ├── commons-codec-1.9.jar  │       ├── commons-logging-1.2.jar  │       ├── config  │       │   ├── extra_main.dic  │       │   ├── extra_single_word.dic  │       │   ├── extra_single_word_full.dic  │       │   ├── extra_single_word_low_freq.dic  │       │   ├── extra_stopword.dic  │       │   ├── IKAnalyzer.cfg.xml  │       │   ├── main.dic  │       │   ├── preposition.dic  │       │   ├── quantifier.dic  │       │   ├── stopword.dic  │       │   ├── suffix.dic  │       │   └── surname.dic  │       ├── elasticsearch-analysis-ik-5.5.2.jar  │       ├── httpclient-4.5.2.jar  │       ├── httpcore-4.4.4.jar  │       └── plugin-descriptor.properties
  1. 重啟 elasticsearch
  2. 測試

分別用下面兩種方式檢查一下分詞效果

ik_max_word分詞法

GET _analyze  {    "analyzer":"ik_max_word",    "text":"中華人民共和國國歌"  }

結果

{    "tokens": [      {        "token": "中華人民共和國",        "start_offset": 0,        "end_offset": 7,        "type": "CN_WORD",        "position": 0      },      {        "token": "中華人民",        "start_offset": 0,        "end_offset": 4,        "type": "CN_WORD",        "position": 1      },      {        "token": "中華",        "start_offset": 0,        "end_offset": 2,        "type": "CN_WORD",        "position": 2      },      {        "token": "華人",        "start_offset": 1,        "end_offset": 3,        "type": "CN_WORD",        "position": 3      },      {        "token": "人民共和國",        "start_offset": 2,        "end_offset": 7,        "type": "CN_WORD",        "position": 4      },      {        "token": "人民",        "start_offset": 2,        "end_offset": 4,        "type": "CN_WORD",        "position": 5      },      {        "token": "共和國",        "start_offset": 4,        "end_offset": 7,        "type": "CN_WORD",        "position": 6      },      {        "token": "共和",        "start_offset": 4,        "end_offset": 6,        "type": "CN_WORD",        "position": 7      },      {        "token": "國",        "start_offset": 6,        "end_offset": 7,        "type": "CN_CHAR",        "position": 8      },      {        "token": "國歌",        "start_offset": 7,        "end_offset": 9,        "type": "CN_WORD",        "position": 9      }    ]  }

智慧分詞法

GET _analyze  {    "analyzer":"ik_smart",    "text":"中華人民共和國國歌"  }

結果

{    "tokens": [      {        "token": "中華人民共和國",        "start_offset": 0,        "end_offset": 7,        "type": "CN_WORD",        "position": 0      },      {        "token": "國歌",        "start_offset": 7,        "end_offset": 9,        "type": "CN_WORD",        "position": 1      }    ]  }

修改 Mapping中text類型的欄位定義

...  "title": {            "type": "text",            "analyzer": "ik_max_word",            "search_analyzer": "ik_max_word",            "include_in_all": "true"          },  ...

已有大數據需要重建索引

參考 https://github.com/medcl/elasticsearch-analysis-ik