您好,登录后才能下订单哦!
Elasticsearch作为一款强大的开源搜索引擎,其核心功能之一就是全文检索。而全文检索的基础是分词(Analysis),即将文本拆分成有意义的词语单元。对于中文搜索而言,由于中文没有像英文那样明显的空格分隔,因此需要专门的中文分词器来处理。
IK分词器(IK Analyzer)是一款优秀的中文分词插件,专门为Elasticsearch设计。它支持细粒度切分和智能切分两种分词模式,能够较好地处理中文文本。本文将详细介绍在Elasticsearch 5.x版本中如何安装、配置和使用IK分词器。
IK分词器是基于开源项目Lucene的Analyzer接口实现的中文分词组件,主要包含以下特性:
两种分词模式:
ik_smart
:智能切分,倾向于较长的词语组合ik_max_word
:细粒度切分,尽可能多地切分出词语支持自定义词典:
兼容性好:
首先需要下载与Elasticsearch 5.x版本兼容的IK分词器插件。可以从以下地址获取:
确保下载的版本与你的Elasticsearch版本匹配。例如Elasticsearch 5.6.16对应的IK分词器版本可能是5.6.16
。
Elasticsearch插件有两种安装方式:
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.16/elasticsearch-analysis-ik-5.6.16.zip
mkdir -p plugins/ik
安装完成后,可以通过以下命令验证插件是否安装成功:
curl -XGET 'http://localhost:9200/_cat/plugins?v'
或者在Elasticsearch启动日志中查看是否加载了IK插件。
在创建索引时,可以在mapping中指定使用IK分词器:
PUT /my_index
{
"settings": {
"analysis": {
"analyzer": {
"ik_smart_analyzer": {
"type": "custom",
"tokenizer": "ik_smart"
},
"ik_max_analyzer": {
"type": "custom",
"tokenizer": "ik_max_word"
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}
}
可以使用_analyze
API测试分词效果:
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": 7,
"end_offset": 9,
"type": "CN_WORD",
"position": 8
}
]
}
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
}
]
}
可以看到ik_max_word
会尽可能多地切分出词语,而ik_smart
则会做更智能的组合。
IK分词器支持自定义词典,可以扩展专业术语、新词等。
IK分词器的词典文件通常位于:
config/analysis-ik/
目录下,主要包含:
main.dic
:主词典quantifier.dic
:量词词典suffix.dic
:后缀词典surname.dic
:姓氏词典stopword.dic
:停用词词典IKAnalyzer.cfg.xml
:配置文件编辑IKAnalyzer.cfg.xml
文件:
<?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/mydict.dic;custom/single_word_low_freq.dic</entry>
<!-- 用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">custom/ext_stopword.dic</entry>
</properties>
词典文件是纯文本文件,每行一个词,UTF-8编码。例如:
区块链
人工智能
机器学习
云计算
大数据
IK分词器支持热更新词典,无需重启Elasticsearch。配置方法:
IKAnalyzer.cfg.xml
:<entry key="remote_ext_dict">http://your_server.com/getCustomDict</entry>
<entry key="remote_ext_stopwords">http://your_server.com/getStopwordDict</entry>
你的服务器需要返回词典内容,响应头包含Last-Modified
和ETag
字段
IK分词器会定期(默认60秒)检查词典更新
通常建议:
ik_max_word
,尽可能多地切分词语,提高召回率ik_smart
,提高准确率PUT /my_index
{
"mappings": {
"my_type": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}
}
对于同一个字段,可以同时使用多种分词方式:
PUT /my_index
{
"mappings": {
"my_type": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"smart": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
}
}
这样可以通过content
或content.smart
字段使用不同的分词方式搜索。
GET /my_index/_search
{
"query": {
"match": {
"content": "中华人民共和国"
}
}
}
问题:某些专业术语或新词没有被正确切分
解决方案: - 将这些词添加到自定义词典中 - 定期更新词典,特别是对于新出现的术语
问题:配置的停用词仍然出现在搜索结果中
解决方案: - 检查停用词词典文件路径是否正确 - 确保文件编码为UTF-8无BOM格式 - 检查停用词是否确实存在于词典文件中
问题:修改了远程词典但ES没有加载最新内容
解决方案:
- 检查HTTP服务是否正常返回词典内容
- 确保响应头包含Last-Modified
和ETag
字段
- 检查IK配置的URL是否正确
问题:使用IK分词器后索引速度变慢
解决方案:
- 考虑使用ik_smart
代替ik_max_word
进行索引
- 优化词典大小,移除不必要的词语
- 增加ES集群资源
词典管理:
测试策略:
_analyze
API测试效果监控:
多语言支持:
IK分词器是Elasticsearch中文搜索的重要组件,合理配置和使用IK分词器可以显著提升中文搜索体验。本文介绍了IK分词器的安装、配置、使用和优化方法,希望能帮助开发者更好地在Elasticsearch 5.x中实现中文搜索功能。
随着业务发展,记得定期检查和更新词典,特别是对于新兴术语和业务专有名词。同时,结合业务场景选择合适的分词策略,平衡召回率和准确率,才能构建出高效的搜索系统。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。