因爲ik目前最高支持es 8.2.3,所以本文基於8.2.3的環境編寫.
1、集羣環境構建
參考Es 集羣搭建及相關配置和相關參數解讀,分別下載Es和kibana 8.2.3版本,部署到相應的服務器,刪除es原有的data目錄.配置elasticsearch.yml,節點配置如下:
節點配置
cluster.name: test_cluster node.name: node-1 network.host: 0.0.0.0 http.port: 9200 transport.port: 9300 http.cors.enabled: true http.cors.allow-origin: "*" discovery.seed_hosts: ["127.0.0.1:9300", "172.18.100.231:9300","172.18.100.224:9300"] cluster.initial_master_nodes: ["node-1", "node-2","node-3"] xpack.security.enabled: false
注意事項:節點名稱、殺毒軟件、防火牆等等,否則集羣構建會失敗
接着運行各自節點的elasticsearch.bat文件.
環境構建到此結束.
2、ik分詞器安裝部署 下載地址
注意es和ik分詞器的版本匹配.這裏下載8.2.3的ik分詞器
下載完畢之後去es的工作目錄的plugins文件夾下新建ik文件夾,將下載下來的ik壓縮包解壓縮至ik文件夾下,重啓es,集羣中所有節點重複此操作.
3、ik 分詞器簡介
3.1 詞庫介紹
ik分詞器主要有以下詞庫,位於config目錄下
(1)、main.dic 主詞庫,包含日常生活中常用的詞
(2)、stopword.dic 英文停用詞,當出現該詞庫中的文本內容時,將不會建立倒排索引
(3)、quantifier.dic 計量單位等
(4)、suffix.dic 後綴名、行政單位等
(5)、surname.dic 百家姓等
(6)、preposition.dic 語氣詞等
3.2 配置介紹
IKAnalyzer.cfg.xml ik配置文件位於config目錄下
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>IK Analyzer 擴展配置</comment> <!--用戶可以在這裏配置自己的擴展字典 --> <entry key="ext_dict"></entry> <!--用戶可以在這裏配置自己的擴展停止詞字典--> <entry key="ext_stopwords"></entry> <!--用戶可以在這裏配置遠程擴展字典 --> <!-- <entry key="remote_ext_dict">words_location</entry> --> <!--用戶可以在這裏配置遠程擴展停止詞字典--> <!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties>
主要用於擴展配置.
3.3 使用方式
ik分詞器主要分爲ik_smart
、ik_max_word
下面分別測試,使用kibna dev tools.
ik_smart
GET test_index/_analyze { "tokenizer": "ik_smart", "text": ["中華人民共和國"] }
分詞結果如下:
{ "tokens" : [ { "token" : "中華人民共和國", "start_offset" : 0, "end_offset" : 7, "type" : "CN_WORD", "position" : 0 } ] }
ik_max_word
GET test_index/_analyze { "tokenizer": "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 } ] }
根據分詞結果,很明顯ik_max_word分的粒度更加的細和全面,所以一般都是用ik_max_word作爲分詞器.
3.4 擴展分詞
一般情況下,詞庫是夠用的,但是如果碰到一些特殊詞彙如網絡用詞,這個時候就需要手動添加相關的詞彙進入到詞庫中.ik添加自定義詞庫的步驟如下
(1)、在config目錄下,新增自定義詞庫文件
內容其中一個文件如下:
(2)、修改配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>IK Analyzer 擴展配置</comment> <!--用戶可以在這裏配置自己的擴展字典 --> <entry key="ext_dict">custom_1.dic;custom_2.dic</entry> <!--用戶可以在這裏配置自己的擴展停止詞字典--> <entry key="ext_stopwords"></entry> <!--用戶可以在這裏配置遠程擴展字典 --> <!-- <entry key="remote_ext_dict">words_location</entry> --> <!--用戶可以在這裏配置遠程擴展停止詞字典--> <!-- <entry key="remote_ext_stopwords">words_location</entry> --> </properties>
(3)、集羣中所有的節點重複此操作
(4)、重啓所有節點
(5)、測試
dev tools執行以下語句
GET test_index/_analyze { "tokenizer": "ik_max_word", "text": ["啊啦搜"] }
結果如下:
{ "tokens" : [ { "token" : "啊啦搜", "start_offset" : 0, "end_offset" : 3, "type" : "CN_WORD", "position" : 0 } ] }
啊啦搜被作爲一個詞進行了分詞,說明自定義詞庫生效了.
其餘內容參考官方文檔