原理: elasticsearch会每隔一分钟从config目录下的IKAnalyzer.cfg.xml文件指定的远程地址中拉取词库,进行更新
部署tomcat
下载地址:https://tomcat.apache.org/download-80.cgi
解压
[ajtuser@centos7 ~]$ tar -zvxf apache-tomcat-8.5.69.tar.gz
[ajtuser@centos7 ~]$ mv apache-tomcat-8.5.69 /usr/local/
[ajtuser@centos7 ~]$ cd /usr/local/
[ajtuser@centos7 local]$ mv apache-tomcat-8.5.69/ tomcat
启动
[ajtuser@centos7 local]$ sh ./tomcat/bin/startup.sh
# 验证tomcat是否启动成功
curl localhost:8080
新增热更新词库文件
# 切换到tomcat webapps的ROOT
[ajtuser@centos7 ROOT]$ cd /usr/local/tomcat/webapps/ROOT/
# 新增额外词库文件
[ajtuser@centos7 ROOT]$ vim extra_words.dic
凯悦
吉屋
老铁
?
# 保存退出,确保用浏览器可以访问到这个文件
http://192.168.100.206:8080/extra_words.dic
修改elasticsearch的ik配置文件
[ajtuser@centos7 analysis-ik]$ cd /usr/local/elasticsearch/config/analysis-ik
[ajtuser@centos7 analysis-ik]$ vim 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"></entry>
? ? ? ? <!--用户可以在这里配置自己的扩展停止词字典-->
? ? ? <entry key="ext_stopwords"></entry>
? ? ? <!--用户可以在这里配置远程扩展字典 -->
? ? ? <entry key="remote_ext_dict">http://localhost:8080/extra_words.dic</entry>
? ? ? <!--用户可以在这里配置远程扩展停止词字典-->
? ? ? <!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
?
验证
# kibana devtool控制台检查分词效果
GET _analyze?pretty
{
"analyzer": "ik_smart",
"text": "老铁凯悦吉屋"
}
# 分词结果
{
?"tokens" : [
? {
? ? ?"token" : "老铁",
? ? ?"start_offset" : 0,
? ? ?"end_offset" : 2,
? ? ?"type" : "CN_WORD",
? ? ?"position" : 0
? },
? {
? ? ?"token" : "凯悦",
? ? ?"start_offset" : 2,
? ? ?"end_offset" : 4,
? ? ?"type" : "CN_WORD",
? ? ?"position" : 1
? },
? {
? ? ?"token" : "吉屋",
? ? ?"start_offset" : 4,
? ? ?"end_offset" : 6,
? ? ?"type" : "CN_WORD",
? ? ?"position" : 2
? }
]
}
这样每次只要更新tomcat的ROOT目录下的extra_words.dic文件中的词库即可,不需要重启elasticsearch
|