IK分析插件整合了Lucene IK分析器,並支援自訂字典。它支援 Elasticsearch 和 OpenSearch 的主要版本。由 INFINI Labs ❤️ 維護和支援。
該外掛程式包括分析器: ik_smart
、 ik_max_word
和分詞器: ik_smart
、 ik_max_word
您可以從這裡下載打包的插件:https: https://release.infinilabs.com/
,
或者你可以使用plugin
cli 來安裝插件,如下所示:
對於彈性搜尋
bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-ik/8.4.1
對於開放搜尋
bin/opensearch-plugin install https://get.infini.cloud/opensearch/analysis-ik/2.12.0
提示:取代您自己的與您的elasticsearch或opensearch相關的版本號碼。
1.建立索引
curl -XPUT http://localhost:9200/index
2.建立映射
curl -XPOST http://localhost:9200/index/_mapping -H ' Content-Type:application/json ' -d '
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
} '
3.索引一些文檔
curl -XPOST http://localhost:9200/index/_create/1 -H ' Content-Type:application/json ' -d '
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/index/_create/2 -H ' Content-Type:application/json ' -d '
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/index/_create/3 -H ' Content-Type:application/json ' -d '
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/index/_create/4 -H ' Content-Type:application/json ' -d '
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'
4.查詢高亮
curl -XPOST http://localhost:9200/index/_search -H ' Content-Type:application/json ' -d '
{
"query" : { "match" : { "content" : "中国" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
'
結果
{
"took" : 14 ,
"timed_out" : false ,
"_shards" : {
"total" : 5 ,
"successful" : 5 ,
"failed" : 0
},
"hits" : {
"total" : 2 ,
"max_score" : 2 ,
"hits" : [
{
"_index" : " index " ,
"_type" : " fulltext " ,
"_id" : " 4 " ,
"_score" : 2 ,
"_source" : {
"content" : "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
},
"highlight" : {
"content" : [
" <tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
]
}
},
{
"_index" : " index " ,
"_type" : " fulltext " ,
"_id" : " 3 " ,
"_score" : 2 ,
"_source" : {
"content" : "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
},
"highlight" : {
"content" : [
"均每天扣1艘<tag1>中国</tag1>渔船 "
]
}
}
]
}
}
設定檔IKAnalyzer.cfg.xml
可以位於{conf}/analysis-ik/config/IKAnalyzer.cfg.xml
或{plugins}/elasticsearch-analysis-ik-*/config/IKAnalyzer.cfg.xml
<? xml version = " 1.0 " encoding = " UTF-8 " ?>
<! DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
< properties >
< entry key = " ext_dict " >custom/mydict.dic;custom/single_word_low_freq.dic</ entry >
< entry key = " ext_stopwords " >custom/ext_stopword.dic</ entry >
< entry key = " remote_ext_dict " >location</ entry >
< entry key = " remote_ext_stopwords " >http://xxx.com/xxx.dic</ entry >
</ properties >
目前插件支援 IK 分析的熱重載字典,透過前面在 IK 設定檔中提到的配置。
< entry key = " remote_ext_dict " >location</ entry >
< entry key = " remote_ext_stopwords " >location</ entry >
其中location
指的是一個URL,例如http://yoursite.com/getCustomDict
。這個要求只需要滿足以下兩點即可完成分段熱更新。
HTTP 請求需要回傳兩個標頭,一個是Last-Modified
,另一個是ETag
。這兩個都是字串類型,如果其中一個發生變化,插件就會取得新的分詞來更新詞庫。
HTTP請求回傳的內容格式為每行一個字,換行符號用n
表示。
滿足以上兩個要求就可以實現熱詞更新,無需重啟ES實例。
您可以將需要自動更新的熱詞放在UTF-8編碼的.txt檔案中。將其放在 nginx 或其他簡單的 HTTP 伺服器下。當.txt檔案被修改時,HTTP伺服器會在客戶端請求該檔案時自動傳回對應的Last-Modified和ETag。您也可以建立一個單獨的工具來從業務系統中提取相關詞彙並更新此 .txt 檔案。
請確保您的自訂字典的文字格式是UTF8編碼。
ik_max_word:執行最細粒度的文字分割。例如,它將“中華人民共和國國歌”分割為“中華人民共和國、中華人民、中華、華人、人民共和國、人民、人、民、共和國、共和、和、國國、國歌”,窮舉生成各種可能的組合,適合Term Query。
ik_smart:執行文字的最粗粒度分割。例如,它將“中華人民共和國國歌”分割為“中華人民共和國、國歌”,適合詞組查詢。
注意:ik_smart 不是 ik_max_word 的子集。
隨意加入 Discord 伺服器來討論有關該項目的任何內容:
https://discord.gg/4tKTMkkvVX
版權所有©️ INFINI 實驗室。
根據 Apache 許可證 2.0 版(“許可證”)獲得許可;除非遵守許可證,否則您不得使用此文件。您可以在以下位置取得許可證副本:
http://www.apache.org/licenses/LICENSE-2.0
除非適用法律要求或書面同意,否則根據許可證分發的軟體均以「原樣」分發,不帶任何明示或暗示的保證或條件。請參閱許可證,了解許可證下管理權限和限制的特定語言。