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
除非适用法律要求或书面同意,否则根据许可证分发的软件均按“原样”分发,不带任何明示或暗示的保证或条件。请参阅许可证,了解许可证下管理权限和限制的特定语言。