前言ElasticSearch默认的分词插件对中文支持很不友好。一段话按规则会以每个中文字符来拆解,再分别建立倒排索引。如"中华人民共和国国歌",默认分词会建立中、华、人、民、共、和、国、歌共8个索引,而不是更合理的"中华人民共和国"、"国歌"索引。这不仅浪费索引空间,基于中文语义的关键字搜索匹配也极其低下。
本文将介绍,如何在elasticSearch中安装IK中文分词插件,以便更好应用于中文汉字信息的搜索场景。
前提条件docker工具安装
安装
本文安装插件基于docker环境,ik插件版本需与es版本匹配,否则会安装不成功。
插件安装安装分在线和离线两种安装方式。
在线安装2.下载安装包,在线安装./bin/elasticsearch-plugininstall
github文件下载可能很慢,下载失败可以替换成其他国内地址。注意插件版本需与es版本匹配。
离线安装手动下载中文分词插件
将插件复制到docker容器
dockercp/tmp/{替换为ES容器名或id}:/usr/share/elasticsearch/plugins进入容器dockerexec-it{替换为ES容器名或id}sh创建插件目录mkdir/usr/share/elasticsearch/plugins/ik移动文件到插件目录mv/usr/share/elasticsearch/plugins//usr/share/elasticsearch/plugins/ik进入目录并解压cd/usr/share/elasticsearch/plugins/退出并重启容器exitdockerrestart{替换为ES容器名或id}验证插件在kibana工具的devtool中执行,匹配以下结果表明插件安装成功。
GET/_analyze?pretty{"analyzer":"ik_smart","text":["中华人民共和国国歌"]}返回结果:{"tokens":[{"token":"中华人民共和国","start_offset":0,"_offset":7,"type":"CN_WORD","position":0},{"token":"国歌","start_offset":7,"_offset":9,"type":"CN_WORD","position":1}]}总结基于esik中文分词插件的安装,本文做了如下介绍。希望能对大家有所帮助和参考。
首先分析了es默认分词器对中文分词的不足;
然后讲解了es在线和离线的两种安装方式;
最后通过es的可视化kibana工具进行插件是否安装成功的验证。
:hover{text-decoration:none;}/*pc样式*/.pgc-card{box-sizing:border-box;height:164px;border:1pxsolide8e8e8;height:120px;position:absolute;right:76px;top:20px;}.pgc-cover{position:absolute;width:162px;height:162px;top:0;left:0;background-size:cover;}.pgc-content{overflow:hidden;position:relative;top:50%;-webkit-transform:translateY(-50%);transform:translateY(-50%);}.pgc-content-title{font-size:18px;color:444;overflow:hidden;text-overflow:ellipsis;padding-top:9px;overflow:hidden;line-height:1.2em;display:-webkit-inline-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;}.pgc-content-price{font-size:22px;color:406599;font-size:14px;text-align:center;}.pgc-buy-text{padding-top:10px;}.pgc-icon-buy{height:23px;width:20px;display:inline-block;background:url();}
Elasticsearch源码解析与优化实战计算机与互联网书籍
¥65
购买